Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 计算各组小计_Sql Server_Grouping_Subtotal - Fatal编程技术网

Sql server 计算各组小计

Sql server 计算各组小计,sql-server,grouping,subtotal,Sql Server,Grouping,Subtotal,我需要从多表报告我使用这个查询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 'ٍ

我需要从多表报告我使用这个查询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]
我想要的是在每个组后面添加一行 如果它是相同的产品,相同的代码和商店,我需要 收集采购1+输入+Prch2+输入减去销售、输出、销售2 像这样:

谢谢

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

其要点是

使用WITH语句将原始查询存储在q中 从q中选择全部 通过再次从QT计算余额来进一步细化组 把结果结合起来 SQL Server 2000 SQL Server 2005+ 不是汇总,而是使用WITH语句和UNION也可以做到这一点

其要点是

使用WITH语句将原始查询存储在q中 从q中选择全部 通过再次从QT计算余额来进一步细化组 把结果结合起来 SQL Server 2000 SQL Server 2005+

SQL的什么实现?SQL Server?神谕MySQL?PostgreSQL?是否要求在单个SQL语句中完成此操作?我认为这是不可能的,但如果可能的话,我很想看看那个美妙的sql语句。不过,在存储过程中执行这项操作应该很容易。@Icarus-我认为使用ROLLUP可能是可行的,但我还没有使用它们。很明显,如果这是SQL Server查询,@Mohammed需要做的就是在Group BY之后添加with ROLLUP子句。请参阅此示例:@lcarus我是初学者,如何在存储过程中执行此操作?SQL的什么实现?SQL Server?神谕MySQL?PostgreSQL?是否要求在单个SQL语句中完成此操作?我认为这是不可能的,但如果可能的话,我很想看看那个美妙的sql语句。不过,在存储过程中执行这项操作应该很容易。@Icarus-我认为使用ROLLUP可能是可行的,但我还没有使用它们。很明显,如果这是SQL Server查询,@Mohammed需要做的就是在Group BY之后添加with ROLLUP子句。请参阅此示例:@lcarus我是初学者,如何在存储过程中执行此操作?感谢@Lieven,它似乎在我的PC中运行良好,但当我尝试在SQL Server 2000上运行的客户端服务器上测试它时:我面临着如何使其在SQL Server 2000和2008中运行的问题?@Mohammed Rabee-With is SQL Server 2005+。要让它在SQL Server 2000上运行,您只需将其中qtwo的每个实例替换为其中的SELECT语句with and drop with offcourse我不知道我在哪里做的​​一个错误,但它不起作用:@Mohammed Rabee-我添加了SQL Server 2000的查询。谢谢@Lieven,它似乎在我的PC上运行良好,但当我尝试在客户端服务器上测试它时,在SQLServer2000上运行的是什么:我面临的问题是,我应该做些什么才能使它在SQLServer2000和2008中运行?@MohammedRabee-With is SQLServer2005+。要让它在SQL Server 2000上运行,您只需将其中qtwo的每个实例替换为其中的SELECT语句with and drop with offcourse我不知道我在哪里做的​​一个错误,但它不起作用:@Mohammed Rabee-我添加了对SQL Server 2000的查询。 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
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
;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