我有三个表(如下所示,带有相关列)
表SALESSaleCodes
"SaleCode" VARCHAR(2) COLLATE "ANSI" DEFAULT '',
"Description" VARCHAR(25) COLLATE "ANSI" DEFAULT ''
表INVOIHeader
"DocNum" INTEGER DEFAULT 0,
"AcctDate" DATE
表INVOIItems
"DocNum" INTEGER DEFAULT 0,
"SaleCode" VARCHAR(2) COLLATE "ANSI" DEFAULT '',
"Cost" DECIMAL(19,2) DEFAULT 0.00,
"SellPrice" DECIMAL(19,2) DEFAULT 0.00,
"LaborEach" DECIMAL(19,2) DEFAULT 0.00,
"QtySold" DECIMAL(19,2) DEFAULT 0.00
我写了这个语句,它返回SALESSaleCodes中的所有salecodes以及我想要获得的各个列,包括count为零且sum为NULL的那些。
SELECT a.SaleCode, a.Description, COUNT(b.SaleCode) AS Count, SUM(b.SellPrice * b.QtySold) AS Parts,
SUM(b.LaborEach * b.QtySold) AS Labor, SUM(b.Cost * b.QtySold) AS Cost
FROM SALESSaleCodes a
LEFT JOIN INVOIItems b ON a.SaleCode = b.SaleCode
GROUP BY a.SaleCode
ORDER BY a.SaleCode
我需要的是考虑来自INVOIHeader的AcctDate,其中该日期在两个给定日期之间。
我试过这个
SELECT a.SaleCode, a.Description, COUNT(b.SaleCode) AS Count, SUM(b.SellPrice * b.QtySold) AS Parts,
SUM(b.LaborEach * b.QtySold) AS Labor, SUM(b.Cost * b.QtySold) AS Cost
FROM SALESSaleCodes a
LEFT JOIN INVOIItems b ON a.SaleCode = b.SaleCode
LEFT JOIN INVOIHeader c ON b.DocNum = c.DocNum
WHERE c.AcctDate BETWEEN
CAST('2013-10-01' AS DATE) AND
CAST('2013-10-31' AS DATE)
GROUP BY b.SaleCode
ORDER BY b.SaleCode
但是返回的只是属于该范围内的SaleCodes而且它没有给我SALESSaleCodes的所有SaleCodes。
我需要什么声明才能获得所有SaleCodes,无论它们是否在此范围内,并且仅包含此日期范围内的总和和计数。其他总和和计数将为NULL。
我在使用SQL 2003标准(ANSI ISO / IEC 9075:2003)的ElevateDB中工作。我知道大多数人都是Oracle,SQLServer或MySQL用户,但我想我会在这里问一下,看看你们能想出什么。
答案 0 :(得分:1)
...
WHERE (
(c.AcctDate BETWEEN CAST('2013-10-01' AS DATE) AND CAST('2013-10-31' AS DATE))
OR c.AcctDate IS NULL
)
...