我如何返回一个月没有销售的记录,例如 2004 年 7 月?
用户将提供一个日期,比如 2004 年 7 月,然后我需要查看没有销售的产品。
SELECT
DP.ProductID
,DP.Name
,FS.OrderDate
,FS.OrderQty
,FS.OrderAmount
FROM dbo.DimProduct AS DP
LEFT JOIN dbo.FactSales as FS on FS.ProductID = DP.ProductID
SELECT DP.*
FROM dbo.DimProduct AS DP
LEFT JOIN dbo.FactSales as FS
ON FS.ProductID = DP.ProductID AND
DATENAME(month, FS.OrderDate) = 'July' AND
YEAR(FS.OrderDate) = 2004
WHERE FS.ProductID IS NULL
也许这可以解决您的问题:
SELECT
DP.ProductID
,DP.Name
,FS.OrderDate
,FS.OrderQty
,FS.OrderAmount
FROM dbo.DimProduct AS DP
LEFT JOIN dbo.FactSales as FS on FS.ProductID = DP.ProductID
WHERE FS.OrderAmount == 0
SELECT DP.*
FROM dbo.DimProduct AS DP
WHERE NOT EXISTS ( SELECT 1
FROM dbo.FactSales AS FS
WHERE FS.ProductID = DP.ProductID
AND DATENAME(month, FS.OrderDate) = 'July'
AND YEAR(FS.OrderDate) = 2004 )