I need to show data between time 06:00 - 18:00 and 18:00 - 06:00 at same time
this is sample Query I M Using. I can not retrieve second group data. How to do?
like 1, 2, 3 comes in 06:00 - 18:00 range
and 4, 5, 6 comes in 18:00 - 06:00 range
SELECT COUNT(cp.comm_pend_id) AS comm_pend_id, cp.UserID, um.Username, CONVERT(varchar, cp.submitted_date, 101) AS Date, SUM(cp.Earning)
AS Earning, SUM(cp.total_commission) AS total_commission
FROM dbo.comm_pending AS cp INNER JOIN
dbo.user_master AS um ON cp.UserID = um.UserID
GROUP BY cp.UserID, CONVERT(varchar, cp.submitted_date, 101), um.Username, cp.PaidStatus, CONVERT(varchar(10), cp.submitted_date, 108)
HAVING (cp.PaidStatus = 'unpaid') AND (CONVERT(varchar(10), cp.s开发者_运维知识库ubmitted_date, 108) BETWEEN '06:00:00' AND '18:00:00')
ORDER BY cp.UserID
You cannot retrieve the second group because 18:00 is greater than 06:00, so there's nothing between them in the same day. For the second group you should filter like this:
((CONVERT(varchar(10), cp.submitted_date, 108) < '06:00:00'
OR
(CONVERT(varchar(10), cp.submitted_date, 108) > '18:00:00')
A CASE statement might be able to help. For example:
create table #times (num int, dt datetime)
insert into #times values (1,'2011-4-6 0:00:00')
insert into #times values (2,'2011-4-6 4:00:00')
insert into #times values (3,'2011-4-6 8:00:00')
insert into #times values (4,'2011-4-6 12:00:00')
insert into #times values (5,'2011-4-6 16:00:00')
insert into #times values (6,'2011-4-6 20:00:00')
select
sum(case when (datepart(hh,dt) between 6 and 17) then num else 0 end) as daySum,
sum(case when (datepart(hh,dt) not between 6 and 17) then num else 0 end) as nightSum
from #times
精彩评论