2 wayyun wayyun 于 2014.06.18 14:50 提问

sql语句问题求大神帮忙帮忙

select count(*) from (SELECT HY_GUID,COUNT(*) number FROM dbo.BI_GYDD GROUP BY HY_GUID) a where number=1
select count(*) from (SELECT HY_GUID,COUNT(*) number FROM dbo.BI_GYDD GROUP BY HY_GUID) a where number=2
select count(*) from (SELECT HY_GUID,COUNT(*) number FROM dbo.BI_GYDD GROUP BY HY_GUID) a where number=3
select count(*) from (SELECT HY_GUID,COUNT(*) number FROM dbo.BI_GYDD GROUP BY HY_GUID) a where number=4
select count(*) from (SELECT HY_GUID,COUNT(*) number FROM dbo.BI_GYDD GROUP BY HY_GUID) a where number>=5
如何把这5条sql语句变成一条,我要的结果是 =1=2=3=4大于5的用户分别说多少,不是要总数的亲们

2个回答

feiyun0112
feiyun0112   Rxr 2014.06.18 15:02

select
A=(select count(*) from (SELECT HY_GUID,COUNT(*) number FROM dbo.BI_GYDD GROUP BY HY_GUID) a where number=1)
,B=(select count(*) from (SELECT HY_GUID,COUNT(*) number FROM dbo.BI_GYDD GROUP BY HY_GUID) a where number=2)
,C=(select count(*) from (SELECT HY_GUID,COUNT(*) number FROM dbo.BI_GYDD GROUP BY HY_GUID) a where number=3)
,D=(select count(*) from (SELECT HY_GUID,COUNT(*) number FROM dbo.BI_GYDD GROUP BY HY_GUID) a where number=4)
,E=(select count(*) from (SELECT HY_GUID,COUNT(*) number FROM dbo.BI_GYDD GROUP BY HY_GUID) a where number>=5)

chbhhz
chbhhz   2014.06.26 16:46

用你的表不好测,我换成我库里的表了
select sum(t21), sum(t22)
from (select decode(t.answer_id, 361, 1, 0) t21,
decode(t.answer_id, 363, 1,0) t22
from tb_satisfy_answer t
group by t.answer_id) t2;

思路:吧满足条件的标1,不满足条件的标2 在做sum,就是你要的结果。
楼上的可以满足要求,但是效率......

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!