Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6324109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:38:41+00:00 2026-05-24T16:38:41+00:00

I need report from multi table I use this query (SQL Server) Select CASE

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:38:42+00:00Added an answer on May 24, 2026 at 4:38 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This multiple SELECT QUERY runs consecutively. I need one report with multiple columns for
i need a report and i should use pivot table for it.Report will be
I need advice from more advanced SQL experts on this. I am being asked
SELECT id, amount FROM report I need amount to be amount if report.type='P' and
I need to query an MySQL table to get a list of names then,
I need to produce a SQL report showing the number of times a particular
I want to generate a technical report from lisp (AllegroCL in my case) and
I have a report that use a multi-value parameter into a in statement in
This is the desired result I need to populate as a report, where xx
i need to export 3 full sql table values in to a single excel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.