简体   繁体   English

第一个表中的所有值,即使其他表上没有匹配项

[英]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.

相关问题 连接两个表。 Select 一个表中的所有行并且仅匹配另一表中的值? - Join two tables. Select all rows from one table and only matching values from other table? 如何在两个表中插入其他值的同时,根据两个表中的userid将第一个表中的用户名插入第二个表中 - how to insert user name from first table into second table based on userid from both tables whle inserting other values into table2 如何通过替换其他表中两个字段的值来从表中选择所有记录? - How to select all records from a table by replacing values for two fields from other tables? 从第一个表中选择任何内容 - select nothing from first table 联合两个表中的所有客户并从第二个表中分配日期值,以防客户在第一个表中不存在 - Union all customers from two tables and assign date values from second table in case customer does not exist in first table 如何从多个表中获取所有记录,甚至没有值的记录 - How to get all records from multiple tables, even those with no values 使用 SQL 根据第一个表从其他表中选择行 - Selecting rows from other tables based on the first table using SQL 当它们没有共同点时,如何从多个其他表中更新表 1? - How to update Table 1 from multiple other tables when they have nothing in common? 从具有特定表名的所有表中选择所有值 - Select all values from all tables with specific table name 连接两个表,列出第一个表的所有结果 - Joining two tables listing all the results from the first table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM