我有一個查詢,我剝離了一些不需要的字段並在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
我不;噸瞭解缺什麼,讓我不同的結果。我試着逐個挑選查詢,但我沒有看到它打破的地方。
您的查詢看起來像您期望在您的組和變量中具有多個值。你不可能通過變化的方式來做到這一點。 – 2015-02-10 16:33:53