这是我的SQL查询
SELECT
T0.DocDate, T0.DocNum, T0.TrnspCode, T0.[LicTradNum] as 'Cust Type',
T0.CardName as 'Customer Name', T2.ItemName as 'Item Description',
T1.Quantity , T1.LineTotal as 'Total Sales S$',
(T1.GrssProfit-ISNULL(T1.U_Add_Cost_SGD,0)) as 'Net GP S$',
CASE when T0.TrnspCode =5 then '0' else 80 end as Delivery
FROM
[dbo].[OINV] T0
INNER JOIN
[dbo].[INV1] T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN
[dbo].[OITM] T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN
OITB T3 ON T2.ItmsGrpCod = T3.ItmsGrpCod
INNER JOIN
OSLP T4 ON T0.SlpCode = T4.SlpCode
LEFT OUTER JOIN
OIDC T5 ON T0.Indicator=T5.Code
LEFT OUTER JOIN
OSHP T6 ON T0.TrnspCode = T6.TrnspCode
WHERE
T0.DocType <> 'S'
AND T4.SlpName <> '-No Sales Employee-'
AND T2.ItemName LIKE '%compressed wood%'
AND T0.[LicTradNum] LIKE '%Enno%'
AND year(T0.DocDate) = YEAR(getdate())
AND month(T0.DocDate) = MONTH(getdate())
运行上述查询时的输出:
DocDate DocNum TrnspCode Cust Type Customer Name Item Description Quantity Total Sales S$ Net GP S$ Delivery
----------------------- ----------- --------- -------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- -----------
2016-10-03 00:00:00.000 30072379 4 Enno T & C Manufacturing Company Pte Ltd Compressed Wooden Pallet, Model: EGPW-101, Size: 1100mm x 1100mm x 135mm 200.000000 2800.000000 1283.420000 80
2016-10-03 00:00:00.000 30072379 4 Enno T & C Manufacturing Company Pte Ltd Compressed Wooden Pallet, Model: EGPW-101, Size: 1100mm x 1100mm x 135mm 1.000000 0.000000 -7.580000 80
2016-10-04 00:00:00.000 30072416 4 Enno-Ex Biomin Singapore Pte Ltd Compressed Wood Pallet, Model: EGPW-104, Size: 1220mm x 1020mm x 140mm 100.000000 1450.000000 705.000000 80
2016-10-04 00:00:00.000 30072435 4 Enno GES (Singapore) Pte Ltd Compressed Wood Pallet, Model: EGPW-104, Size: 1220mm x 1020mm x 140mm 50.000000 725.000000 352.500000 80
2016-10-05 00:00:00.000 30072413 4 Enno Markem-Imaje Compressed Wooden Pallet, Model: EGPW-101, Size: 1100mm x 1100mm x 135mm 50.000000 700.000000 320.850000 80
2016-10-06 00:00:00.000 30072474 4 Enno Faci Asia Pacific Pte Ltd Compressed Wooden Pallet, Model: EGPW-101, Size: 1100mm x 1100mm x 135mm 50.000000 775.000000 389.090000 80
最后一列,传递,还应该依赖于DocNum行第一次出现在查询中。当DocNum出现在前一行时,我们将返回0,否则我们将根据TrnspCode计算返回值,如查询已经做的那样。
你可以在这个例子中看到DocNum 30072379,第一次出现它必须返回80,而在第二个幻象它应该返回0:
DocDate DocNum TrnspCode Cust Type Customer Name Item Description Quantity Total Sales S$ Net GP S$ Delivery
----------------------- ----------- --------- -------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- -----------
2016-10-03 00:00:00.000 30072379 4 Enno T & C Manufacturing Company Pte Ltd Compressed Wooden Pallet, Model: EGPW-101, Size: 1100mm x 1100mm x 135mm 200.000000 2800.000000 1283.420000 80
2016-10-03 00:00:00.000 30072379 4 Enno T & C Manufacturing Company Pte Ltd Compressed Wooden Pallet, Model: EGPW-101, Size: 1100mm x 1100mm x 135mm 1.000000 0.000000 -7.580000 0
2016-10-04 00:00:00.000 30072416 4 Enno-Ex Biomin Singapore Pte Ltd Compressed Wood Pallet, Model: EGPW-104, Size: 1220mm x 1020mm x 140mm 100.000000 1450.000000 705.000000 80
2016-10-04 00:00:00.000 30072435 4 Enno GES (Singapore) Pte Ltd Compressed Wood Pallet, Model: EGPW-104, Size: 1220mm x 1020mm x 140mm 50.000000 725.000000 352.500000 80
2016-10-05 00:00:00.000 30072413 4 Enno Markem-Imaje Compressed Wooden Pallet, Model: EGPW-101, Size: 1100mm x 1100mm x 135mm 50.000000 700.000000 320.850000 80
2016-10-06 00:00:00.000 30072474 4 Enno Faci Asia Pacific Pte Ltd Compressed Wooden Pallet, Model: EGPW-101, Size: 1100mm x 1100mm x 135mm 50.000000 775.000000 389.090000 80
请帮我修改我的查询
答案 0 :(得分:0)
我尽我所能,可能有效:
SELECT T0.DocDate, T0.DocNum, T0.TrnspCode, T0.[LicTradNum] as 'Cust Type',
T0.CardName as 'Customer Name', T2.ItemName as 'Item Description',
T1.Quantity , T1.LineTotal as 'Total Sales S$',
(T1.GrssProfit-ISNULL(T1.U_Add_Cost_SGD,0)) as 'Net GP S$',
CASE when T0.TrnspCode =5 then '0'
when COALESCE(T0.DocNum) = T0.DocNum then '80'
when
(
SELECT count(T0.DocNum) FROM [dbo].[OINV] T0
GROUP BY T0.DocNum
)>1 then '0'
else 80 end as Delivery
FROM [dbo].[OINV] T0
INNER JOIN [dbo].[INV1] T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN [dbo].[OITM] T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN OITB T3 ON T2.ItmsGrpCod = T3.ItmsGrpCod
INNER JOIN OSLP T4 ON T0.SlpCode = T4.SlpCode
LEFT OUTER JOIN OIDC T5 ON T0.Indicator=T5.Code
LEFT OUTER JOIN OSHP T6 ON T0.TrnspCode = T6.TrnspCode
WHERE T0.DocType <> 'S'
AND T4.SlpName <> '-No Sales Employee-'
AND T2.ItemName Like '%compressed wood%'
and T0.[LicTradNum] like '%Enno%'
and year(T0.DocDate) =YEAR(getdate()) and month(T0.DocDate) = MONTH(getdate())
答案 1 :(得分:0)
这是您的查询,其子查询检查是否已出现DocNum:
SELECT T0.DocDate, T0.DocNum, T0.TrnspCode, T0.[LicTradNum] as 'Cust Type',
T0.CardName as 'Customer Name', T2.ItemName as 'Item Description',
T1.Quantity , T1.LineTotal as 'Total Sales S$',
(T1.GrssProfit-ISNULL(T1.U_Add_Cost_SGD,0)) as 'Net GP S$',
CASE when T0.TrnspCode =5 or exists (select * from OINV T7 where T7.DocNum = T0.DocNum and T7.DocDate < T0.DocDate) then '0'
else 80 end as Delivery
FROM [dbo].[OINV] T0
INNER JOIN [dbo].[INV1] T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN [dbo].[OITM] T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN OITB T3 ON T2.ItmsGrpCod = T3.ItmsGrpCod
INNER JOIN OSLP T4 ON T0.SlpCode = T4.SlpCode
LEFT OUTER JOIN OIDC T5 ON T0.Indicator=T5.Code
LEFT OUTER JOIN OSHP T6 ON T0.TrnspCode = T6.TrnspCode
WHERE T0.DocType <> 'S'
AND T4.SlpName <> '-No Sales Employee-'
AND T2.ItemName Like '%compressed wood%'
and T0.[LicTradNum] like '%Enno%'
and year(T0.DocDate) =YEAR(getdate()) and month(T0.DocDate) = MONTH(getdate())