我有一个查询,我正在剥离一些不需要的字段并在where子句中放入一个限定符。 导出到excel时,除去Load Size(参见第一个查询)中的所有字段,我得到84行。 在第二个查询中,我只是删除一些字段并添加限定符以除去那些负载大小为0的字段。我使用相同的参数运行它,但得到87行。我不知道删除是什么触发了这一点。 原始查询:
Declare @start Varchar(20),
@end VarChar(20),
@division varchar(2),
@group varchar(20)
set @start = '02/01/2015'
set @end = '02/09/2015'
set @division= '02'
set @group = 'anodize'
--Get the most recent order date
SELECT
MAX(DATEPHYSICAL) AS 'DATEPHYSICAL',
ITEMID
INTO
#tempOrderDates
FROM
dbo.INVENTTRANS
GROUP BY
ITEMID
SELECT
ITEMID,
SUM(dbo.CUSTINVOICETRANS.QTY) AS 'Quanity',
SUM(LINEAMOUNTMST) AS 'Sales',
COUNT(DISTINCT dbo.CUSTINVOICEJOUR.SALESID) AS 'Total Orders'
INTO
#tempItemRevenue
FROM
dbo.CUSTINVOICEJOUR INNER JOIN
dbo.CUSTINVOICETRANS ON dbo.CUSTINVOICEJOUR.INVOICEID = dbo.CUSTINVOICETRANS.INVOICEID
WHERE
dbo.CUSTINVOICETRANS.DIMENSION2_ IN (@division)
AND
CONVERT(DATETIME, dbo.CUSTINVOICETRANS.INVOICEDATE, 101) BETWEEN @start AND @end
GROUP BY
ITEMID
SELECT
ITEMGROUPID AS 'Process',
[DESCRIPTION] AS 'Division',
dbo.INVENTTABLE.ITEMID AS 'Item ID',
EXTITEM AS 'Cust Item ID',
dbo.INVENTTABLE.CUSTNAME AS 'Customer',
cu.[SalesGroup],
dbo.USERINFO.NAME AS 'Sales Engineer',
cu.[Street],
cu.[City],
cu.[State],
cu.[ZipCode],
ISNULL(PRICE / CASE WHEN PRICEUNIT = 0 THEN NULL ELSE PRICEUNIT END, 0) AS 'Unit Price',
PRICEDATE AS 'Date of Price',
LOADQTY AS 'Load Size',
LOADQTY * ISNULL(PRICE / CASE WHEN PRICEUNIT = 0 THEN NULL ELSE PRICEUNIT END, 0) AS 'Load Value',
Sales,
Quanity,
[Total Orders],
CEILING(ISNULL(Quanity / CASE WHEN [Total Orders] = 0 THEN NULL ELSE [Total Orders] END, 0)) AS 'Avg. Order Quanity',
DATEPHYSICAL AS 'Last Order Date'
FROM
dbo.INVENTTABLE INNER JOIN
dbo.INVENTTABLEMODULE ON dbo.INVENTTABLE.ITEMID = dbo.INVENTTABLEMODULE.ITEMID INNER JOIN
#tempOrderDates ON dbo.INVENTTABLE.ITEMID = #tempOrderDates.ITEMID INNER JOIN
#tempItemRevenue ON dbo.INVENTTABLE.ITEMID = #tempItemRevenue.ITEMID INNER JOIN
dbo.DIMENSIONS ON NUM = dbo.INVENTTABLE.DIMENSION2_ INNER JOIN
dbo.CUSTTABLE cu ON ACCOUNTNUM = CUSTACCOUNT LEFT OUTER JOIN
dbo.SMMSALESUNITMEMBERS ON SALESUNITID = cu.SALESGROUP AND SALESMANAGER = 1 INNER JOIN
dbo.USERINFO ON ID = SALESMANID
WHERE
MODULETYPE = 2
AND
ITEMGROUPID IN (@group)
AND
dbo.INVENTTABLE.DIMENSION2_ IN (@division)
DROP TABLE #tempOrderDates
DROP TABLE #tempItemRevenue
修改后的查询摆脱了推销员的东西和客户信息:
Declare @start Varchar(20),
@end VarChar(20),
@division varchar(2),
@group varchar(20)
set @start = '02/01/2015'
set @end = '02/09/2015'
set @division= '02'
set @group = 'anodize'
SELECT
ITEMID,
SUM(dbo.CUSTINVOICETRANS.QTY) AS 'Quanity',
SUM(LINEAMOUNTMST) AS 'Sales',
COUNT(DISTINCT dbo.CUSTINVOICEJOUR.SALESID) AS 'Total Orders'
INTO
#tempItemRevenue
FROM
dbo.CUSTINVOICEJOUR INNER JOIN
dbo.CUSTINVOICETRANS ON dbo.CUSTINVOICEJOUR.INVOICEID = dbo.CUSTINVOICETRANS.INVOICEID
WHERE
dbo.CUSTINVOICETRANS.DIMENSION2_ IN (@division)
AND
CONVERT(DATETIME, dbo.CUSTINVOICETRANS.INVOICEDATE, 101) BETWEEN @start AND @end
GROUP BY
ITEMID
SELECT
ITEMGROUPID AS 'Process',
[DESCRIPTION] AS 'Division',
LOADQTY AS 'Load Size',
LOADQTY * ISNULL(PRICE / CASE WHEN PRICEUNIT = 0 THEN NULL ELSE PRICEUNIT END, 0) AS 'Load Value',
Sales,
Quanity
FROM
dbo.INVENTTABLE
INNER JOIN
dbo.INVENTTABLEMODULE ON dbo.INVENTTABLE.ITEMID = dbo.INVENTTABLEMODULE.ITEMID
inner JOIN
#tempItemRevenue ON dbo.INVENTTABLE.ITEMID = #tempItemRevenue.ITEMID
INNER JOIN
dbo.DIMENSIONS ON NUM = dbo.INVENTTABLE.DIMENSION2_
WHERE
MODULETYPE = 2
AND
ITEMGROUPID IN (@group)
AND
dbo.INVENTTABLE.DIMENSION2_ IN (@division)
and LOADQTY >0
DROP TABLE #tempItemRevenue
我不明白缺少什么会给我带来不同的结果。我已经尝试一点一点地挑选查询,但我没有看到它在哪里中断。
答案 0 :(得分:1)
问题很可能出在您删除的联接中。如果删除销售人员和客户表,则可能会返回这些联接正在删除的行。
我建议检查联接中的值,看看有什么区别。您可能有一些NULL或一些不存在的ID或行不存在的行:SALESMANAGER = 1
。
看到分别只有84行和87行,您应该能够通过眼睛检查差异,然后检查这些记录是否有差异。
答案 1 :(得分:0)
我认为问题是你也删除了一些INNER JOIN:这可能会导致更多的行出现。尝试删除字段,并保留第一个查询的INNER JOINS。
问候。
答案 2 :(得分:0)
第一个查询是您加入表CUSTTABLE
而第二个查询未加入。
您可能会发现3个与CUSTTABLLE
表
答案 3 :(得分:0)
我差异的第一选择是删除#tempOrderDates或CustTable。它们属于内部联接,因此删除可以使其他记录不在原始查询中。
由于结果集太小,请查找第二个结果集中的记录示例,而不是第一个结果集。然后,获取原始查询,更改为select *并将您在第二个查询中删除的所有内部联接更改为左联接,并查看带有thsoe记录的表数据。您可以考虑仅对记录进行过滤( s)你有兴趣看到。