Return 行的值在另一个 table 中不存在

Return rows with values that don't exist in another table

我想查询至少提交了一份报告但在前八个季度都没有报告的公司。以下是查询:

select distinct ct.CompanyId, c.companyName, 
'Reported Quarters' = STUFF(
                                (
                                    select distinct ', ' + CAST(QuarterId as varchar(max))
                                    from CompanyTransaction as b
                                    where b.CompanyId = ct.CompanyId
                                    FOR XML PATH('') 
                                ),1,1,''
                            )
                            
from CompanyTransaction as ct
left join ( select CompanyId, count(distinct QuarterId) as cnt
            from CompanyTransaction
            group by CompanyId
            having count(distinct QuarterId) = 8
          ) as ct2 on ct.CompanyId = ct2.CompanyId
join Company as c on ct.CompanyId = c.CompanyId
where ct2.CompanyId is null
order by ct.CompanyId

我在 CompanyTransaction table 上进行了自我离开加入,以检索没有 8 个不同 QuarterId 总数的公司。查询结果显示公司 ID、公司名称和报告季度(每个公司提交的季度)。

CompanyId   CompanyName                 Reported Quarters
1           Cabbage Corp                20191, 20192, 20193
2           Future Industries           20191, 20192, 20194, 20201
3           Republic City               20191, 20192, 20193
4           Keum Enterprises            20191, 20203
5           Varrick Global Industries   20191, 20192, 20193, 20194, 20201, 20202, 20204

我想展示但不确定实施的是公司未报告的季度:

CompanyId   CompanyName                 Missing Quarters
1           Cabbage Corp                20194, 20201, 20202, 20203, 20204
2           Future Industries           20193, 20202, 20203, 20204
3           Republic City               20194, 20201, 20202, 20203, 20204
4           Keum Enterprises            20192, 20193, 20194, 20201, 20202, 20204
5           Varrick Global Industries   20203

非常感谢任何帮助 and/or 的建议!

What I would like to show, and unsure of implementing, are the quarters that a company that has not reported for:

让我假设所有桌子和所有宿舍都在 CompanyTransaction。然后想法是为所有公司和季度生成行并过滤掉不存在的行。

以下将使用string_agg()。您似乎知道如何在必要时转换为 stuff()

select c.companyid, c.companyname,
       string_agg(q.quarterid, ', ') within group (order by q.quarterid)
from (select distinct companyid, companyname
      from CompanyTransaction
     ) c cross join
     (select distinct quarterid
      from CompanyTransaction
     ) q left join
     CompanyTransaction ct
     on ct.companyid = c.companyid and
        ct.quarterid = q.quarterid
where ct.companyid is null
group by c.companyid, c.companyname;

其实老式的聚合方法也没有那么糟糕:

select c.companyid, c.companyname,
       stuff( (select concat(', ', q.quarterid)
               from quarters q left join
                    CompanyTransaction ct
                    on ct.quarterid = q.quarterid and
                       ct.companyid = c.companyid
               where ct.quarterid is null
               order by q.quarterid
               for xml path ('')
              ), 1, 2, ''
            ) as missing_quarterids
from (select distinct companyid, companyname
      from CompanyTransaction
     ) c;