我有这个:
use [VERPLOEGEN-NAV2009-LIVE]
SELECT V.No_, VPL.[Location Code],V.Name,
SUM(VPL.Amount) AS Total,
V.[Freight-free Limit]
-- ISNULL(CAST( V.[Freight-free Limit] AS VARCHAR(10)), 'geen waarde')
FROM [Verploegen POC$Purch_ Inv_ Line] VPL
JOIN [Verploegen POC$Vendor] V
ON VPL.[Buy-from Vendor No_] = V.No_
--WHERE [Buy-from Vendor No_] = '3929' -- is empty??' --'3932' --'3929' --'3923' --'3904' --'3885' --'3820' --'3894'
WHERE VPL.[Buy-from Vendor No_] = '3929'
AND VPL.Type = 1 AND ([Posting Date] BETWEEN '2016-01-01' AND '2016-12-31')
GROUP BY VPL.[Location Code], V.[Freight-free Limit], V.No_, V.Name --, VPL.[Direct Unit Cost]
ORDER BY V.Name
并且有一个属性:VPL.Type = 1,但你与type=2
具有相同的属性。但是在结果中我想要显示两个属性,使用type = 1和type =2
。因为type = 1
的总和不同,所以type = 2.
的总和不同type = 1
的结果是这样的:
3929 AM ASF Fischer BV 20.89000000000000000000 350.00000000000000000000
3929 DB ASF Fischer BV -0.29000000000000000000 350.00000000000000000000
3929 DL ASF Fischer BV 0.34000000000000000000 350.00000000000000000000
3929 RD ASF Fischer BV 20.06000000000000000000 350.00000000000000000000
3929 ZM ASF Fischer BV 0.04000000000000000000 350.00000000000000000000
那么如何使用type = 2创建一个额外的列?
谢谢
如果我这样做:
use [VERPLOEGEN-NAV2009-LIVE]
SELECT V.No_, VPL.[Location Code],V.Name,
SUM(VPL.Amount) AS Total,
SUM(VPL.Amount) as total2,
V.[Freight-free Limit]
-- ISNULL(CAST( V.[Freight-free Limit] AS VARCHAR(10)), 'geen waarde')
FROM [Verploegen POC$Purch_ Inv_ Line] VPL
JOIN [Verploegen POC$Vendor] V
ON VPL.[Buy-from Vendor No_] = V.No_
--WHERE [Buy-from Vendor No_] = '3929' -- is empty??' --'3932' --'3929' --'3923' --'3904' --'3885' --'3820' --'3894'
WHERE VPL.[Buy-from Vendor No_] = '3929'
AND VPL.Type = 1 AND ([Posting Date] BETWEEN '2016-01-01' AND '2016-12-31')
GROUP BY VPL.[Location Code], V.[Freight-free Limit], V.No_, V.Name --, VPL.[Direct Unit Cost]
ORDER BY V.Name
结果这个:
3929 AM ASF Fischer BV 20.89000000000000000000 20.89000000000000000000 350.00000000000000000000
3929 DB ASF Fischer BV -0.29000000000000000000 -0.29000000000000000000 350.00000000000000000000
3929 DL ASF Fischer BV 0.34000000000000000000 0.34000000000000000000 350.00000000000000000000
3929 RD ASF Fischer BV 20.06000000000000000000 20.06000000000000000000 350.00000000000000000000
3929 ZM ASF Fischer BV 0.04000000000000000000 0.04000000000000000000 350.00000000000000000000
但Total和Total2现在具有相同的值
你的意思是这样吗?
use [VERPLOEGEN-NAV2009-LIVE]
SELECT V.No_, VPL.[Location Code],V.Name,
sum(case when Type =1 then 1 else 0 end )As type1sum,
sum(case when Type =2 then 1 else 0 end )As type2sum,
V.[Freight-free Limit]
-- ISNULL(CAST( V.[Freight-free Limit] AS VARCHAR(10)), 'geen waarde')
FROM [Verploegen POC$Purch_ Inv_ Line] VPL
JOIN [Verploegen POC$Vendor] V
ON VPL.[Buy-from Vendor No_] = V.No_
--WHERE [Buy-from Vendor No_] = '3929' -- is empty??' --'3932' --'3929' --'3923' --'3904' --'3885' --'3820' --'3894'
WHERE VPL.[Buy-from Vendor No_] = '3929'
AND ([Posting Date] BETWEEN '2016-01-01' AND '2016-12-31')
GROUP BY VPL.[Location Code], V.[Freight-free Limit], V.No_, V.Name --, VPL.[Direct Unit Cost]
ORDER BY V.Name
输出:
3929 AM ASF Fischer BV 4 39 350.00000000000000000000
3929 DB ASF Fischer BV 4 67 350.00000000000000000000
3929 DL ASF Fischer BV 8 128 350.00000000000000000000
3929 RD ASF Fischer BV 4 36 350.00000000000000000000
3929 ZM ASF Fischer BV 1 10 350.00000000000000000000
但是我必须把总和(金额)放在哪里?
答案 0 :(得分:0)
从中删除Vpl.type并使用SUM与CASE ..
select
col1..
,sum(case when Type =1 then amount else 0 end )As type1sum,
sum(case when Type =2 then amount else 0 end )As type2sum
from
table
grouping columns