我正在尝试连接多个表,需要在同一行返回匹配信息。
我有3种可能的价格
这些是“分层”定价,所以我需要处理数量。
表格可能包含不匹配的记录。例如,标准定价可能具有数量1,100,200,500定价,而网站定价可能只有数量1和25定价,而客户定价可能具有数量1,100,200,这就是全部。
所以定价表如下:
PriceID, Quantity, Price, WebsiteID, CustomerGroupID
10 1 10 NULL NULL
11 1 9 1 NULL
12 1 8 1 5
13 10 9 NULL NULL
14 20 8 NULL NULL
15 8 7 1 5
我希望看到的是:
Quantity, sPrice, wPrice, cPrice
1 10 9 8
8 NULL NULL 7
10 9 NULL NULL
20 8 NULL NULL
有了我的话,我可以使用CASE语句来处理定价的价格和其他功能 - 但我似乎无法弄清楚将它们组合在一起的方法。
答案 0 :(得分:2)
这应该符合您的需求:
select
quantity,
sum(case when websiteid is null and customergroupid is null
then price
end) as sprice,
sum(case when websiteid is not null and customergroupid is null
then price
end) as wprice,
sum(case when customergroupid is not null
then price
end) as cprice
from
t
group by
quantity
SQLFiddle here