[英]Working days SQL query
I have a problem I can't get my head around. 我有一个问题,我无法解决。
I have a table of events and a table of users. 我有一个事件表和一个用户表。 Some events are chargeable and some are not. 有些事件收费,有些则不收费。 What I want to establish is the number chargeable days per week month and year for every user. 我要确定的是每个用户每个月每个月要收取的天数。
Here is the data... 这是数据...
ID FromDate ToDate isChargeable Username
1 2014-11-03 2014-11-04 Y AUser
2 2014-11-04 2014-11-06 Y AUser
3 2014-11-07 2014-11-07 Y AUser
And I've written a basic query to calculate the difference between the from and to dates and sum them adding 1 to the total as the toDate counts as a full day. 而且我编写了一个基本查询来计算起始日期和截止日期之间的差异,并对它们进行求和,然后将总计加1,以将toDate视为一整天。
SELECT DISTINCT FromDate, ToDate, isChargeable,Username, DATEDIFF(day, FromDate, ToDate) + 1) AS 'count1'
FROM dbo.vDiary
GROUP BY FromDate, ToDate, isChargeable,Username
This results in... 这导致...
FromDate ToDate isChargeable Username count1
2014-11-03 2014-11-04 Y AUser 2
2014-11-04 2014-11-06 Y AUser 3
2014-11-07 2014-11-07 Y AUser 1
Which is incorrect as it's showing 6 working days because two events are overlapping. 这是不正确的,因为它显示6个工作日,因为两个事件重叠。
How can I allow for this overlapping? 如何允许这种重叠? I need to state that if there is an event on a particular day that is chargeable then that day is a chargeable day. 我需要声明,如果某个特定日期有活动需要收费,那么该天就是应收费的日子。
I think I need a calendar table but I'm not sure on the process of using it to get the right results. 我认为我需要一个日历表,但是我不确定使用它来获取正确结果的过程。
Any help would be really appreciated!! 任何帮助将非常感激!!
Thanks 谢谢
Steve 史蒂夫
EDIT: Trying solution from below 编辑:从下面尝试解决方案
;WITH cte
AS (SELECT Row_number()OVER(PARTITION BY Username ORDER BY FromDate) rn,
FromDate,ToDate,isChargeable,Username
FROM #calen)
SELECT a.FromDate,a.ToDate,a.isChargeable,a.Username,
CASE
WHEN a.FromDate > b.ToDate
OR b.ToDate IS NULL THEN Datediff(day, a.FromDate, a.ToDate) + 1
ELSE Datediff(day, Dateadd(dd, 1, b.ToDate), a.ToDate)+ 1
END DatDiff,
*
FROM cte a
LEFT JOIN cte b
ON a.rn = b.rn + 1
AND a.username = b.username
WHERE ( a.ToDate > b.todate
OR b.ToDate IS NULL )
Produces (updated) 产生(更新)
FromDate ToDate isChargeable Username DatDiff rn FromDate ToDate isChargeable Username rn FromDate ToDate isChargeable Username
2014-11-03 2014-11-04 Y AUser 2 1 2014-11-03 2014-11-04 Y AUser NULL NULL NULL NULL NULL
2014-11-04 2014-11-06 Y AUser 2 2 2014-11-04 2014-11-06 Y AUser 1 2014-11-03 2014-11-04 Y AUser
2014-11-07 2014-11-07 Y AUser 1 3 2014-11-07 2014-11-07 Y AUser 2 2014-11-04 2014-11-06 Y AUser
2014-12-03 2014-12-15 Y AUser 13 4 2014-12-03 2014-12-15 Y AUser 3 2014-11-07 2014-11-07 Y AUser
2014-12-10 2014-12-17 Y AUser 2 5 2014-12-10 2014-12-17 Y AUser 4 2014-12-03 2014-12-15 Y AUser
2015-12-04 2015-12-15 Y AUser 12 6 2015-12-04 2015-12-15 Y AUser 5 2014-12-10 2014-12-17 Y AUser
2014-11-03 2014-11-03 Y BUser 1 1 2014-11-03 2014-11-03 Y BUser NULL NULL NULL NULL NULL
2014-11-04 2014-11-04 Y BUser 1 2 2014-11-04 2014-11-04 Y BUser 1 2014-11-03 2014-11-03 Y BUser
Try this. 尝试这个。
If you have Unique ID's try this. 如果您有唯一ID,请尝试此操作。
create table #calen(ID int, FromDate date, ToDate date,isChargeable char(1),Username varchar(20))
INSERT #calen
VALUES (1,'2014-11-03','2014-11-04','Y ','AUser'),
(2,'2014-11-04','2014-11-06','Y','AUser'),
(3,'2014-11-07','2014-11-07','Y','AUser')
SELECT a.FromDate,a.ToDate,a.isChargeable,a.Username,
CASE
WHEN a.FromDate > b.ToDate
OR b.ToDate IS NULL THEN Datediff(day, a.FromDate, a.ToDate) + 1
ELSE Datediff(day, Dateadd(dd, 1, b.ToDate), a.ToDate)
+ 1
END DatDiff
FROM #calen a
LEFT JOIN #calen b
ON a.id = b.id + 1
WHERE ( a.ToDate > b.todate
OR b.ToDate IS NULL )
Update : 更新:
For more than one user. 对于多个用户。
;WITH cte
AS (SELECT Row_number()OVER(PARTITION BY Username ORDER BY FromDate) rn,
FromDate,ToDate,isChargeable,Username
FROM #calen)
SELECT a.FromDate,a.ToDate,a.isChargeable,a.Username,
CASE
WHEN a.FromDate > b.ToDate
OR b.ToDate IS NULL THEN Datediff(day, a.FromDate, a.ToDate) + 1
ELSE Datediff(day, Dateadd(dd, 1, b.ToDate), a.ToDate)+ 1
END DatDiff
FROM cte a
LEFT JOIN cte b
ON a.rn = b.rn + 1
AND a.username = b.username
WHERE ( a.ToDate > b.todate
OR b.ToDate IS NULL )
OUTPUT : 输出:
FromDate ToDate isChargeable Username DatDiff
---------- ---------- ------------ -------- -------
2014-11-03 2014-11-04 Y AUser 2
2014-11-04 2014-11-06 Y AUser 2
2014-11-07 2014-11-07 Y AUser 1
If all the ID values are not unique we can have the below query: 如果所有ID值都不都是唯一的,我们可以进行以下查询:
SELECT ID, FromDate, ToDate, isChargeable, Username,
CASE WHEN EXISTS(SELECT ToDate FROM UserTable B WHERE A.FromDate = B.ToDate
AND A.UserName = B.UserName AND A.ID <> B.ID) THEN
DATEDIFF(day, FromDate, ToDate)
ELSE
DATEDIFF(day, FromDate, ToDate) + 1
END AS 'count1'
FROM UserTable A
else we have to create CTE to create unique ROWID. 否则,我们必须创建CTE才能创建唯一的ROWID。
;WITH UserTable
AS (SELECT Row_number() OVER(ORDER BY FromDate) RowID,
FromDate,ToDate, isChargeable, Username
FROM #user)
SELECT RowID, FromDate, ToDate, isChargeable, Username,
CASE WHEN EXISTS(SELECT ToDate FROM UserTable B WHERE A.FromDate = B.ToDate
AND A.UserName = B.UserName AND A.RowID <> B.RowID) THEN
DATEDIFF(day, FromDate, ToDate)
ELSE
DATEDIFF(day, FromDate, ToDate) + 1
END AS 'count1'
FROM UserTable A
try this : 尝试这个 :
WITH CTE AS
(SELECT a.* ,y.flag
FROM EventsTable a
OUTER APPLY
(SELECT DATEDIFF(dd, a.ToDate, x.FromDate) as flag
FROM EventsTable x
WHERE a.id = x.id AND x.FromDate > a.ToDate) y)
SELECT id,FromDate,ToDate,isChargeable,Username,
CASE WHEN flag = 1 THEN
DATEDIFF(dd, FromDate, ToDate)
ELSE DATEDIFF(dd, FromDate,ToDate) + 1
END AS count1
FROM CTE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.