1

c_user_idSQL Server 2008 データベースに連続してエントリを作成したユーザー を選択するにはどうすればよいですか?

できれば以下のように連続して3回以上エントリーした人(とはID欄でpras.chla@gmail.comソート)c_id descc_id

c_id    c_user_id             c_entry
1427    xermadr.asdf@me.com   155575
1426    pras.chla@gmail.com   155829
1425    pras.chla@gmail.com   155826
1424    pras.chla@gmail.com   155828
1423    pras.chla@gmail.com   155830
1422    sdfe.qqol@gmail.com   155559 

再度、感謝します ?

4

2 に答える 2

4

一方通行

SELECT DISTINCT c_user_id
FROM   tab t1
       CROSS APPLY (SELECT 1 AS C
                    FROM   (SELECT TOP 2 *
                            FROM   tab t2
                            WHERE  t2.c_id < t1.c_id
                            ORDER  BY t2.c_id DESC) T 
HAVING COUNT(c_user_id) = 2 AND COUNT(DISTINCT c_user_id) = 1 AND MIN(c_user_id) = t1.c_user_id) CA

または別の

WITH T AS
(
SELECT *,
       ROW_NUMBER() OVER (order by c_id) -
       ROW_NUMBER() OVER (PARTITION BY c_user_id order by c_id) AS Grp
FROM   tab t1
)
SELECT DISTINCT c_user_id
FROM T
GROUP BY c_user_id, Grp
HAVING COUNT(*) >=3
于 2012-09-28T17:42:10.340 に答える
1
;WITH someUserTableWithOrderNumber as
(
SELECT ROW_NUMBER ( )  OVER (order by c_id) OrderNumber, 
    c_id, 
    c_user_id, 
    c_entry 
FROM someUserTable
)
SELECT DISTINCT a.c_user_id
FROM someUserTableWithOrderNumber a
JOIN someUserTableWithOrderNumber b on a.OrderNumber = b.OrderNumber + 1 AND a.c_user_id = b.c_user_id 
JOIN someUserTableWithOrderNumber c on b.OrderNumber = c.OrderNumber + 1 AND b.c_user_id = c.c_user_id 
JOIN someUserTableWithOrderNumber d on c.OrderNumber = d.OrderNumber + 1 AND c.c_user_id = d.c_user_id 
于 2012-09-28T17:22:58.473 に答える