如下一表 table (说明 code ='000' 代表房费 )
name code je
张三 000 100
张三 000 200
张三 001 20
张三 004 30
李四 000 200
李四 003 20
李四 005 10要求得到一个统计结果姓名 房费金额 其他金额 房费次数
张三 300 50 2
李四 200 30 1
请教这个sql 如何写?谢谢
name code je
张三 000 100
张三 000 200
张三 001 20
张三 004 30
李四 000 200
李四 003 20
李四 005 10要求得到一个统计结果姓名 房费金额 其他金额 房费次数
张三 300 50 2
李四 200 30 1
请教这个sql 如何写?谢谢
select name as 姓名, (select sum(je) from tb b where a.name = b.name and code='000') as 房费金额,
(select sum(je) from tb c where a.name = c.name and code<>'000') as 其他金额,
(select count(*) from tb d where a.name = d.name and code='000') as 房费次数
from tb a
declare @T table
(name varchar(4),code varchar(3),je int)
insert into @T
select '张三','000',100 union all
select '张三','000',200 union all
select '张三','001',20 union all
select '张三','004',30 union all
select '李四','000',200 union all
select '李四','003',20 union all
select '李四','005',10select 姓名=name,
房费金额=sum(case when code='000' then je else 0 end),
其他金额=sum(case when code<>'000' then je else 0 end),
房费次数=sum(case when code='000' then 1 else 0 end)
from @T group by name order by 1 desc
/*
姓名 房费金额 其他金额 房费次数
---- ----------- ----------- -----------
张三 300 50 2
李四 200 30 1
*/
insert into tb select '张三','000',100
insert into tb select '张三','000',200
insert into tb select '张三','001',20
insert into tb select '张三','004',30
insert into tb select '李四','000',200
insert into tb select '李四','003',20
insert into tb select '李四','005',10
go
select name,
sum(case when code='000' then je else 0 end)as 房费金额,
sum(case when code!='000' then je else 0 end)as 其他金额,
sum(case when code='000' then 1 else 0 end)as 房费次数
from tb
group by name
/*
name 房费金额 其他金额 房费次数
---------- ----------- ----------- -----------
李四 200 30 1
张三 300 50 2(2 行受影响)*/
go
drop table tb
姓名,
sum(case when code ='000' then je else 0 end ) as 房费金额,
sum(case when code !='000' then je else 0 end ) as 其他金额,
count(1) as 房费次数
group by
姓名
create table tb
(name nvarchar(3),code varchar(3),je int)insert into tb
select '张三','000', 100 union all
select '张三', '000', 200 union all
select '张三', '001', 20 union all
select '张三', '004', 30 union all
select '李四', '000', 200 union all
select '李四', '003', 20 union all
select '李四', '005', 10;with cte as (
select *,rownum=rank() over(partition by name order by code) from tb
)select t1.name,t1.房费金额,t2.其他金额,t1.房费次数 from
(select name,SUM(je) AS 房费金额,COUNT(je) AS 房费次数 from cte where rownum=1 group by name) t1
left join (select name,SUM(je) AS 其他金额 from cte where rownum!=1 group by name) t2 on t1.name=t2.name
name 房费金额 其他金额 房费次数
---- ----------- ----------- -----------
李四 200 30 1
张三 300 50 2(2 行受影响)
姓名,
sum(case when code ='000' then je else 0 end ) as 房费金额,
sum(case when code !='000' then je else 0 end ) as 其他金额,
sum(case when code='000' then 1 else 0 end) as 房费次数
group by
姓名
(
name char(8)
,code char(3)
,je integer
);
insert into atd.test_1024 values
('张三','000',100)
,('张三','000',200)
,('张三','001',20)
,('张三','004',30)
,('李四','000',200)
,('李四','003',20)
,('李四','005',10);select a.name,
sum(case when a.code='000' then a.je else 0 end),
sum(case when a.code<>'000'then a.je else 0 end),
sum(case when a.code='000' then 1 else 0 end)
from atd.test_1024 a
group by a.name;