如何对Sql数据及其小计

时间:2015-11-05 09:42:49

标签: sql sql-server

我从SQL查询中获取数据。我得到以下格式数据

S.no     name          budget
---------------------------------
1      Local Travel     123
2      Global Travel    123
3      Local Expense     12
4      Current Expense   12

这个结果我得到了但我想要的是,请看下面的表格格式

Travel

    1    Local Travel     123
    2    Global Travel    123
             subtotal =   246

Expense

   3    Local Expenses    12
   4    Current Expe      12
         Subtotal   =     24

请帮忙。我在这里粘贴我的实际查询

SELECT 
    concat([Segment_0], '-' , [Segment_1], '-' , [Segment_2]),  
    T2.[AcctName],
    T5.[Name], 
    SUM(T0.[Debit]) as Actual, 
    SUM(T3.[DebLTotal]) as Budget 
FROM
    JDT1 T0  
INNER JOIN 
    OJDT T1 ON T0.[TransId] = T1.[TransId] 
INNER JOIN 
    OACT T2 ON T0.[Account] = T2.[AcctCode], OBGT T3 
INNER JOIN 
    BGT1 T4 ON T3.[AbsId] = T4.[BudgId] 
INNER JOIN 
    OBGS T5 ON T3.[Instance] = T5.[AbsId],OASC T6 
WHERE 
    T2.[AcctName] LIKE '%Travel%' 
    AND T5.[Name] = 'Main Budget 2015'
GROUP BY 
    concat([Segment_0], '-' , [Segment_1], '-' , [Segment_2]),  
    T2.[AcctName], T5.[Name]

我正在为我的表Please check here

添加脚本

1 个答案:

答案 0 :(得分:0)

我首先将查询结果保存在临时表中,这样我就可以显示您所显示的视图,如下所示:

S.no     name          budget
---------------------------------
1      Local Travel     123
2      Global Travel    123
3      Local Expense     12
4      Current Expense   12

为了做到这一点,只需在#testTable之前插入from即可 像这样:

...
 SUM(T3.[DebLTotal]) as Budget 
into #testTable
FROM
    JDT1 T0  
....

然后在#myTemp中使用上面的视图,我只想写:

    select sno, name, budget, 
(case 
when name like '%Travel' then right(name, 6)
when name like '%Expense%' then right(name,7)
end) as NameTrim 
into #myTemp
from #testTable

上面的查询会创建另一个名为NameTrim的列,以便能够区分费用或旅行。

然后我使用窗口功能分组:

select sno, name, budget,NameTrim, 
(case 
when  NameTrim like 'Travel' then 
sum(budget) over (partition by NameTrim)end)  as TravelCost,
(case 
when NameTrim like 'Expense' then 
sum(budget) over (partition by NameTrim)end)as Expense
from #myTemp

模拟环境:

create table #testTable (sno int, name varchar (50), budget int)

insert into #testTable values 
('1', 'Local Travel' , '123'),
('2', 'Global Travel' , '123'),
('3', 'Local Expense' , '12'),
('4', 'Curent Expense' , '12')

select sno, name, budget, 
(case 
when name like '%Travel' then right(name, 6)
when name like '%Expense%' then right(name,7)
end) as NameTrim 
into #myTemp
from #testTable

select sno, name, budget,NameTrim, 
(case 
when  NameTrim like 'Travel' then 
sum(budget) over (partition by NameTrim)end)  as TravelCost,
(case 
when NameTrim like 'Expense' then 
sum(budget) over (partition by NameTrim)end)as Expense
from #myTemp

这可以解决您的问题吗?似乎工作正常,查询产生:

sno name           budget    NameTrim     TravelCost    Expense
3   Local Expense   12       Expense       NULL            24
4   Curent Expense  12       Expense       NULL            24
1   Local Travel    123      Travel      246               NULL
2   Global Travel   123      Travel      246               NULL