求一个查询语句 急!

NIRVANA_HUI 2006-11-09 10:31:48
单位 类型 名称 所属
北京 新 茶馆 首都
北京 重 红楼 首都
天津 租 西游 地方
北京 新 西游 首都
河北 重 林家 地方
河南 新 雷雨 地方
天津 租 红楼 地方
天津 新 水浒 地方

以上的表的数据还有很多,我想得到一个汇总的结果是:
合计 新 重 租
总计(首都+地方) 8 4 2 2
1、首都 3 2 1 0
北京 3 2 1 0
2、地方 5 2 1 2
天津 3 1 0 2
河北 1 0 1 0
河南 1 1 0 0

这样的SQL怎么写 就一条语句出来 谢谢 各位高手!!!
...全文
580 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
soltek 2006-11-10
  • 打赏
  • 举报
回复
Mark,牛人
guojh021 2006-11-10
  • 打赏
  • 举报
回复
sql2005可以解决吧。
yanidealsay521 2006-11-10
  • 打赏
  • 举报
回复
原来火星上也有中国.mark
tigerwithwing 2006-11-10
  • 打赏
  • 举报
回复
原来地球上不只是有中国
luckyprg 2006-11-10
  • 打赏
  • 举报
回复
mark.
lsd1025 2006-11-10
  • 打赏
  • 举报
回复
学习了,up
louifox 2006-11-09
  • 打赏
  • 举报
回复
declare @t table(单位 varchar(4),类型 varchar(4),名称 varchar(4),所属 varchar(4))
insert into @t select '北京','新','茶馆','首都'
insert into @t select '北京','重','红楼','首都'
insert into @t select '天津','租','西游','地方'
insert into @t select '北京','新','西游','首都'
insert into @t select '河北','重','林家','地方'
insert into @t select '河南','新','雷雨','地方'
insert into @t select '天津','租','红楼','地方'
insert into @t select '天津','新','水浒','地方'

select 单位=case when (grouping(单位)=1 and grouping(所属) =0) then 所属 when (grouping(单位)=1 and grouping(所属) =1) then '总计(首都+地方) 'else 单位 end,合计=count(名称),
新=sum(case 类型 when '新' then 1 else 0 end),
重=sum(case 类型 when '重' then 1 else 0 end),
租=sum(case 类型 when '租' then 1 else 0 end)
from @t
group by 所属,单位 with rollup
order by grouping(所属)desc,所属 desc,grouping(单位)desc

单位 合计 新 重 租
---------------- ----------- ----------- ----------- -----------
总计(首都+地方) 8 4 2 2
首都 3 2 1 0
北京 3 2 1 0
地方 5 2 1 2
河北 1 0 1 0
河南 1 1 0 0
天津 3 1 0 2

(所影响的行数为 7 行)
子陌红尘 2006-11-09
  • 打赏
  • 举报
回复
select
[单位/内容] = ISNULL(单位, '总计'),

[合计] = SUM(1),
[新出] = SUM(CASE 类型 WHEN '新出' THEN 1 ELSE 0 END),
[重印] = SUM(CASE 类型 WHEN '重印' THEN 1 ELSE 0 END),
[租型] = sum(case 类型 when '租型' then 1 else 0 end)
FROM 表名
GROUP BY 单位 WITH ROLLUP

在这个基础上我不知道怎么写了, 怎么效率高怎么实现,我想让总计显示在第一列,并且有一个首都和地方的分组。
-------------------------------------------------------------------------------------

修改一下程序中对结果输出的顺序处理,从结果集的尾部向结果集首记录反序输出即可。
dawugui 2006-11-09
  • 打赏
  • 举报
回复
我上面不是给你写好了?
除了用union(all),没其他办法.
NIRVANA_HUI 2006-11-09
  • 打赏
  • 举报
回复
select
[单位/内容] = ISNULL(单位, '总计'),

[合计] = SUM(1),
[新出] = SUM(CASE 类型 WHEN '新出' THEN 1 ELSE 0 END),
[重印] = SUM(CASE 类型 WHEN '重印' THEN 1 ELSE 0 END),
[租型] = sum(case 类型 when '租型' then 1 else 0 end)
FROM 表名
GROUP BY 单位 WITH ROLLUP

在这个基础上我不知道怎么写了, 怎么效率高怎么实现,我想让总计显示在第一列,并且有一个首都和地方的分组。
NIRVANA_HUI 2006-11-09
  • 打赏
  • 举报
回复
select
[单位/内容] = ISNULL(单位, '总计'),

[合计] = SUM(1),
[新出] = SUM(CASE 类型 WHEN '新出' THEN 1 ELSE 0 END),
[重印] = SUM(CASE 类型 WHEN '重印' THEN 1 ELSE 0 END),
[租型] = sum(case 类型 when '租型' then 1 else 0 end)
FROM 表名
GROUP BY 单位 WITH ROLLUP
NIRVANA_HUI 2006-11-09
  • 打赏
  • 举报
回复
select
[单位/内容] = ISNULL(单位, '总计'),

[合计] = SUM(1),
[新出] = SUM(CASE 类型 WHEN '新出' THEN 1 ELSE 0 END),
[重印] = SUM(CASE 类型 WHEN '重印' THEN 1 ELSE 0 END),
[租型] = sum(case 类型 when '租型' then 1 else 0 end)
FROM BOOK
GROUP BY 单位 WITH ROLLUP
zicxc 2006-11-09
  • 打赏
  • 举报
回复
NIRVANA_HUI() ( ) 信誉:100 Blog 2006-11-09 10:56:00 得分: 0


还有需要一个首都和地方的分组


-------------------------------
?
看不到吗
dawugui 2006-11-09
  • 打赏
  • 举报
回复
declare @t table(单位 varchar(4),类型 varchar(4),名称 varchar(4),所属 varchar(4))
insert into @t select '北京','新','茶馆','首都'
insert into @t select '北京','重','红楼','首都'
insert into @t select '天津','租','西游','地方'
insert into @t select '北京','新','西游','首都'
insert into @t select '河北','重','林家','地方'
insert into @t select '河南','新','雷雨','地方'
insert into @t select '天津','租','红楼','地方'
insert into @t select '天津','新','水浒','地方'


select 单位 = '总计' , sum(合计) as 合计 , sum(新) as 新 , sum(重) as 重 , sum(租) as 租 from
(
select
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from @t group by 所属
) t1

union all

select 单位 = '1、首都' , sum(合计) as 合计 , sum(新) as 新 , sum(重) as 重 , sum(租) as 租 from
(
select
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from @t where 所属 = '首都' group by 所属
) t2

union all

select 单位 = '北京' , sum(合计) as 合计 , sum(新) as 新 , sum(重) as 重 , sum(租) as 租 from
(
select
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from @t where 所属 = '首都' group by 所属
) t3

union all

select 单位 = '2、地方' , sum(合计) as 合计 , sum(新) as 新 , sum(重) as 重 , sum(租) as 租 from
(
select
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from @t where 所属 = '地方' group by 所属
) t2

union all

select
单位,
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from @t where 所属 <> '首都' group by 所属,单位

单位 合计 新 重 租
------- ----------- ----------- ----------- -----------
总计 8 4 2 2
1、首都 3 2 1 0
北京 3 2 1 0
2、地方 5 2 1 2
河北 1 0 1 0
河南 1 1 0 0
天津 3 1 0 2

(所影响的行数为 7 行)
dawugui 2006-11-09
  • 打赏
  • 举报
回复
declare @t table(单位 varchar(4),类型 varchar(4),名称 varchar(4),所属 varchar(4))
insert into @t select '北京','新','茶馆','首都'
insert into @t select '北京','重','红楼','首都'
insert into @t select '天津','租','西游','地方'
insert into @t select '北京','新','西游','首都'
insert into @t select '河北','重','林家','地方'
insert into @t select '河南','新','雷雨','地方'
insert into @t select '天津','租','红楼','地方'
insert into @t select '天津','新','水浒','地方'


select 单位 = '总计' , sum(合计) as 合计 , sum(新) as 新 , sum(重) as 重 , sum(租) as 租 from
(
select
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from @t group by 所属
) t1

union all

select 单位 = '首都' , sum(合计) as 合计 , sum(新) as 新 , sum(重) as 重 , sum(租) as 租 from
(
select
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from @t where 所属 = '首都' group by 所属
) t2

union all

select 单位 = '北京' , sum(合计) as 合计 , sum(新) as 新 , sum(重) as 重 , sum(租) as 租 from
(
select
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from @t where 所属 = '首都' group by 所属
) t3

union all

select 单位 = '地方' , sum(合计) as 合计 , sum(新) as 新 , sum(重) as 重 , sum(租) as 租 from
(
select
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from @t where 所属 = '地方' group by 所属
) t2

union all

