22,302
社区成员




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
------------------------------------------------------------------------
-- 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
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 行)
*/
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
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 行)
*/
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 行)
*/