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
Not a
ROLLUPbut using aWITHstatement and aUNIONcould do the trick just as wel.The gist of it is to
qusing theWITHstatementSELECTall fromqGROUP BYagain fromqto calculate the balancesUNIONthe results togetherSQL Server 2000
SQL Server 2005+