group by生成综合报表的sql问题(200分答谢,明天再加100分)!

DiligencyMan 2009-01-05 11:10:21
我有订单表有如下字段,hotid,hotclasstype1,hotclasstype2,hotcalsstype3,postuserid,posttime,......(等等字段就不举例子了,很多字段)

我想生成订单大类-->订单中类-->订单小类 这样的有层次的统计效果,hotclasstype1,hotclasstype2,hotcalsstype3这个字段的关系是这样的:

大类包括种类1:N,然后种类里面包括小类1:N,三个字段的关系在另外一张辅助表helptype(id,pid,name)里面比如大类1的id为1,那么种类1的id为2(其中pid为1),小类1的id为3(pid为2),这样依次存放数据。


现在需要,做一个报表-->订单3个类型的订单数量统计,而要生成的报表统计显示数据如下:

序号 类别 数量
1 大类1 100
2 中类1 50
3 小类1 10
4 大类2 200
5 中类2 100
6 小类2 50
7 …… 200
合计 710


我用普通的如下sql语句:
SELECT CLASSID,SUBCLASSID,COMPANY_ID,SUM(CLASSID),SUM(SUBCLASSID),COUNT(COMPANY_ID) 
FROM HOT_ORDER
GROUP BY CLASSID,SUBCLASSID,COMPANY_ID
ORDER BY CLASSID,SUBCLASSID,COMPANY_ID


是搞不定这样的需求的,问下各位达人,有好招没有啊?我是用ireport报表工具来做的,所以先要求写好sql,然后再做其他的,第一步sql还没有搞定呢!
...全文
193 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
MJunnnn 2009-01-05
  • 打赏
  • 举报
回复
乌龟怎么还不来,事实又一次证明乌龟走得太慢了,^_^
viva369 2009-01-05
  • 打赏
  • 举报
回复
With Rollup
-狙击手- 2009-01-05
  • 打赏
  • 举报
回复
------------------------------------------------------------------------
-- Author: HappyFlyStone
-- Date : 2009-01-05 11:21:57
-- Ver: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86)
-- Apr 14 2006 01:12:25
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Standard Edition on Windows NT 5.0 (Build 2195: Service Pack 4)
--
------------------------------------------------------------------------

-- Test Data: B_XSJL
IF OBJECT_ID('B_XSJL') IS NOT NULL
DROP TABLE B_XSJL
Go
CREATE TABLE B_XSJL(hotid INT,hotclasstype1 INT,hotclasstype2 INT,hotcalsstype3 INT,postuserid INT)
Go
INSERT INTO B_XSJL
SELECT 1,1,1,1,1 UNION ALL
SELECT 2,1,1,2,1 UNION ALL
SELECT 3,1,1,3,1 UNION ALL
SELECT 4,1,2,1,1 UNION ALL
SELECT 5,1,2,2,1 UNION ALL
SELECT 6,2,1,1,1 UNION ALL
SELECT 7,2,1,3,1
GO
--Start
SELECT
isnull(ltrim(hotclasstype1),'大类1') as hotclasstype1,
isnull(ltrim(hotclasstype2),'大类2') as hotclasstype2,
isnull(ltrim(hotcalsstype3),'大类3') as hotclasstype3,
count(1) as [sum]
FROM
B_XSJL
group by
hotclasstype1,hotclasstype2,hotcalsstype3
with rollup
having grouping(hotcalsstype3) = 1

--Result:
/*
hotclasstype1 hotclasstype2 hotclasstype3 sum
------------- ------------- ------------- -----------
1 1 大类3 3
1 2 大类3 2
1 大类2 大类3 5
2 1 大类3 2
2 大类2 大类3 2
大类1 大类2 大类3 7

(6 行受影响)
*/
--End
dobear_0922 2009-01-05
  • 打赏
  • 举报
回复
顶大乌龟
DiligencyMan 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 happyflystone 的回复:]
是2005还是2000 ?
[/Quote]

是sql2000,如何实现这样的功能?
oraclelogan 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 happyflystone 的回复:]
是2005还是2000 ?
[/Quote]

