I have a query as below which will return each Remote server sales for today Result as per below
datebooking NameOfDay ORC CWP TC TotalBooking EmailSent EmailNotSent
20150524 Sunday 447 144 272 1441 1441 0
The datebooking actually i have it trim down to 8 numeric
My question is could I return each remote sales server breakdown by per hour? Imagine one day will be 24 lines of details instead of one line per day That mean under booking date will we in this format 20150524 12am to 1am sales, 2am to 3am so on and so forth? How should I accomplish this query based on my current query?
SELECT
Convert(char(8), OrderH_dtmInitiated, 112)as datebooking,
Datename (weekday, OrderH_dtmInitiated) As NameOfDay,
--count(distinct OrderH_strCinemaId)as Cinemasite,
SUM(case when OrderH_strCinemaId like '1101' then 1 else 0 end)as ORC,
SUM(case when OrderH_strCinemaId like '1102' then 1 else 0 end)as CWP,
SUM(case when OrderH_strCinemaId like '1104' then 1 else 0 end)as TC,
count(distinct OrderH_intID)as TotalBooking,
SUM(case when OrderH_strEmailConfirmationSent like 'Y' then 1 else 0 end) as EmailSent,
SUM(case when OrderH_strEmailConfirmationSent is NULL then 1 else 0 end) as EmailNotSent
FROM [VISTAIT].[dbo].[tblOrderHistory]
WHERE OrderH_dtmInitiated >= (SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE() )))
GROUP BY Convert(char(8), OrderH_dtmInitiated, 112), Datename (weekday, OrderH_dtmInitiated )
ORDER by Convert(char(8), OrderH_dtmInitiated, 112)
If I understand correctly, you would just add the hour to the select
and group by
:
SELECT Convert(char(8), OrderH_dtmInitiated, 112) as datebooking,
datename(hour, OrderH_dtmInitiated) as hourbooking,
. . .
FROM . . .
GRUOP BY Convert(char(8), OrderH_dtmInitiated, 112),
datename(hour, OrderH_dtmInitiated)
This assumes that OrderH_dtmInitiated
actually has the time information.
And alternative method would be to use a date/time format that includes the hour, so it is easy to put into one field:
SELECT Convert(char(13), OrderH_dtmInitiated, 121) as datebooking,
. . .
FROM . . .
GROUP BY Convert(char(13), OrderH_dtmInitiated, 121)
The format would then be "YYYY-MM-DD HH".
EDIT:
If you want am/pm for the hour, probably the easiest way is an explicit case
:
SELECT Convert(char(8), OrderH_dtmInitiated, 112) as datebooking,
(CASE WHEN datename(hour, OrderH_dtmInitiated) < '12'
THEN datename(hour, OrderH_dtmInitiated) + ' AM'
ELSE datename(hour, OrderH_dtmInitiated) + ' PM'
END) as hourbooking,
. . .
FROM . . .
GRUOP BY Convert(char(8), OrderH_dtmInitiated, 112),
datename(hour, OrderH_dtmInitiated)
Note: I wrote the logic using only datename()
quite intentionally. That way, the whole case
statement doesn't need to be in the group by
clause.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.