我有一个查询
public class CartOrder {
List<Data> data;
public CartOrder () {
data = new ArrayList<Data>();
}
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
public void addData(Data data) {
this.data.add(data);
}
class Data {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
给出了如下输出:
SELECT circlename,
alarmname,
count(alarmname) as Count
from temip_alarm
where alarmname='Site Down' or
alarmname='Predicted_Site Down' or
alarmname='Mains Fail' or
alarmname='Fire & Smoke' or
alarmname='Shelter Temperature High'
group by circlename, alarmname;
但我需要这样的数据:
任何人都可以帮我实现这个目标..
答案 0 :(得分:1)
使用case
表达式进行条件聚合:
SELECT circlename,
sum(case when alarmname = 'Site Down' then 1 else 0 end) as SiteDownCount,
...,
count(*) as GrandTotal
from temip_alarm
where alarmname IN ('Site Down', 'Predicted_Site Down', 'Mains Fail',
'Fire & Smoke', 'Shelter Temperature High')
group by circlename
答案 1 :(得分:0)
这为SQL Server
扩展了@jarh
版本
SELECT (case when GROUPING(circlename) = 1
then 'GrandTotal' else circlename end) circlename,
sum(case when alarmname = 'Site Down' then 1 else 0 end) as SiteDownCount,
...,
count(*) as GrandTotal
from temip_alarm
where alarmname IN ('Site Down', 'Predicted_Site Down', 'Mains Fail',
'Fire & Smoke', 'Shelter Temperature High')
group by rollup(circlename)