问题
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