SQL Help : Exception report - extended

那年仲夏 提交于 2021-02-11 15:00:42

问题


Extend on: SQL help - exception report

In my previous post I requested hep for:

I have:

  • a company table (CompanyID, CompanyName),
  • Date table (Datekey int, date, isTradingHoliday 0/1),
  • Fact table (id, datekey, companyID, StockClosePrice)

I need help to write a query to find for which days and for which companies I don't have data in the fact table.

SQL below does the job

select c.*, d.*
from companies c
cross join dates d 
where d.isTradingHoliday = 0
  and not exists (select 1 from facts f 
                  where f.datekey = d.datekey and f.companyID = c.companyID) 

I have noticed for some companies, for some days, StockClosePrice is 0.00 - I want to include those in the exception report.

Any help is appreciated


回答1:


I think you just want one more condition in the subquery:

where d.isTradingHoliday = 0 and
      not exists (select 1
                  from facts f
                  where f.datekey = d.datekey and
                        f.companyID = c.companyID and
                        f.StockClosePrice <> 0.00
                 ) 



回答2:


DECLARE @Year   int = 2019

SELECT  CT.*, DT.*
FROM            FactTable       FT
    RIGHT JOIN   CompanyTable    CT  ON  CT.CompanyID    =   FT.companyID
    RIGHT JOIN   DateTable       DT  ON  DT.Datekey      =   FT.datekey

WHERE
        YEAR(DT.DateField)  =   @Year
    AND FT.ID               IS  NULL


来源:https://stackoverflow.com/questions/61821248/sql-help-exception-report-extended

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!