使用布尔条件计算具有条件 sql 的月份
using a boolean condition count the months that have the condition sql
所以我有一个 table 这样的:
---id---datetime---month---active
1 2016-12-01 Dec-16 0
1 2016-12-02 Dec-16 1
1 2017-01-28 Jan-17 1
1 2017-02-03 Feb-17 0
1 2017-02-24 Feb-17 0
1 2017-03-05 Mar-17 0
1 2017-03-24 Mar-17 1
1 2017-04-02 Apr-17 1
1 2017-04-25 Apr-17 1
1 2017-05-02 May-17 1
1 2017-05-28 May-17 0
我想要这样的结果:
---id---monthCount---Active
1 1 0
1 2 1
1 2 0
1 3 1
1 1 0
table确实有超过1个id
。
现在我只是使用分区获取最小日期和最大日期之间的差异,并按 id
、active
排序并按 datetime
排序,但这给了我从它第一次变为 1 到它最终变为 0 之间的月份。我希望它通过 active
.
的每次更改详细分开
我怎样才能做到这一点?
您可以使用行号的差异来识别组(这是一个间隙和孤岛问题)。那么最终的解决方案需要count(distinct)
:
select id, count(distinct month) as monthcount, active
from (select t.*,
row_number() over (partition by id order by month, active) as seqnum_i,
row_number() over (partition by id, active order by month) as seqnum_ia
from t
) t
group by id, active, (seqnum_i - seqnum_ia);
这是假设数据按 id、月份、活动排序 -- 这是问题中数据的排序。最好有一个单独的列来明确定义行的排序顺序。
在 sql 服务器 2012+ 中,您可以像这样使用 LAG
DECLARE @SampleData AS TABLE
(
id int,
[datetime] datetime,
active bit
)
;WITH temp AS
(
SELECT sd.id,
sd.active,
sd.[datetime],
lag(sd.active) over(PARTITION BY sd.id ORDER BY sd.datetime) AS previousActive,
lag(sd.id) over( ORDER BY sd.id, sd.datetime) AS previousId
FROM @SampleData sd
)
,temp1 AS
(
SELECT *,
sum(CASE WHEN t.previousActive IS NULL OR t.previousActive != t.active OR t.id != t.previousId THEN 1
ELSE 0 END)
OVER(PARTITION BY t.id ORDER BY t.[datetime]) AS groupid
FROM temp t
)
SELECT t.id, count(DISTINCT month(t.datetime)) AS monthCount, t.active
FROM temp1 t
GROUP BY id, t.groupid, t.active
演示 link:http://rextester.com/SJZ16279
所以我有一个 table 这样的:
---id---datetime---month---active
1 2016-12-01 Dec-16 0
1 2016-12-02 Dec-16 1
1 2017-01-28 Jan-17 1
1 2017-02-03 Feb-17 0
1 2017-02-24 Feb-17 0
1 2017-03-05 Mar-17 0
1 2017-03-24 Mar-17 1
1 2017-04-02 Apr-17 1
1 2017-04-25 Apr-17 1
1 2017-05-02 May-17 1
1 2017-05-28 May-17 0
我想要这样的结果:
---id---monthCount---Active
1 1 0
1 2 1
1 2 0
1 3 1
1 1 0
table确实有超过1个id
。
现在我只是使用分区获取最小日期和最大日期之间的差异,并按 id
、active
排序并按 datetime
排序,但这给了我从它第一次变为 1 到它最终变为 0 之间的月份。我希望它通过 active
.
我怎样才能做到这一点?
您可以使用行号的差异来识别组(这是一个间隙和孤岛问题)。那么最终的解决方案需要count(distinct)
:
select id, count(distinct month) as monthcount, active
from (select t.*,
row_number() over (partition by id order by month, active) as seqnum_i,
row_number() over (partition by id, active order by month) as seqnum_ia
from t
) t
group by id, active, (seqnum_i - seqnum_ia);
这是假设数据按 id、月份、活动排序 -- 这是问题中数据的排序。最好有一个单独的列来明确定义行的排序顺序。
在 sql 服务器 2012+ 中,您可以像这样使用 LAG
DECLARE @SampleData AS TABLE
(
id int,
[datetime] datetime,
active bit
)
;WITH temp AS
(
SELECT sd.id,
sd.active,
sd.[datetime],
lag(sd.active) over(PARTITION BY sd.id ORDER BY sd.datetime) AS previousActive,
lag(sd.id) over( ORDER BY sd.id, sd.datetime) AS previousId
FROM @SampleData sd
)
,temp1 AS
(
SELECT *,
sum(CASE WHEN t.previousActive IS NULL OR t.previousActive != t.active OR t.id != t.previousId THEN 1
ELSE 0 END)
OVER(PARTITION BY t.id ORDER BY t.[datetime]) AS groupid
FROM temp t
)
SELECT t.id, count(DISTINCT month(t.datetime)) AS monthCount, t.active
FROM temp1 t
GROUP BY id, t.groupid, t.active
演示 link:http://rextester.com/SJZ16279