|
Пользователь
Регистрация: 26.07.2006
Сообщений: 43
|
RE: Запрос на подсчет суммы
Я нaдеюсь Вы смoжете применить этoт пример:
create table #tmp(
[Month] int,
[Quarter] int,
[Year] int,
[Num1] int,
[Num2] int)
Insert into #tmp
values(1,1,2006,10,5)
Insert into #tmp
values(1,1,2006,5,10)
Insert into #tmp
values(2,1,2006,10,5)
Insert into #tmp
values(2,1,2006,5,10)
Insert into #tmp
values(3,1,2006,10,5)
Insert into #tmp
values(3,1,2006,5,10)
Insert into #tmp
values(4,2,2006,10,5)
Insert into #tmp
values(4,2,2006,5,10)
Insert into #tmp
values(5,2,2006,10,5)
Insert into #tmp
values(5,2,2006,5,10)
Insert into #tmp
values(6,2,2006,10,5)
Insert into #tmp
values(6,2,2006,5,10)
Insert into #tmp
values(7,3,2006,10,5)
Insert into #tmp
values(7,3,2006,5,10)
Insert into #tmp
values(8,3,2006,10,5)
Insert into #tmp
values(8,3,2006,5,10)
Insert into #tmp
values(9,3,2006,10,5)
Insert into #tmp
values(9,3,2006,5,10)
Insert into #tmp
values(10,4,2006,10,5)
Insert into #tmp
values(10,4,2006,5,10)
Insert into #tmp
values(11,4,2006,10,5)
Insert into #tmp
values(11,4,2006,5,10)
Insert into #tmp
values(12,4,2006,10,5)
Insert into #tmp
values(12,4,2006,5,10)
select 1 as [Sort], *
from #tmp
union
select distinct [Sort], isnull([Month], 13) [Month],
isnull([Quarter], 5) [Quarter], [Year], Num1, Num2
from
(select 2 as [Sort],
[Month], [Quarter], [Year],
sum(Num1) Num1, Sum(Num2) Num2
from #tmp
group by [Month], [Quarter], [Year]
With cube) as sums
where ([Year] is Not Null)
and (([Quarter] Is Null and [Month] Is Null) Or ([Quarter] Is Not Null))
order by 4,3,2
drop table #tmp
|