学习了,sql2000如何写,sql2005如何写?
子陌红尘 2009-01-05
  • 打赏
  • 举报
回复
try:

SELECT CLASSID,SUBCLASSID,COMPANY_ID,SUM(CLASSID),SUM(SUBCLASSID),COUNT(COMPANY_ID)
FROM HOT_ORDER
GROUP BY CLASSID,SUBCLASSID,COMPANY_ID With Rollup
ORDER BY CLASSID,SUBCLASSID,COMPANY_ID
-狙击手- 2009-01-05
  • 打赏
  • 举报
回复
是2005还是2000 ?
-狙击手- 2009-01-05
  • 打赏
  • 举报
回复
我也接点分
DiligencyMan 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liangCK 的回复:]
我可以接乌龟50分吗?
[/Quote]

可以啊,解决了,给你111分,如何写这样的sql啊!
水族杰纶 2009-01-05
  • 打赏
  • 举报
回复
等dawugui
幫頂~~
DiligencyMan 2009-01-05
  • 打赏
  • 举报
回复
等待乌龟大哥的到来!
liangCK 2009-01-05
  • 打赏
  • 举报
回复
我可以接乌龟50分吗?
DiligencyMan 2009-01-05
  • 打赏
  • 举报
回复
我明天加了100分再结贴吧!
DiligencyMan 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 dawugui 的回复:]
SQL codeCREATE TABLE tb(hotid int,hotclasstype1 varchar(10),hotclasstype2 varchar(10),hotclasstype3 varchar(10),postuserid INT)
Go
INSERT INTO tb
SELECT 1,'大类1','中类1','小类1',1 UNION ALL
SELECT 2,'大类1','中类1','小类2',1 UNION ALL
SELECT 3,'大类1','中类1','小类3',1 UNION ALL
SELECT 4,'大类1','中类2','小类1',1 UNION ALL
SELECT 5,'大类1','中类2','小类2',1 UNION ALL
SELECT 6,'大类2','中…
[/Quote]

大乌龟哥哥,你的也能达到我的要求哦,你的第一种写法跟oraclelogan的是一种思路,第二种写法很好,比较适合我的思路啊!谢谢了
DiligencyMan 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 oraclelogan 的回复:]
大乌龟还没有回复啊,呵呵!

从网上找到了一个前辈的实例,整理了下,希望对楼主有所帮助!



