Consulting

Results 1 to 4 of 4

Thread: Need Help with SQL Query Syntax for Complex Join and Aggregation

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Jun 2024
    Location
    US
    Posts
    8
    Location

    Need Help with SQL Query Syntax for Complex Join and Aggregation

    Hello,


    I am currently working on a project where I need to retrieve specific data from multiple tables in my SQL Server database. I have three tables: Orders, OrderDetails, and Products. Orders table contains order information with columns OrderID and OrderDate. OrderDetails table contains details about each order item with columns OrderID, ProductID, and Quantity. Products table contains product details with columns ProductID and ProductName.

    What I need to achieve is to retrieve a summary report showing the total quantity of each product ordered within a specified date range; grouped by product name ; along with the total quantity ordered overall.

    I am trying to write a SQL query to accomplish this but have been running into syntax errors and difficulties with the aggregation.
    I have tried so far:


    HTML Code:
    SELECT 
        p.ProductName,
        SUM(od.Quantity) AS TotalQuantity,
        -- Additional aggregation and grouping logic
    
    FROM 
        Orders o
        INNER JOIN OrderDetails od ON o.OrderID = od.OrderID
        INNER JOIN Products p ON od.ProductID = p.ProductID
    
    WHERE 
        o.OrderDate BETWEEN '2024-01-01' AND '2024-06-30'
    GROUP BY 
        p.ProductName
    ORDER BY 
        TotalQuantity DESC;
    However, this query isn't yielding the correct results; and I'm struggling to properly aggregate the quantities and include the overall total in the same result set.

    Could someone please help me correct the syntax and suggest how to structure the query to achieve the desired output?





    Any help would be greatly appreciated.







    Thank you!
    gregbowerscpq
    Last edited by Aussiebear; 06-26-2024 at 09:52 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •