再求一条sql语句

KOOK_OKKO 2009-10-15 04:27:34
id hierarchy money
1 第一层 10
2 第二层 15
3 第二层 15
4 第二层 18
5 第四层 13
6 第三层 7
7 第三层 8

请问如何得到如下结果
id 第一层个数 第一层总额 第二层个数 第二层总额 第三层个数 第三层总额 第四层个数 第四层总额
1 1 10 3 48 2 15 1 13
...全文
51 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
小宏 2009-10-15
  • 打赏
  • 举报
回复
乌龟哥好厉害
小宏 2009-10-15
  • 打赏
  • 举报
回复

if object_id('a') is not null
drop table a
go
create table a(id int identity(1,1) , hierarchy varchar(20) , money int)
go
insert into a
select'第一层',10 union all
select'第二层',15 union all
select'第二层',15 union all
select'第二层',18 union all
select'第四层',13 union all
select'第三层',7 union all
select'第三层',8
go
declare @sql varchar(8000)

select @sql = isnull(@sql+',' , '') + 'sum(case when hierarchy = ''' + ltrim(hierarchy) + ''' then 1 else 0 end) as [' + ltrim(hierarchy)+ '数量]' +
', sum(case when hierarchy = ''' + ltrim(hierarchy) + ''' then money else '''' end) as [' +ltrim(hierarchy)+'总额]'
from (select distinct hierarchy from a ) t

set @sql = 'select ' + @sql + 'from a '
exec (@sql)

--小F-- 2009-10-15
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-15 16:30:19
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[hierarchy] varchar(6),[money] int)
insert [tb]
select 1,'第一层',10 union all
select 2,'第二层',15 union all
select 3,'第二层',15 union all
select 4,'第二层',18 union all
select 5,'第四层',13 union all
select 6,'第三层',7 union all
select 7,'第三层',8
--------------开始查询--------------------------
select
id=1,
sum(case hierarchy when '第一层' then 1 else 0 end) [第一层个数],
sum(case hierarchy when '第一层' then ltrim([money]) else 0 end) as [第一层总额],
sum(case hierarchy when '第二层' then 1 else 0 end) [第二层个数],
sum(case hierarchy when '第二层' then ltrim([money]) else 0 end) as [第二层总额],
sum(case hierarchy when '第三层' then 1 else 0 end) [第三层个数],
sum(case hierarchy when '第三层' then ltrim([money]) else 0 end) as [第三层总额],
sum(case hierarchy when '第四层' then 1 else 0 end) [第四层个数],
sum(case hierarchy when '第四层' then ltrim([money]) else 0 end) as [第四层总额]
from tb
----------------结果----------------------------
/* id 第一层个数 第一层总额 第二层个数 第二层总额 第三层个数 第三层总额 第四层个数 第四层总额
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1 1 10 3 48 2 15 1 13

(1 行受影响)
*/

dawugui 2009-10-15
  • 打赏
  • 举报
回复
create table tb(id int, hierarchy varchar(10) , [money] int)
insert into tb values(1 , '第一层',10)
insert into tb values(2 , '第二层',15)
insert into tb values(3 , '第二层',15)
insert into tb values(4 , '第二层',18)
insert into tb values(5 , '第四层',13 )
insert into tb values(6 , '第三层',7)
insert into tb values(7 , '第三层',8)
go

--如果 hierarchy固定。
select id,
sum(case hierarchy when '第一层' then cnt else 0 end) [第一层个数],
sum(case hierarchy when '第一层' then [money] else 0 end) [第一层总额],
sum(case hierarchy when '第二层' then cnt else 0 end) [第二层个数],
sum(case hierarchy when '第二层' then [money] else 0 end) [第二层总额],
sum(case hierarchy when '第三层' then cnt else 0 end) [第三层个数],
sum(case hierarchy when '第三层' then [money] else 0 end) [第三层总额],
sum(case hierarchy when '第四层' then cnt else 0 end) [第四层个数],
sum(case hierarchy when '第四层' then [money] else 0 end) [第四层总额]
from
(
select (select min(id) from tb) id, hierarchy , count(1) cnt , sum([money]) [money] from tb group by hierarchy
) t
group by id

--如果 hierarchy不固定。
declare @sql varchar(8000)
set @sql = 'select id '
select @sql = @sql + ' , sum(case hierarchy when ''' + hierarchy + ''' then cnt else 0 end) [' + hierarchy + '个数]'
+ ' , sum(case hierarchy when ''' + hierarchy + ''' then [money] else 0 end) [' + hierarchy + '总额]'
from (select distinct hierarchy from tb) as a
set @sql = @sql + ' from (select (select min(id) from tb) id, hierarchy , count(1) cnt , sum([money]) [money] from tb group by hierarchy) m group by id'
exec(@sql)

drop table tb

/*
id 第一层个数 第一层总额 第二层个数 第二层总额 第三层个数 第三层总额 第四层个数 第四层总额
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1 1 10 3 48 2 15 1 13

(所影响的行数为 1 行)
*/
华夏小卒 2009-10-15
  • 打赏
  • 举报
回复

if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[hierarchy] varchar(6),money int)
insert [tb]
select 1,'第一层' ,10union all
select 2,'第二层' ,15union all
select 3,'第二层' ,15union all
select 4,'第二层' ,18union all
select 5,'第四层' ,13union all
select 6,'第三层' ,7union all
select 7,'第三层',8
--------------开始查询--------------------------
select
id=1,
sum(case hierarchy when '第一层' then 1 else 0 end) [第一层个数],
sum(case hierarchy when '第一层' then money else 0 end) [第一层总额],
sum(case hierarchy when '第二层' then 1 else 0 end) [第二层个数],
sum(case hierarchy when '第二层' then money else 0 end) [第二层总额],
sum(case hierarchy when '第三层' then 1 else 0 end) [第三层个数],
sum(case hierarchy when '第三层' then money else 0 end) [第三层总额],
sum(case hierarchy when '第四层' then 1 else 0 end) [第四层个数],
sum(case hierarchy when '第四层' then money else 0 end) [第四层总额]
from tb


id 第一层个数 第一层总额 第二层个数 第二层总额 第三层个数 第三层总额 第四层个数 第四层总额
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1 1 10 3 48 2 15 1 13

(1 行受影响)


34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