gpt4 book ai didi

sql 序列计数

转载 作者:行者123 更新时间:2023-12-04 06:50:30 33 4
gpt4 key购买 nike

我有一张表如下

userid    answer

1 true

1 true

1 false

1 true

1 true

1 true

2 true

1 true

我想获得每个用户的最新真实序列计数

所以我会得到
userid   count

1 4
2 1

请帮忙

最佳答案

WITH Answers
AS
(
SELECT 1 AS xxxid , 1 AS userid, 'true' AS answer UNION ALL
SELECT 2 AS xxxid , 1 AS userid, 'true' AS answer UNION ALL
SELECT 3 AS xxxid , 1 AS userid, 'false' AS answer UNION ALL
SELECT 4 AS xxxid , 1 AS userid, 'true' AS answer UNION ALL
SELECT 5 AS xxxid , 1 AS userid, 'true' AS answer UNION ALL
SELECT 6 AS xxxid , 1 AS userid, 'true' AS answer UNION ALL
SELECT 7 AS xxxid , 2 AS userid, 'true' AS answer UNION ALL
SELECT 8 AS xxxid , 1 AS userid, 'true' AS answer )

SELECT userid,
COUNT(*) AS [COUNT]
FROM Answers A
WHERE NOT EXISTS
(SELECT *
FROM Answers a2
WHERE answer = 'false' /*change this to 0 if using bit datatype*/
AND a2.userid = a.userid
)
OR xxxid >
(SELECT MAX(xxxid)
FROM Answers a2
WHERE answer = 'false' /*change this to 0 if using bit datatype*/
AND a2.userid = a.userid
)
GROUP BY userid

关于sql 序列计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3193322/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com