SQL codeSELECT B.NAME_COUNT,B.NUM
FROM (
SELECT
--统计名称 大类-->中类-->小类
CASE WHEN (A.HOTCLASSTYPE1 IS NOT NULL AND A.HOTCLASSTYPE2 IS NOT NULL AND A.HOTCLASSTYPE3 IS NOT NULL) THEN ' '+CAST(A.HOTCLASSTYPE3 AS VARCHAR(120))
WHEN (A.HOTCLASSTYPE1 IS NOT N…
[/Quote]

谢谢,你的可以达到我的要求,不过为啥你的sql全部是大写啊,一点也不容易读啊,我还是习惯小写的sql写法!
dawugui 2009-01-05
  • 打赏
  • 举报
回复
CREATE TABLE tb(hotid int,hotclasstype1 varchar(10),hotclasstype2 varchar(10),hotclasstype3 varchar(10),postuserid INT)
Go
INSERT INTO tb
SELECT 1,'大类1','中类1','小类1',1 UNION ALL
SELECT 2,'大类1','中类1','小类2',1 UNION ALL
SELECT 3,'大类1','中类1','小类3',1 UNION ALL
SELECT 4,'大类1','中类2','小类1',1 UNION ALL
SELECT 5,'大类1','中类2','小类2',1 UNION ALL
SELECT 6,'大类2','中类1','小类1',1 UNION ALL
SELECT 7,'大类2','中类2','小类2',1
GO

--方法一,使用with rollup
select case when hotclasstype3 = '' and hotclasstype2 = '' then hotclasstype1
when hotclasstype3 = '' and hotclasstype2 <> '' then ' ' + hotclasstype2
when hotclasstype3 <> '' then ' ' + hotclasstype3
end 类别, 数量 from
(
select isnull(hotclasstype1,'合计') hotclasstype1,
isnull(hotclasstype2,'') hotclasstype2 ,
isnull(hotclasstype3,'') hotclasstype3 ,
sum(postuserid) 数量
from tb group by hotclasstype1 ,hotclasstype2 ,hotclasstype3 with rollup
) t
order by case hotclasstype1 when '合计' then 2 else 1 end, hotclasstype1 , hotclasstype2 , hotclasstype3

--方法二,使用union all
select case when hotclasstype3 = '' and hotclasstype2 = '' then hotclasstype1
when hotclasstype3 = '' and hotclasstype2 <> '' then ' ' + hotclasstype2
when hotclasstype3 <> '' then ' ' + hotclasstype3
end 类别, 数量 from
(
select hotclasstype1 ,hotclasstype2 ,hotclasstype3 , sum(postuserid) 数量 from tb group by hotclasstype1 ,hotclasstype2 ,hotclasstype3
union all
select hotclasstype1 ,hotclasstype2 ,hotclasstype3='' , sum(postuserid) 数量 from tb group by hotclasstype1 ,hotclasstype2
union all
select hotclasstype1 ,hotclasstype2='' ,hotclasstype3='' , sum(postuserid) 数量 from tb group by hotclasstype1
union all
select hotclasstype1 ='合计',hotclasstype2='' ,hotclasstype3='' , sum(postuserid) 数量 from tb
) t
order by case hotclasstype1 when '合计' then 2 else 1 end, hotclasstype1 , hotclasstype2 , hotclasstype3

drop table tb

/*
类别 数量
-------------- -----------
大类1 5
中类1 3
小类1 1
小类2 1
小类3 1
中类2 2
小类1 1
小类2 1
大类2 2
中类1 1
小类1 1
中类2 1
小类2 1
合计 7

(所影响的行数为 14 行)
*/
-狙击手- 2009-01-05
  • 打赏
  • 举报
回复
cREATE TABLE tb(hotid int,hotclasstype1 varchar(10),hotclasstype2 varchar(10),hotclasstype3 varchar(10),postuserid INT)
Go
INSERT INTO tb
SELECT 1,'大类1','中类1','小类1',1 UNION ALL
SELECT 2,'大类1','中类1','小类2',1 UNION ALL
SELECT 3,'大类1','中类1','小类3',1 UNION ALL
SELECT 4,'大类1','中类2','小类1',1 UNION ALL
SELECT 5,'大类1','中类2','小类2',1 UNION ALL
SELECT 6,'大类2','中类1','小类1',1 UNION ALL
SELECT 7,'大类2','中类2','小类2',1
GO


SELECT
isnull(case when isnull(hotclasstype3,'') = '' and isnull(hotclasstype2,'') = '' then hotclasstype1
when isnull(hotclasstype3,'') = '' and isnull(hotclasstype2,'') <> '' then ' ' + hotclasstype2
when isnull(hotclasstype3,'') <> '' then ' ' + hotclasstype3
end ,'合计')类别,
count(1) as [sum]
FROM
tb
group by
hotclasstype1,hotclasstype2,hotclasstype3
with rollup
order by grouping(hotclasstype1)


/*
类别 sum
-------------- -----------
小类1 1
小类2 1
小类3 1
中类1 3
小类1 1
小类2 1
中类2 2
大类1 5
小类1 1
中类1 1
小类2 1
中类2 1
大类2 2
合计 7

(14 行受影响)
*/

drop table tb
dawugui 2009-01-05
  • 打赏
  • 举报
回复
CREATE TABLE tb(hotid int,hotclasstype1 varchar(10),hotclasstype2 varchar(10),hotclasstype3 varchar(10),postuserid INT)
Go
INSERT INTO tb
SELECT 1,'大类1','中类1','小类1',1 UNION ALL
SELECT 2,'大类1','中类1','小类2',1 UNION ALL
SELECT 3,'大类1','中类1','小类3',1 UNION ALL
SELECT 4,'大类1','中类2','小类1',1 UNION ALL
SELECT 5,'大类1','中类2','小类2',1 UNION ALL
SELECT 6,'大类2','中类1','小类1',1 UNION ALL
SELECT 7,'大类2','中类2','小类2',1
GO

select case when hotclasstype3 = '' and hotclasstype2 = '' then hotclasstype1
when hotclasstype3 = '' and hotclasstype2 <> '' then ' ' + hotclasstype2
when hotclasstype3 <> '' then ' ' + hotclasstype3
end 类别, 数量 from
(
select hotclasstype1 ,hotclasstype2 ,hotclasstype3 , sum(postuserid) 数量 , id = 1 from tb group by hotclasstype1 ,hotclasstype2 ,hotclasstype3
union all
select hotclasstype1 ,hotclasstype2 ,hotclasstype3='' , sum(postuserid) 数量 , id = 2 from tb group by hotclasstype1 ,hotclasstype2
union all
select hotclasstype1 ,hotclasstype2='' ,hotclasstype3='' , sum(postuserid) 数量 , id = 3 from tb group by hotclasstype1
union all
select hotclasstype1 ='合计',hotclasstype2='' ,hotclasstype3='' , sum(postuserid) 数量 , id = 4 from tb
) t
order by case id when 4 then 1 end, hotclasstype1 , hotclasstype2 , hotclasstype3

drop table tb

/*
类别 数量
-------------- -----------
大类1 5
中类1 3
小类1 1
小类2 1
小类3 1
中类2 2
小类1 1
小类2 1
大类2 2
中类1 1
小类1 1
中类2 1
小类2 1
合计 7

(所影响的行数为 14 行)

*/
dawugui 2009-01-05
  • 打赏
  • 举报
回复
CREATE TABLE tb(hotid int,hotclasstype1 varchar(10),hotclasstype2 varchar(10),hotcalsstype3 varchar(10),postuserid INT)
Go
INSERT INTO tb
SELECT 1,'大类1','中类1','小类1',1 UNION ALL
SELECT 2,'大类1','中类1','小类2',1 UNION ALL
SELECT 3,'大类1','中类1','小类3',1 UNION ALL
SELECT 4,'大类1','中类2','小类1',1 UNION ALL
SELECT 5,'大类1','中类2','小类2',1 UNION ALL
SELECT 6,'大类2','中类1','小类1',1 UNION ALL
SELECT 7,'大类2','中类2','小类2',1
GO

select hotclasstype1 ,hotclasstype2 ,hotcalsstype3 , 数量 from
(
select hotclasstype1 ,hotclasstype2 ,hotcalsstype3 , sum(postuserid) 数量 , id = 1 from tb group by hotclasstype1 ,hotclasstype2 ,hotcalsstype3
union all
select hotclasstype1 ,hotclasstype2 ,hotcalsstype3='' , sum(postuserid) 数量 , id = 2 from tb group by hotclasstype1 ,hotclasstype2
union all
select hotclasstype1 ,hotclasstype2='' ,hotcalsstype3='' , sum(postuserid) 数量 , id = 3 from tb group by hotclasstype1
union all
select hotclasstype1 ='合计',hotclasstype2='' ,hotcalsstype3='' , sum(postuserid) 数量 , id = 4 from tb
) t
order by case id when 4 then 1 end, hotclasstype1 , hotclasstype2 , hotcalsstype3

drop table tb

/*
hotclasstype1 hotclasstype2 hotcalsstype3 数量
------------- ------------- ------------- -----------
大类1 5
大类1 中类1 3
大类1 中类1 小类1 1
大类1 中类1 小类2 1
大类1 中类1 小类3 1
大类1 中类2 2
大类1 中类2 小类1 1
大类1 中类2 小类2 1
大类2 2
大类2 中类1 1
大类2 中类1 小类1 1
大类2 中类2 1
大类2 中类2 小类2 1
合计 7

(所影响的行数为 14 行)

*/
加载更多回复(1)

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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