Click here to Skip to main content
15,888,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I don't know how to get orders foreach hour how many orders are placed in a perticualar date.I mean for example

Date=2012-08-07,
6AM=20,7AM=30,8AM=40,9AM=0(if no orders are placed at 9AM),.......up to 20PM like.

please give me one sample query.

Thanks&Regards,
Raghu.
Posted
Comments
Kenneth Haugland 8-Aug-12 1:11am    
Use Date time < and > and you should have it...

1 solution

suppose Order_tbl have some data as given below
CSS
Name   orderdt                   ordertm   
-----------------------------------------------------
aaaa   2012-08-08 00:00:00.000   2012-08-08 10:54:15.053
bbbb   2012-08-08 00:00:00.000   2012-08-08 10:54:17.270
cccc   2012-08-08 00:00:00.000   2012-08-08 10:54:18.727
dddd   2012-08-08 00:00:00.000   2012-08-08 10:54:19.117
eeee   2012-08-08 00:00:00.000   2012-08-08 11:54:21.000
ffff   2012-08-08 00:00:00.000   2012-08-08 11:55:21.000
gggg   2012-08-08 00:00:00.000   2012-08-08 10:55:21.663
hhhh   2012-08-08 00:00:00.000   2012-08-08 10:55:21.957


now, check below query for this table

SQL
select case when datepart(hour,ordertm)>12 then convert(varchar,datepart(hour,ordertm)-12) +' PM' else convert(varchar,datepart(hour,ordertm)) +' AM' end  as hours,
count(*) as Total_orders from Order_tbl
where (orderdt)='2012-08-08' group by datepart(hour,ordertm)


result for date '2012-08-08'
CSS
Hours   Total_Orders
-------------------------
10 AM	6
11 AM	2


Happy Coding!
:)
 
Share this answer
 
v2
Comments
Madhugundi 8-Aug-12 2:55am    
hi,
i executed my query based on your query but i am getting zero rows.
my query look like is
DECLARE @Sql NVARCHAR(300);
SET @Sql ='select case when cast(SUBSTRING(ShipDate,11, 3)as varchar)>12 then cast(SUBSTRING(ShipDate,11, 3)as varchar)-12 else cast(SUBSTRING(ShipDate,11, 3)as varchar) end as hours,
count(Id) as Total_orders from ShipmentDetail where (ShipDate)=''2012-07-26''group by cast(SUBSTRING(ShipDate,11, 3)as varchar)' ;

EXEC(@Sql);
print @Sql;
Aarti Meswania 8-Aug-12 3:09am    
shipdate & shiptime should be in different columns.
you can not compare date-time field to date only.
try this,

select case when datepart(hour,ordertm)>12 then convert(varchar,datepart(hour,ordertm)-12) +' PM' else convert(varchar,datepart(hour,ordertm)) +' AM' end as hours,
count(*) as Total_orders from
(select id,convert(Datetime, CONVERT(VARCHAR(10),ordertm,105),105) as shipdate) as Orderdt, shipdt as orderTm from ShipmentDetail ) as a
where (orderdt)='2012-08-08' group by datepart(hour,ordertm)
Madhugundi 8-Aug-12 3:45am    
HI,
If u don't mind give me more clarity on this
in my table shipdate is in nvarchar type does here datepart() will work if it is how to convert nvarchart to date to get hour and also your query will written for each hour or only the orders existing hours.i am trying to get orders for every hour if orders are not there on a paticular hour it will return 0 on result.

please help me,

Thanks&Regards,
Raghu.
Aarti Meswania 8-Aug-12 3:47am    
please copy paste any value of shipdate field.
Madhugundi 8-Aug-12 3:51am    
ShipDate Column Value is like: 2012-07-25 15:54:32 PDT

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900