Не ROLLUP
, но использование оператора WITH
и UNION
может сделать то же самое.
Суть в том, чтобы
- Сохранить исходный запрос в
q
, используя оператор WITH
SELECT
все из q
- Далее уточните
GROUP BY
снова из q
, чтобы вычислить сальдо 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