您的位置:首页 > 博客中心 > 数据库 >

sql server 点滴累计

时间:2022-03-14 04:03

1.选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中

答:即指定 GROUP BY 时,选择列表中任一非聚合表达式内的所有列都应包含在 GROUP BY 列表中,或者 GROUP BY表达式必须与选择列表表达式完全匹配。

2. case when then else end 语法的使用

select
i.index_name,i.index_def,
  sum(case when w.years= 2014 then w.actual_value else 0 end) as a1,
  sum(case when w.years= 2014 then w.ranking else 0 end ) as r1,

  sum(case when w.years= (2014-1) then w.actual_value else 0 end ) as a2,
  sum(case when w.years= (2014-1) then w.ranking else 0 end ) as r2,


from bm_work w,bm_index i
where
w.indexdef_id=i.uuid and
w.ent_id=‘2c90e4da49514c750149515eb56f0003‘ and w.summary_state=2
group by i.index_name,i.index_def;

这个语句达到这样一个效果



热门排行

今日推荐

热门手游