【问题标题】:Calculating sub-total in each group计算每组的小计
【发布时间】:2011-08-08 20:11:00
【问题描述】:

我需要来自多表的报告我使用这个查询(SQL Server)

Select  CASE When ([bills].[BT] ='0' and [bills].[T] = 1 )Then 'Purchas1'
When([bills].[BT] ='0' and [bills].[T] = 3 ) Then 'Output'
When([bills].[BT] ='0' and [bills].[T] = 4 ) Then 'Input' 
When [bills].[BT] ='1'  Then  'ٍSales'
When [bills].[BT] = '2' Then  'Prch2'
When [bills].[BT] = '3' Then  'ٍSales2'
When [bills].[BT] = '4'  Then 'SInput' 
END AS BillType,
[mat].[Name] as Product,
[mat].[Code], [store].[Name], 
SUM( [billInfo].[qty]) as Qtys 
from [mat],[billInfo000],[store],[bu],[bills] 
Where [bu].[TG] =[bills].[g]
and [billInfo].[ParentGUID] =[bu].[g] 
and [billInfo].[StoreGUID] =[store].[g] 
and [billInfo].[MatGUID] = [mat].[g] 
Group by [bills].[BT],[bills].[T],[mat].[Name], 
[mat].[Code],[store].[Name] ,[mat].[qty]

我想要的是在每组之后用计算添加一行 如果它是相同的产品和相同的代码和商店我需要 收集purchase1+Input+Prch2+SInput减去Sales、Output、Sales2 像这样:

账单类型 |产品 |代码 |姓名 |数量 -------- -------- ---- ---- ---- Purchas1 Pro1 001 主要 150 输出 Pro1 001 主 10 销售 Pro1 001 主要 30 Purch2 Pro1 001 主要 50 Balance Pro1 001 主 160 输出 Pro1 001 分支 10 销售 Pro1 001 分公司 10 Balance Pro1 001 Brabch -20

谢谢

【问题讨论】:

  • SQL有什么实现? SQL 服务器?甲骨文? MySQL? PostgreSQL?
  • 您是否需要在单个 SQL 语句中完成此操作?我不认为这是可能的,但如果是的话,我有兴趣看到那个美妙的 sql 语句。不过,在存储过程中执行此操作应该很容易。
  • @Icarus - 我认为ROLLUP 可能是可能的,但我没有和他们玩过很多
  • 看来,如果这是一个 SQL Server 查询,@Mohammed 需要做的就是在 Group BY 之后添加一个 WITH ROLLUP 子句。请参阅此示例:blog.sqlauthority.com/2010/02/24/…
  • @lcarus 我是初学者,如何在存储过程中执行此操作?

标签: sql-server grouping subtotal


【解决方案1】:

不是ROLLUP,而是使用WITH 语句和UNION 也可以做到这一点。

它的要点是

  • 使用WITH 语句将原始查询存储在q
  • SELECT全部来自q
  • 再次从q进一步细化GROUP BY以计算余额
  • UNION结果一起

SQL Server 2000

SELECT  *
FROM    (
          SELECT  CASE  WHEN ([bills].[BT] ='0' and [bills].[T] = 1 ) THEN 'Purchas1'
                        WHEN ([bills].[BT] ='0' and [bills].[T] = 3 ) THEN 'Output'
                        WHEN ([bills].[BT] ='0' and [bills].[T] = 4 ) THEN 'Input' 
                        WHEN [bills].[BT] = '1' THEN 'Sales'
                        WHEN [bills].[BT] = '2' THEN 'Prch2'
                        WHEN [bills].[BT] = '3' THEN 'Sales2'
                        WHEN [bills].[BT] = '4' THEN 'SInput' 
                  END AS BillType
                  , [mat].[Name] AS Product
                  , [mat].[Code]
                  , [store].[Name]
                  , SUM([billInfo].[qty]) AS Qtys 
          FROM    [mat]
                  INNER JOIN [billInfo000] ON [billInfo000].[MatGUID] = [mat].[g]
                  INNER JOIN [store] ON [store].[g] = [billInfo0001].[StoreGUID]
                  INNER JOIN [bu] ON [bu].[g] = [billInfo000].[ParentGUID]
                  INNER JOIN [bills] ON [bills].[g] = [bu].[TG]
          GROUP BY
                [bills].[BT]
                , [bills].[T]
                , [mat].[Name]
                , [mat].[Code]
                , [store].[Name]
                , [mat].[qty]
        ) bt
UNION ALL
SELECT  'Balance'
        , Product
        , Code
        , Name
        , SUM(
            CASE  WHEN BillType = 'Purchas1' THEN Qtys
                  WHEN BillType = 'Output' THEN Qtys * -1
                  WHEN BillType = 'Sales' THEN Qtys * -1
                  WHEN BillType = 'Purch2' THEN Qtys
            END)
FROM    (
          SELECT  CASE  WHEN ([bills].[BT] ='0' and [bills].[T] = 1 ) THEN 'Purchas1'
                        WHEN ([bills].[BT] ='0' and [bills].[T] = 3 ) THEN 'Output'
                        WHEN ([bills].[BT] ='0' and [bills].[T] = 4 ) THEN 'Input' 
                        WHEN [bills].[BT] = '1' THEN 'Sales'
                        WHEN [bills].[BT] = '2' THEN 'Prch2'
                        WHEN [bills].[BT] = '3' THEN 'Sales2'
                        WHEN [bills].[BT] = '4' THEN 'SInput' 
                  END AS BillType
                  , [mat].[Name] AS Product
                  , [mat].[Code]
                  , [store].[Name]
                  , SUM([billInfo].[qty]) AS Qtys 
          FROM    [mat]
                  INNER JOIN [billInfo000] ON [billInfo000].[MatGUID] = [mat].[g]
                  INNER JOIN [store] ON [store].[g] = [billInfo0001].[StoreGUID]
                  INNER JOIN [bu] ON [bu].[g] = [billInfo000].[ParentGUID]
                  INNER JOIN [bills] ON [bills].[g] = [bu].[TG]
          GROUP BY
                [bills].[BT]
                , [bills].[T]
                , [mat].[Name]
                , [mat].[Code]
                , [store].[Name]
                , [mat].[qty]
        ) balance
GROUP BY
        Product
        , Code
        , Name

SQL Server 2005+

;WITH q AS (
  SELECT  CASE  WHEN ([bills].[BT] ='0' and [bills].[T] = 1 ) THEN 'Purchas1'
                WHEN ([bills].[BT] ='0' and [bills].[T] = 3 ) THEN 'Output'
                WHEN ([bills].[BT] ='0' and [bills].[T] = 4 ) THEN 'Input' 
                WHEN [bills].[BT] = '1' THEN 'Sales'
                WHEN [bills].[BT] = '2' THEN 'Prch2'
                WHEN [bills].[BT] = '3' THEN 'Sales2'
                WHEN [bills].[BT] = '4' THEN 'SInput' 
          END AS BillType
          , [mat].[Name] AS Product
          , [mat].[Code]
          , [store].[Name]
          , SUM([billInfo].[qty]) AS Qtys 
  FROM    [mat]
          INNER JOIN [billInfo000] ON [billInfo000].[MatGUID] = [mat].[g]
          INNER JOIN [store] ON [store].[g] = [billInfo0001].[StoreGUID]
          INNER JOIN [bu] ON [bu].[g] = [billInfo000].[ParentGUID]
          INNER JOIN [bills] ON [bills].[g] = [bu].[TG]
  GROUP BY
        [bills].[BT]
        , [bills].[T]
        , [mat].[Name]
        , [mat].[Code]
        , [store].[Name]
        , [mat].[qty]
)
SELECT  *
FROM    q
UNION ALL
SELECT  'Balance'
        , Product
        , Code
        , Name
        , SUM(
            CASE  WHEN BillType = 'Purchas1' THEN Qtys
                  WHEN BillType = 'Output' THEN Qtys * -1
                  WHEN BillType = 'Sales' THEN Qtys * -1
                  WHEN BillType = 'Purch2' THEN Qtys
            END)
FROM    q
GROUP BY
        Product
        , Code
        , Name

【讨论】:

  • 谢谢@Lieven,它似乎在我的电脑上运行良好但是当我尝试在客户端服务器上测试它时,它适用于 SQL-Server 2000 :( 我遇到了“With”问题我应该怎么做怎样让它在 SQL-Server 2000 和 2008 中都能正常工作?
  • @Mohammed Rabee - WITH 是 SQL Server 2005 +。要让它适用于 SQL Server 2000,您需要做的就是用WITH 中的SELECT 语句替换q(其中两个)的每个实例(并删除WITHoffcourse)
  • 我不知道我在哪里犯了错误但是它不起作用:(
  • @Mohammed Rabee - 我已经为 SQL Server 2000 添加了查询。
猜你喜欢
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 1970-01-01
  • 2012-04-06
相关资源
最近更新 更多