Search code examples
sqlsql-serveradventureworks

Adventure Works SQL Server Enquiry


I am writing a SQL query using the AdventureWorks 2014 database.

I want to show Which orders contain more than two products? Show order number, order value, and number of products that the order contains.

I tried to write statement by itself (see below), but I'd like to be able to solve the relation :

select SalesOrderID , ProductID , LineTotal
from Sales.SalesOrderDetail
order by SalesOrderID

enter image description here


Solution

  • SELECT SalesOrderID,
           COUNT(ProductID) as total_products,
           SUM(LineTotal) as total_invoice
    FROM SalesOrderDetail s
    GROUP BY SalesOrderID
    HAVING COUNT(ProductID) > 2
    ORDER BY s.SalesOrderID