union all and group by

Muhammad Anzar 41 Reputation points
2021-03-27T22:53:22.27+00:00

SELECT ProductName Detail, sum(PackQty) PurchaseQty, SUM(Purchase) PurchaseValue, sum(SaleQuantity) SaleQty,--, SUM(Sale) Sale, sum (Received) CasReceive , SUM(paid) CashPaid FROM ( SELECT PurchaseDate AS TransactionDate, ProductName, 0 AS Sale, Total as Purchase, PackQuantity as PackQty, 0 as SaleQuantity, 0 AS Received, 0 as Paid FROM dbo.Pur_OrderDetail UNION ALL SELECT ReciveDate AS TransactionDate, Remarks, Received as CashReceived, 0 AS Purchase, 0 as PackQty , 0 as SaleQuantity, Received AS Received, 0 as Paid FROM dbo.CashReceived UNION ALL SELECT SaleDate AS TransactionDate, ProductName, Total as Sale, 0 AS Purchase, 0 as PackQty , SaleQuantity,-- as SaleQty 0 AS Received, 0 as Paid FROM dbo.[Order Details] UNION ALL SELECT PaymentDate AS TransactionDate, SupplierName, 0 as CashReceived, 0 AS Purchase, 0 as PackQty , 0 as SaleQuantity, 0 AS Received, Paid FROM dbo.CashPayments)as temp1 where TransactionDate between '2021-03-17' and '2021-03-28' GROUP BY ProductName--, TransactionDate --i need TransactionDate for parameter and not in group by clause

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,631 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,547 questions
{count} votes

Accepted answer
  1. MelissaMa-MSFT 24,176 Reputation points
    2021-03-29T02:37:43.953+00:00

    Hi @Muhammad Anzar ,

    Welcome to Microsoft Q&A!

    Please refer below and check whether it is helpful.

    ;with cte as   
    (SELECT PurchaseDate AS TransactionDate, ProductName, 0 AS Sale, Total as Purchase, PackQuantity as PackQty, 0 as SaleQuantity,   
    0 AS Received, 0 as Paid FROM dbo.Pur_OrderDetail   
    UNION ALL   
    SELECT ReciveDate AS TransactionDate, Remarks, Received as CashReceived, 0 AS Purchase, 0 as PackQty , 0 as SaleQuantity,   
    Received AS Received, 0 as Paid FROM dbo.CashReceived   
    UNION ALL   
    SELECT SaleDate AS TransactionDate, ProductName, Total as Sale, 0 AS Purchase, 0 as PackQty , SaleQuantity,  
    0 AS Received, 0 as Paid FROM dbo.[Order Details]   
    UNION ALL   
    SELECT PaymentDate AS TransactionDate, SupplierName, 0 as CashReceived, 0 AS Purchase, 0 as PackQty , 0 as SaleQuantity,   
    0 AS Received, Paid FROM dbo.CashPayments)  
    ,cte1 as (  
    select ProductName,Sale, Purchase,PackQty, SaleQuantity, Received,  Paid  
     from cte   
    where TransactionDate between '2021-03-17' and '2021-03-28' )  
    select ProductName Detail, sum(PackQty) PurchaseQty, SUM(Purchase) PurchaseValue, sum(SaleQuantity) SaleQty,   
    SUM(Sale) Sale, sum (Received) CasReceive , SUM(paid) CashPaid   
    from cte1  
    GROUP BY ProductName  
    

    If above is not working,we recommend that you post CREATE TABLE statements for your tables together with INSERT statements with sample data, enough to illustrate all angles of the problem. We also need to see the expected result of the sample.

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful