计算每组的小计

发布于 2024-11-28 16:09:54 字数 1366 浏览 1 评论 0原文

我需要来自多表的报告我使用此查询(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]

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

BillType | Product | Code | Name | Qtys 
--------   -------   ----   ----   ---- 
Purchas1   Pro1      001    Main    150
Output     Pro1      001    Main    10
Sales      Pro1      001    Main    30
Purch2     Pro1      001    Main    50
Balance    Pro1      001    Main    160
Output     Pro1      001    Branch  10
Sales      Pro1      001    Branch  10
Balance    Pro1      001    Brabch  -20

谢谢

I need report from multi table I use this query (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]

what i want is adding one row after each group with Calculating
If it's same Product and same code and store i need
collect purchase1+Input+Prch2+SInput minus Sales, Output, Sales2
Like This:


BillType | Product | Code | Name | Qtys
-------- ------- ---- ---- ----
Purchas1 Pro1 001 Main 150
Output Pro1 001 Main 10
Sales Pro1 001 Main 30
Purch2 Pro1 001 Main 50
Balance Pro1 001 Main 160
Output Pro1 001 Branch 10
Sales Pro1 001 Branch 10
Balance Pro1 001 Brabch -20

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-12-05 16:09:54

不是 ROLLUP,而是使用 WITH 语句和 UNION 也可以达到同样的效果。

其要点是

  • 使用 WITH 语句
  • SELECT all from q 将原始查询存储在 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

Not a ROLLUP but using a WITH statement and a UNION could do the trick just as wel.

The gist of it is to

  • Store your original query in q using the WITH statement
  • SELECT all from q
  • Further refine the GROUP BY again from qto calculate the balances
  • UNION the results together

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文