Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7911165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:08:55+00:00 2026-06-03T13:08:55+00:00

This is my first post here so if I’ve been mistaken for my post

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:08:56+00:00Added an answer on June 3, 2026 at 1:08 pm

    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.

    SELECT  --cast(Orders.OrderDate as date),
            Products.ProductId 
            SUM(OrderDetails.OrderDetailQuantity) AS totalOrdered,
    FROM Orders 
    INNER JOIN OrderDetails ON Orders.OrderId = OrderDetails.OrderId 
    INNER JOIN Products ON OrderDetails.ProductId = Products.ProductId 
    where Orders.OrderDate between cast(@from as date) AND cast(@to as date)
    GROUP 
    BY      --cast(Orders.OrderDate as date),
            Products.ProductId 
    
    
    -- to illustrate:
    declare @From datetime = '1/1/2000 10:30:22',
            @To datetime = '1/3/2000 9:11:31'
    
    declare @orders table (i int, dt datetime)
    insert into @orders
        values(1, '1/1/2000 8:00'),(2, '1/2/2000 10:00'), (3, '1/4/2000 3:00')
    
    
    -- only returns i=2
    select *
    from    @orders
    where dt between @From and @To
    
    -- returns i=1 & i=2
    select *
    from    @orders
    where dt between cast(@From as date) and cast(@To as date)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello all this is my first post here i have been working in an
This is my first post here, recently i have been working with JSF2.0 with
this is my first post here but I've been a frequent reader of various
Sorry folks...first post here. Just getting the hang of this site. Thanks for the
This is my first post here on Stackoverflow, though over the years I've been
This is my first post here. I have been reading posts here since I
This is my first post here and I wanted to get some input from
This is my first post here. I have a problem. I need to take
this is my first post here on stackoverflow and am very impressed by the
Well, this is my first post here and really enjoying the site. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.