Char
Char

Reputation: 955

How to get data from several days after certain hour of each day?

I am working on some reports about Sales given certain times, I have managed to get the sum of the sales given the day of the week, but i need further narrowing, I need to get the sum of each day of the week sales, that are made after 13:00.

This is the code used for getting the sum of the sales per day of the week

SELECT 
sum(ammount), datename (weekday,sale_date)
FROM SALES where sale_date > '2012-01-01 00:00:00'
group by datename (weekday,sale_date)

Upvotes: 1

Views: 94

Answers (2)

Kermit
Kermit

Reputation: 34055

Why not use the hh part of DATEPART?

SELECT Sum(ammount), 
       Datename (weekday, sale_date) 
FROM   sales 
WHERE  sale_date > '2012-01-01 00:00:00' 
       AND Datepart(hh, sale_date) >= 13 
GROUP  BY Datename (weekday, sale_date) 

Upvotes: 1

Andomar
Andomar

Reputation: 238068

You could add a condition for "after 13:00" to the where clause:

where   sale_date > '2012-01-01 00:00:00'
        and datepart(hour, sale_date) >= 13

Upvotes: 2

Related Questions