将嵌套查询与SQLite中的另一个结果合并

问题描述 投票:0回答:1

我有两组查询:

SELECT 
  t.series_name, 
  ti.num_views_per_telecast 
FROM 
  (
    SELECT 
      ti.telecast_id, 
      ti.network_id, 
      count(*) as num_views_per_telecast 
    FROM 
      tunein AS ti 
      INNER JOIN affiliates AS a ON ti.network_id = a.network_id 
    WHERE 
      ti.dvr_time_shift = 'L+SD' 
      and a.network_name = 'ABC' 
    group by 
      ti.telecast_id, 
      ti.network_id
  ) ti 
  inner join telecast AS t On t.telecast_id = ti.telecast_id 
ORDER BY 
  ti.num_views_per_telecast DESC

select 
  distinct * 
from 
  telecast 
where 
  episode_name = 'friday night dinner' 
  and series_name = 'A Million Little Things' 
  and date(program_start_local) = '2018-10-17'

我希望能够将两者结合起来,以便可以在底部查询中获得剧集的num_views_per_telecast。不太确定如何将这些内容内部连接,因此我可以保留第一组查询的结果。

表的连接方式如下:

enter image description here

我将如何结合这些??

sql r sqlite
1个回答
2
投票

猜测:

SELECT 
  t.*,
  ti.num_views_per_telecast
FROM
  (
    SELECT 
      ti.telecast_id,
      ti.network_id,
      count(*) as num_views_per_telecast
    FROM
      tunein AS ti
      INNER JOIN
      affiliates AS a
      ON
        ti.network_id = a.network_id
      WHERE
        ti.dvr_time_shift = 'L+SD' and 
        a.network_name = 'ABC'
      group by
        ti.telecast_id,
        ti.network_id
  )ti
  inner join telecast AS t
  On 
    t.telecast_id = ti.telecast_id

  --from query2
  where 
    t.episode_name = 'friday night dinner'
    and t.series_name = 'A Million Little Things'
    and t.date(program_start_local) = '2018-10-17'


  ORDER BY ti.num_views_per_telecast DESC


出于注释中给出的原因,DISTINCT是多余的,因此您似乎希望广播中的所有符合某些条件的行。鉴于您的第一个查询包含电视广播,但没有任何条件,并且您仅从其中选择一列,因此将两者合并是一种情况,即将选择的列数增加到(全部来自电视广播)加上其他内容,然后从查询中添加where子句2以限制查询1中的行]

© www.soinside.com 2019 - 2024. All rights reserved.