SQL分组和小计

时间:2013-06-12 14:57:48

标签: sql tsql sql-server-2005

我有以下查询: -

SELECT 
FISCALYEAR, 
DATENAME(Month,JVDate) as Month,
ACCOUNTNUMBER,
ACCOUNTDESCRIPTION, 
CATEGORY, 
POSTINGTYPE,
ORIGNETAMOUNT, 
FROM dbo.vw_GLTrialBalancev2010 INNER JOIN
                        GL00100 ON GL00100.ACTNUMBR_1 = dbo.vw_GLTrialBalancev2010.SEGMENT1 AND GL00100.ACTNUMBR_2 = dbo.vw_GLTrialBalancev2010.SEGMENT2

显示以下结果: -

enter image description here

任何人都可以告诉我一种分组的方法,然后按类别小计。

我希望看到类似下面的内容: -

enter image description here

所有人都非常感谢。

1 个答案:

答案 0 :(得分:1)

SELECT 
  FISCALYEAR, 
  DATENAME(Month,JVDate) as Month,
  ACCOUNTNUMBER,
  ACCOUNTDESCRIPTION, 
  CATEGORY, 
  POSTINGTYPE,
  sum(ORIGNETAMOUNT) as ORIGNETAMOUNT, 
FROM dbo.vw_GLTrialBalancev2010 
INNER JOIN GL00100 
        ON GL00100.ACTNUMBR_1 = dbo.vw_GLTrialBalancev2010.SEGMENT1 
        AND GL00100.ACTNUMBR_2 = dbo.vw_GLTrialBalancev2010.SEGMENT2
group by FISCALYEAR, DATENAME(Month,JVDate) as Month, ACCOUNTNUMBER, 
         ACCOUNTDESCRIPTION, CATEGORY, POSTINGTYPE