0

I have written an SQL query to return the total quantity of products ordered and total price. The query returns a result for each order in the range I have specified.Please find attached the output i am getting.

the desired output i want is as follows:
(year,productcode, productname, orders, quantity, totalprice) 3-1-11 to 6-3-11 , SWAX-0022 , Guava Berry , 34 , 7, 854.55

Please advise.The above output is just an example for one product.


MY SQL Query is as follows:


SELECT
OrderDetails.Year,
OrderDetails.ProductCode,
OrderDetails.ProductName,
OrderDetails.Orders,
OrderDetails.Quantity,
OrderDetails.TotalPrice
FROM (
SELECT
Orders.OrderDate AS Year,
OrderDetails.ProductCode AS ProductCode,
Max(OrderDetails.ProductName) AS ProductName,
Max(Orders.OrderDate) As OrderDate,
Max(Orders.ShipDate) As ShipDate,
Count(OrderDetails.ProductCode) As Orders,
Sum(OrderDetails.Quantity) AS Quantity,
Sum(OrderDetails.TotalPrice * OrderDetails.Quantity) AS TotalPrice
FROM OrderDetails WITH(NOLOCK)
INNER JOIN Orders WITH(NOLOCK) ON OrderDetails.OrderID = Orders.OrderID
LEFT JOIN Products_Extended pe ON OrderDetails.ProductID = pe.ProductID
WHERE Orders.OrderStatus = 'Shipped'
AND DATEDIFF(dd, Orders.OrderDate, '08/03/2010') <= 0
AND DATEDIFF(dd, Orders.OrderDate, '03/01/2011') >= 0
GROUP BY
Orders.OrderDate,
OrderDetails.ProductCode
) OrderDetails
ORDER BY
OrderDetails.Year DESC,
OrderDetails.ProductCode DESC

Edited by Mangesh G: n/a

Attachments ouput.png 24.59 KB
3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by urtrivedi
0

What is the query that you are working on ?

SELECT
OrderDetails.Year,
OrderDetails.ProductCode,
OrderDetails.ProductName,
OrderDetails.Orders,
OrderDetails.Quantity,
OrderDetails.TotalPrice
FROM (
SELECT
Orders.OrderDate AS Year,
OrderDetails.ProductCode AS ProductCode,
Max(OrderDetails.ProductName) AS ProductName,
Max(Orders.OrderDate) As OrderDate,
Max(Orders.ShipDate) As ShipDate,
Count(OrderDetails.ProductCode) As Orders,
Sum(OrderDetails.Quantity) AS Quantity,
Sum(OrderDetails.TotalPrice * OrderDetails.Quantity) AS TotalPrice
FROM OrderDetails WITH(NOLOCK)
INNER JOIN Orders WITH(NOLOCK) ON OrderDetails.OrderID = Orders.OrderID
LEFT JOIN Products_Extended pe ON OrderDetails.ProductID = pe.ProductID
WHERE Orders.OrderStatus = 'Shipped'
AND DATEDIFF(dd, Orders.OrderDate, '08/03/2010') <= 0
AND DATEDIFF(dd, Orders.OrderDate, '03/01/2011') >= 0
GROUP BY
Orders.OrderDate,
OrderDetails.ProductCode
) OrderDetails
ORDER BY
OrderDetails.Year DESC,
OrderDetails.ProductCode DESC

0
select '03-01-2011 to 08-03-2011' AS Year,
OrderDetails.ProductCode AS ProductCode,
OrderDetails.ProductName AS ProductName,
Count(distinct OrderDetails.OrderID) As Orders,
sum( OrderDetails.qty) As total_qty,
sum( OrderDetails.amount) As total_amount,
FROM OrderDetails WITH(NOLOCK) 
INNER JOIN Orders WITH(NOLOCK) ON OrderDetails.OrderID = Orders.OrderID
LEFT JOIN Products_Extended pe ON OrderDetails.ProductID = pe.ProductID 
WHERE Orders.OrderStatus = 'Shipped'
Orders.OrderDate between  '2011-03-08' and '2011-01-03'
GROUP BY
'03-01-2011 to 08-03-2011' ,
OrderDetails.ProductCode,
OrderDetails.ProductName
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.