SQL语句汇总

shsyzl007 2016-06-14 05:27:22
数据表结构
username type num
aaaa 玉米 1212
aaaa 玉米 212
bbb 小麦 2323
bbb ....
只有两种产品 玉米和小麦,玉米价格1.5,小麦价格1.2 求总价值?
...全文
161 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sgyiliya 2016-06-15
  • 打赏
  • 举报
回复
引用 5 楼 sgyiliya 的回复:
推荐2楼,这个简单明了: 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
说错了,推荐3楼。
sgyiliya 2016-06-15
  • 打赏
  • 举报
回复
推荐2楼,这个简单明了: 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
全栈极简 2016-06-14
  • 打赏
  • 举报
回复
with a as(
select type,sum(num) as num from foods group by type),

b as(
select type,(case when type='小麦' then 1.2 when type='玉米' then 1.5 end)*num as price  from a)

select sum(price) as result from b
江南小鱼 2016-06-14
  • 打赏
  • 举报
回复
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
Hello World, 2016-06-14
  • 打赏
  • 举报
回复
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
回首笑浮生 2016-06-14
  • 打赏
  • 举报
回复
WITH t0 AS ( SELECT producttype,SUM(productnum) AS productnum,(CASE WHEN producttype='玉米' THEN 1.5 ELSE 1.2 END ) AS price FROM tbl GROUP BY producttype )SELECT SUM(productnum*price) FROM t0

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