This is my first post here so if I’ve been mistaken for my post and English I say sorry 🙂
I already do research in google and unfortunately until now I can’t solve this.
This my problem, show the total orders by date on each product and I want to distinct by products
Here’s my query sample
SELECT Orders.OrderDate
,SUM(OrderDetails.OrderDetailQuantity) AS totalOrdered
,Products.ProductId
FROM Orders
INNER JOIN OrderDetails ON Orders.OrderId = OrderDetails.OrderId
INNER JOIN Products ON OrderDetails.ProductId = Products.ProductId
GROUP BY Orders.OrderId
,Orders.OrderDate
,Products.ProductId
HAVING (CONVERT(VARCHAR, Orders.OrderDate, 23) BETWEEN @from AND @to)
Now, I want to distinct by product according to the between OrderDate, I know the big problem here is the DATE but I don’t have any idea on how to distinct the date by this query.
Please help me. Thank you.
P.S.: if do you want to solve this in linq expression it would be highly accepted 🙂
Sample data and desired results would help. When you say “distinct by” I assume you mean group by. Note, in the WHERE clause you dont need to cast Order.OrderDate if you ensure that the time component of your @from & @to params are set correctly (to include each entire day). Its never a good idea to apply a cast operation to the left side of a comparison.