DECLARE @t TABLE(Item varchar(10),Color varchar(10),Quantity int)
INSERT @t SELECT 'A','2001',200
INSERT @t SELECT 'A','2002',300
INSERT @t SELECT 'A','2003',400
INSERT @t SELECT 'B','2001',300
INSERT @t SELECT 'B','2002',400
INSERT @t SELECT 'B','2003',100--统计
SELECT Item,Color,Quantity
FROM(
--明细
SELECT Item,Color,Quantity=SUM(Quantity)
,s1=0,s2=Item,s3=0
FROM @t
GROUP BY Item,Color
UNION ALL
--各Item合计
SELECT '','合计',Quantity=SUM(Quantity)
,s1=0,s2=Item,s3=1
FROM @t
GROUP BY Item
UNION ALL
--总计
SELECT '总合计','',Quantity=SUM(Quantity)
,s1=1,s2='',s3=1
FROM @t
)a ORDER BY s1,s2,s3
INSERT @t SELECT 'A','2001',200
INSERT @t SELECT 'A','2002',300
INSERT @t SELECT 'A','2003',400
INSERT @t SELECT 'B','2001',300
INSERT @t SELECT 'B','2002',400
INSERT @t SELECT 'B','2003',100--统计
SELECT Item,Color,Quantity
FROM(
--明细
SELECT Item,Color,Quantity=SUM(Quantity)
,s1=0,s2=Item,s3=0
FROM @t
GROUP BY Item,Color
UNION ALL
--各Item合计
SELECT '','合计',Quantity=SUM(Quantity)
,s1=0,s2=Item,s3=1
FROM @t
GROUP BY Item
UNION ALL
--总计
SELECT '总合计','',Quantity=SUM(Quantity)
,s1=1,s2='',s3=1
FROM @t
)a ORDER BY s1,s2,s3
解决方案 »
- 请教一个效率的问题,有可行答案立马结贴,每个字段建索引?不好吧
- 关于insert into select的问题
- |zyciis| 求一条SQL查询语句 并回答为什么有些贴子没有结贴的问题
- 疑难问题,关于DTS包
- 如何成批替换某个字段的值/在线等待
- 谁来帮我解释下这个存储过程里不明白的地方
- 修改存储过程所有者,出现错误,请教
- VFP6。0报表中,不用添加程序,某个域控件怎样实现自动合计其它几个域控件的值,在线等候!
- 求助:“ROLLBACK TRANSACTION 请求没有对应的 BEGIN TRANSACTION”。弟兄们帮帮忙吧!!!
- sql server 的初级问题
- 查询时用字符代替数字表示?
- 请各位帮忙看看!
insert tc select 'a','2001-1-1',200
union all select 'a','2001-1-3',300
union all select 'a','2002-2-3',300
union all select 'a','2003-3-3',400
union all select 'b','2001-2-3',400
union all select 'b','2003-3-6',300
union all select 'b','2002-4-3',300
union all select 'b','2004-11-3',100
union all select 'b','2001-2-4',399
declare @dt_begin varchar(10),@dt_end varchar(10)
select @dt_begin='2001',@dt_end='2003'
select item,year,quantity
from(
select item,[year]=year(year),quantity=sum(quantity),s1=0,s2=item,s3=0
from tc
where year(year) between @dt_begin and @dt_end
group by item,year(year)
union all
select '合计','',quantity=sum(quantity),s1=0,s2=item,s3=1
from tc
where year(year) between @dt_begin and @dt_end
group by item
union all
select '总计','',quantity=sum(quantity),s1=1,s2='',s3=1
from tc
where year(year) between @dt_begin and @dt_end
)a order by s1,s2,s3
可以满足要求!就是合计,总计那里中间不知道怎么会出现0
INSERT @t SELECT 'A','2001',200
INSERT @t SELECT 'A','2002',300
INSERT @t SELECT 'A','2003',400
INSERT @t SELECT 'B','2001',300
INSERT @t SELECT 'B','2002',400
INSERT @t SELECT 'B','2003',100
select Item=(case when Item is null then '总合计'
when color is null then'合计' else Item end)
,color,sum(Quantity)
from @t
group by Item,color with rollup
1。中间用year的话,就出现0了,是不是字段类型为数字型了
2。某一个行业的明细的要根据年排序。此外,我开始忘了说明了,我的行业名称是从另外一张表(行业编号、行业名称)得来的,
而开始的那张表应当是表(行业编号、年、产值)。两个表要做个连接,不知如何做?
create table tc(item varchar(10),year datetime,quantity int)
insert tc select 'a','2001-1-1',200
union all select 'a','2001-1-3',300
union all select 'a','2002-2-3',300
union all select 'a','2003-3-3',400
union all select 'b','2001-2-3',400
union all select 'b','2003-3-6',300
union all select 'b','2002-4-3',300
union all select 'b','2004-11-3',100
union all select 'b','2001-2-4',399
declare @dt_begin varchar(10),@dt_end varchar(10)
select @dt_begin='2001',@dt_end='2003'
select
item=case
when grouping([year])=0 then item
when grouping([year])=1 and grouping(a.item)<>1 then '合计'
when grouping(a.item)=1 then '总合计'
end,
[year]=case
when grouping([year])=1 then ''
else cast([year] as varchar)
end,
quantity=sum(a.quantity)
from
(select item=item,[year]=year(year),quantity=quantity from tc)a
where [year] between @dt_begin and @dt_end
group by a.item,a.[year] with rollup
这种方法是符合要求的!
inner join 表(行业编号、行业名称)
create table ta(id int,item varchar(10))
insert ta select 1,'a'
union all select 2,'b'
union all select 3,'c'
union all select 4,'d'
create table tc(id int ,year datetime,quantity int)
insert tc select 1,'2001-1-1',200
union all select 1,'2001-1-3',300
union all select 1,'2002-2-3',300
union all select 1,'2003-3-3',400
union all select 1,'2001-2-3',400
union all select 2,'2003-3-6',300
union all select 2,'2002-4-3',300
union all select 3,'2004-11-3',100
union all select 3,'2001-2-4',399
union all select 3,'2003-2-2',222
declare @dt_begin varchar(10),@dt_end varchar(10)
select @dt_begin='2001',@dt_end='2003'
select item=case
when grouping([year])=0 then item
when grouping([year])=1 and grouping(item)<>1 then '合计'
when grouping(item)=1 then '总合计'
end,
[year]=case
when grouping([year])=1 then ''
else cast([year] as varchar)
end,
quantity=sum(quantity)
from ta a right join
(select [id]=id,[year]=year(year),quantity=quantity from tc)b
on a.id=b.[id] and [year] between @dt_begin and @dt_end
group by item,[year] with rollup
如果你要显示全部部门的总计,就应该把right join 改为left Join
这里只显示在tc表中有记录的部门的报表
后面加上过滤条件 where year(year) between @dt_begin and @dt_end
create table #1 (name varchar(10),year varchar(10),value int)
insert into #1 select
'A' , 2001, 200
union all select
'A' , 2002, 300
union all select
'B' , 2001, 300
union all select
'A' , 2003, 400
union all select
'B' , 2002, 400
union all select
'B' , 2003 ,100select 行业名称=case when grouping(year)=1 and grouping(name)=0 then '合计'
else case when grouping(year)=1 and grouping(name)=1 then '总计'
else name end
end,
年=year,产值=sum(value)
from #1
where year between '2001' and '2003'
group by name,year with rollup