select
单位,
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from @t where 所属 <> '首都' group by 所属,单位


单位 合计 新 重 租
---- ----------- ----------- ----------- -----------
总计 8 4 2 2
首都 3 2 1 0
北京 3 2 1 0
地方 5 2 1 2
河北 1 0 1 0
河南 1 1 0 0
天津 3 1 0 2

(所影响的行数为 7 行)
NIRVANA_HUI 2006-11-09
  • 打赏
  • 举报
回复
还有需要一个首都和地方的分组
zicxc 2006-11-09
  • 打赏
  • 举报
回复
修正:

declare @t table(单位 nvarchar(4),类型 nvarchar(4),名称 nvarchar(4),所属 nvarchar(4))
insert into @t select N'北京',N'新',N'茶馆',N'首都'
insert into @t select N'北京',N'重',N'红楼',N'首都'
insert into @t select N'天津',N'租',N'西游',N'地方'
insert into @t select N'北京',N'新',N'西游',N'首都'
insert into @t select N'河北',N'重',N'林家',N'地方'
insert into @t select N'河南',N'新',N'雷雨',N'地方'
insert into @t select N'天津',N'租',N'红楼',N'地方'
insert into @t select N'天津',N'新',N'水浒',N'地方'

select 项目,合计,新,重,租
from (
select N'总计' as 项目,
sum(1) as 合计,
sum(case when 类型=N'新' then 1 else 0 end) as 新,
sum(case when 类型=N'重' then 1 else 0 end) as 重,
sum(case when 类型=N'租' then 1 else 0 end) as 租,
min(所属) as 所属,
1 as ordertype
from @T
union all
select 所属 as 项目,
sum(1) as 合计,
sum(case when 类型=N'新' then 1 else 0 end) as 新,
sum(case when 类型=N'重' then 1 else 0 end) as 重,
sum(case when 类型=N'租' then 1 else 0 end) as 租,
所属,
2 as ordertype
from @T
group by 所属
union all
select 单位 as 项目,
sum(1) as 合计,
sum(case when 类型=N'新' then 1 else 0 end) as 新,
sum(case when 类型=N'重' then 1 else 0 end) as 重,
sum(case when 类型=N'租' then 1 else 0 end) as 租,
所属,
3 as ordertype
from @T
group by 单位,所属
) as t
order by 所属,ordertype,项目


--结果
项目 合计 新 重 租
---- ----------- ----------- ----------- -----------
总计 8 4 2 2
地方 5 2 1 2
天津 3 1 0 2
河北 1 0 1 0
河南 1 1 0 0
首都 3 2 1 0
北京 3 2 1 0

(所影响的行数为 7 行)


NIRVANA_HUI 2006-11-09
  • 打赏
  • 举报
回复
老大们 我想让总计显示在第一列
zicxc 2006-11-09
  • 打赏
  • 举报
回复
select 项目,合计,新,重,租
from (
select '总计' as 项目,
sum(1) as 合计,
sum(case when '类型'='新' then 1 else 0 end) as 新,
sum(case when '类型'='重' then 1 else 0 end) as 重,
sum(case when '类型'='租' then 1 else 0 end) as 租,
min(单位) as 单位,
1 as ordertype
from tablename
union all
select 所属 as 项目,
sum(1) as 合计,
sum(case when '类型'='新' then 1 else 0 end) as 新,
sum(case when '类型'='重' then 1 else 0 end) as 重,
sum(case when '类型'='租' then 1 else 0 end) as 租,
min(单位) as 单位,
2 as ordertype
from tablename
group by 所属
union all
select 单位 as 项目,
sum(1) as 合计,
sum(case when '类型'='新' then 1 else 0 end) as 新,
sum(case when '类型'='重' then 1 else 0 end) as 重,
sum(case when '类型'='租' then 1 else 0 end) as 租,
单位,
3 as ordertype
from tablename
group by 单位
) as t
order by 单位,ordertype
allright_flash 2006-11-09
  • 打赏
  • 举报
回复
(select
isnull(单位) as 单位,
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from

group by
所属,单位 with rollup
having
grouping(所属)=0
)
union all
(
(select
isnull(所属) as 单位,
count(*) as 合计,
sum(case 类型 when '新' then 1 else 0 end) as 新,
sum(case 类型 when '重' then 1 else 0 end) as 重,
sum(case 类型 when '租' then 1 else 0 end) as 租
from

group by
所属,单位 with rollup

)
)
加载更多回复(4)

34,590

社区成员

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

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