select 种类, sum(case when 级别='1级' then 1 else 0 end) [1级记录数], sum(case when 级别='2级' then 1 else 0 end) [2级记录数], sum(case when 级别='1级' then 数量 else 0 end) [1级数量], sum(case when 级别='2级' then 数量 else 0 end) [2级数量] from A group by 种类
select 种类,sum(case 级别 when '1级' then 1 else 0 end)1级记录数 , sum(case 级别 when '2级' then 1 else 0 end)2级记录数, max(case 级别 when '1级' then isnull(数量,0) end)1级数量, max(case 级别 when '2级' then isnull(数量,0) end)2级数量 from a group by 种类 order by 种类 asc
select 种类,sum(case 级别 when '1级' then 1 else 0 end) , sum(case 级别 when '2级' then 1 else 0 end), isnull(sum(case 级别 when '1级' then 数量 end),0), isnull(sum(case 级别 when '2级' then 数量 end),0) from a group by 种类 order by 种类 asc --这么快的速度 让我情何以堪
select 种类, max(case when 级别='1级' then 1 else 0 end) [1级记录数], max(case when 级别='2级' then 1 else 0 end) [2级记录数], sum(case when 级别='1级' then 数量 else 0 end) [1级数量], sum(case when 级别='2级' then 数量 else 0 end) [2级数量] from A group by 种类 这样?
SQL语句统计一个表的记录条数,有两个字段都相同时只算一条记录 例如:记录包含a,b,c,d,e五个字段,当 c都等于某个字符 并且 a相同时,统计只算一条记录,否则才算多条记录 : 表A: id a b c d e1 1 2 3 4 5 2 1 2 6 7 8 3 6 7 8 4 5 4 6 7 6 7 5 5 6 7 8 9 5这时正确统计结果应该是: a值 c值 条数 1 3 1 1 6 1 6 6 1 6 8 2 select a,sum(case when c='8' then 1 else 0 end) from A group by a
这不就是你上面说的希望得到的结果么: create table tb(种类 nvarchar(10),级别 nvarchar(10),数量 int) insert into tb select '一年级','2级',3 insert into tb select '二年级','1级',8 insert into tb select '一年级','1级',5 insert into tb select '一年级','2级',9 go select 种类, sum(case when 级别='1级' then 1 else 0 end) [1级记录数], sum(case when 级别='2级' then 1 else 0 end) [2级记录数], sum(case when 级别='1级' then 数量 else 0 end) [1级数量], sum(case when 级别='2级' then 数量 else 0 end) [2级数量] from tb group by 种类 /* 种类 1级记录数 2级记录数 1级数量 2级数量 ---------- ----------- ----------- ----------- ----------- 二年级 1 0 8 0 一年级 1 2 5 12(2 行受影响) */ go drop table tb
--那就试试子查询吧!select 种类, (select count(distinct 数量) from a where 种类=t.种类 and 级别='1级') [1级记录数], (select count(distinct 数量) from a where 种类=t.种类 and 级别='2级') [2级记录数], sum(case when 级别='1级' then 数量 else 0 end) [1级数量], sum(case when 级别='2级' then 数量 else 0 end) [2级数量] from A t group by 种类
AcHerat: 为什么用这个,count(distinct 数量) 好像这样不对吧
对统计结果再查询select [种类],case when [1级记录数]>0 then 1 else 0 end [1级记录数], case when [2级记录数]>0 then 1 else 0 end [2级记录数], [1级数量],[2级数量] (select 种类, sum(case when 级别='1级' then 1 else 0 end) [1级记录数], sum(case when 级别='2级' then 1 else 0 end) [2级记录数], sum(case when 级别='1级' then 数量 else 0 end) [1级数量], sum(case when 级别='2级' then 数量 else 0 end) [2级数量] from A group by 种类) a
cxmcxm: 你写的语法对吗?
select distinct 种类, (select count(1) from A where t.种类=种类 and A.级别='1级') as [1级记录数], (select count(1) from A where t.种类=种类 and A.级别='2级') as [2级记录数], (select sum(数量) from A where t.种类=种类 and A.级别='1级') as [1级数量], (select sum(数量) from A where t.种类=种类 and A.级别='2级') as [2级数量] from A as t
种类,
sum(case when 级别='1级' then 1 else 0 end) [1级记录数],
sum(case when 级别='2级' then 1 else 0 end) [2级记录数],
sum(case when 级别='1级' then 数量 else 0 end) [1级数量],
sum(case when 级别='2级' then 数量 else 0 end) [2级数量]
from
A
group by
种类
select 种类,sum(case 级别 when '1级' then 1 else 0 end)1级记录数 ,
sum(case 级别 when '2级' then 1 else 0 end)2级记录数,
max(case 级别 when '1级' then isnull(数量,0) end)1级数量,
max(case 级别 when '2级' then isnull(数量,0) end)2级数量
from a
group by 种类 order by 种类 asc
sum(case 级别 when '2级' then 1 else 0 end),
isnull(sum(case 级别 when '1级' then 数量 end),0),
isnull(sum(case 级别 when '2级' then 数量 end),0)
from a
group by 种类 order by 种类 asc
--这么快的速度 让我情何以堪
select
种类,
max(case when 级别='1级' then 1 else 0 end) [1级记录数],
max(case when 级别='2级' then 1 else 0 end) [2级记录数],
sum(case when 级别='1级' then 数量 else 0 end) [1级数量],
sum(case when 级别='2级' then 数量 else 0 end) [2级数量]
from
A
group by
种类
这样?
例如:记录包含a,b,c,d,e五个字段,当 c都等于某个字符 并且 a相同时,统计只算一条记录,否则才算多条记录 :
表A:
id a b c d e1 1 2 3 4 5
2 1 2 6 7 8
3 6 7 8 4 5
4 6 7 6 7 5
5 6 7 8 9 5这时正确统计结果应该是:
a值 c值 条数
1 3 1
1 6 1
6 6 1
6 8 2
select a,sum(case when c='8' then 1 else 0 end) from A group by a
create table tb(种类 nvarchar(10),级别 nvarchar(10),数量 int)
insert into tb select '一年级','2级',3
insert into tb select '二年级','1级',8
insert into tb select '一年级','1级',5
insert into tb select '一年级','2级',9
go
select
种类,
sum(case when 级别='1级' then 1 else 0 end) [1级记录数],
sum(case when 级别='2级' then 1 else 0 end) [2级记录数],
sum(case when 级别='1级' then 数量 else 0 end) [1级数量],
sum(case when 级别='2级' then 数量 else 0 end) [2级数量]
from
tb
group by 种类
/*
种类 1级记录数 2级记录数 1级数量 2级数量
---------- ----------- ----------- ----------- -----------
二年级 1 0 8 0
一年级 1 2 5 12(2 行受影响)
*/
go
drop table tb
--那就试试子查询吧!select
种类,
(select count(distinct 数量) from a where 种类=t.种类 and 级别='1级') [1级记录数],
(select count(distinct 数量) from a where 种类=t.种类 and 级别='2级') [2级记录数],
sum(case when 级别='1级' then 数量 else 0 end) [1级数量],
sum(case when 级别='2级' then 数量 else 0 end) [2级数量]
from
A t
group by
种类
为什么用这个,count(distinct 数量)
好像这样不对吧
case when [2级记录数]>0 then 1 else 0 end [2级记录数],
[1级数量],[2级数量]
(select
种类,
sum(case when 级别='1级' then 1 else 0 end) [1级记录数],
sum(case when 级别='2级' then 1 else 0 end) [2级记录数],
sum(case when 级别='1级' then 数量 else 0 end) [1级数量],
sum(case when 级别='2级' then 数量 else 0 end) [2级数量]
from
A
group by
种类) a
你写的语法对吗?
select distinct 种类,
(select count(1) from A where t.种类=种类 and A.级别='1级') as [1级记录数],
(select count(1) from A where t.种类=种类 and A.级别='2级') as [2级记录数],
(select sum(数量) from A where t.种类=种类 and A.级别='1级') as [1级数量],
(select sum(数量) from A where t.种类=种类 and A.级别='2级') as [2级数量]
from A as t