数据表结构
username type num
aaaa 玉米 1212
aaaa 玉米 212
bbb 小麦 2323
bbb ….
只有两种产品 玉米和小麦,玉米价格1.5,小麦价格1.2 求总价值?
username type num
aaaa 玉米 1212
aaaa 玉米 212
bbb 小麦 2323
bbb ….
只有两种产品 玉米和小麦,玉米价格1.5,小麦价格1.2 求总价值?
解决方案
50
select "a" as name ,100 as number into #temp union select "a",200 union select "b",300 union select "b",100 select * from #temp select sum(case name when "a" then 2 else 5 end * number) as amount from #temp
50
select sum(case username when "aaaa" then num else 0 end)*1.5 + sum(case username when "bbb" then num else 0 end)*1.2 from table