[英]All values from first table, even if nothing matchs on other tables
I have three tables (shown below with the relevant columns) 我有三个表(如下所示,带有相关的列)
Table SALESSaleCodes 表SALESSaleCodes
"SaleCode" VARCHAR(2) COLLATE "ANSI" DEFAULT '',
"Description" VARCHAR(25) COLLATE "ANSI" DEFAULT ''
Table INVOIHeader 表INVOIHeader
"DocNum" INTEGER DEFAULT 0,
"AcctDate" DATE
Table INVOIItems 表INVOI项目
"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
I wrote this statement which returns all salecodes in SALESSaleCodes and the respective columns I am trying to get including those where count is zero and the sums are NULL. 我编写了此语句,该语句返回SALESSaleCodes中的所有salecode和我尝试获取的相应列,包括count为零且总和为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
What I need is one that takes into account the AcctDate from INVOIHeader where that date is between two given dates. 我需要的是考虑到INVOIHeader中AcctDate的日期,该日期介于两个给定日期之间。
I tried this 我试过了
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
But what this returns is only the SaleCodes that fall in that range and it is not giving me all of the SaleCodes from SALESSaleCodes . 但是,返回的结果只是落在该范围内的SaleCodes,并没有给我SALESSaleCodes提供的所有SaleCodes。
What statement would I need to get all of the SaleCodes whether they are within this range or not and only the sums and counts that are in this date range. 无论它们是否在此范围内,并且仅在该日期范围内的总和和计数,我都需要什么语句来获取所有SaleCode。 Other sums and counts would be NULL.
其他总和为NULL。
I am working in ElevateDB which uses SQL 2003 standard (ANSI ISO/IEC 9075:2003). 我正在使用SQL 2003标准(ANSI ISO / IEC 9075:2003)的ElevateDB中工作。 I know most of you are Oracle, SQLServer, or MySQL users but I thought I would ask on here and see what you guys could come up with.
我知道你们中的大多数都是Oracle,SQLServer或MySQL用户,但我想我会在这里问一下,看看你们能想到什么。
...
WHERE (
(c.AcctDate BETWEEN CAST('2013-10-01' AS DATE) AND CAST('2013-10-31' AS DATE))
OR c.AcctDate IS NULL
)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.