我正在使用SQL语言。我想结合查询的结果。 这是我的代码和预期输出。
create table customer(name varchar,dpt varchar);
insert into customer (name,dpt)VALUES ('albe','cse');
insert into customer (name,dpt)VALUES ('bine','cse');
insert into customer (name,dpt)VALUES ('alfred','ece');
insert into customer (name,dpt)VALUES ('booshan','ece');
insert into customer (name,dpt)VALUES ('antony','eee');
insert into customer (name,dpt)VALUES ('job','ece');
查询1 :
select
dpt,
count(name)
from customer
where dpt='ece' or dpt='cse'
group by dpt;
结果:
| dpt | count |
|-----|-------|
| cse | 2 |
| ece | 3 |
我需要结果为:
| cse/ece | 5 |
怎么可能? sqlfiddle在这里:
答案 0 :(得分:5)
好像你正在使用PostgreSQL(可以从sqlfiddle链接中推断出来。)
如果是这种情况,那么您可以在外部查询中对查询应用array_agg
和sum
:
SELECT array_agg(dpt), sum(cnt)
FROM (
SELECT dpt, count(name) as cnt
FROM customer
WHERE dpt='ece' OR dpt='cse'
GROUP BY dpt) t;
答案 1 :(得分:1)
您也可以使用GROUP_CONCAT
SELECT GROUP_CONCAT(dpt), SUM(cnt)
FROM(select
dpt,
count(name) cnt
from customer
where dpt='ece' or dpt='cse'
group by dpt) t;