1

このデータを含むテーブルがあります:

ID   voting_ID   username   timestamp              XMLBallot
1    9           voter01    23. 4. 2012 8:54:45    xmldata
2    9           voter01    21. 4. 2012 14:00:34   xmldata
3    9           voter02    20. 4. 2012 16:01:10   xmldata
4    11          voter01    23. 4. 2012 8:40:45    xmldata
5    9           voter03    19. 4. 2012 21:18:49   xmldata

特定の投票者 (ユーザー名) ごとに最新の投票用紙を 1 つだけ取得する必要がありますvoting_ID

たとえば、返される必要があるデータ@voting_ID=9

ID   voting_ID   username   timestamp              XMLBallot
1    9           voter01    23. 4. 2012 8:54:45    xmldata
3    9           voter02    20. 4. 2012 16:01:10   xmldata
5    9           voter03    19. 4. 2012 21:18:49   xmldata

そのSQL Server 2008クエリの作成を手伝ってください..ありがとうPS:テーブル名はballots

4

4 に答える 4

5

ここにはいくつかのオプションがありますが、ROW_NUMBERグループ化userおよびソート (降順)を追加するとtimestamp、最新のレコードを簡単に選択できます。

ROW_NUMBER の使用

SELECT *
FROM   (
         SELECT ID, voting_ID, username, timestamp, XMLBallot
                , rn = ROW_NUMBER() OVER (PARTITION BY voting_ID, username ORDER BY timestamp DESC)
         FROM   Ballots
       ) bt 
WHERE  rn = 1

または、ユーザーごとの最大タイムスタンプを選択して、それに参加することもできます。

MAX の使用

SELECT bt.ID, bt.voting_ID, bt.username, bt.timestamp, bt.XMLBallot
FROM   Ballots bt
       INNER JOIN (
          SELECT username, voting_ID, timestamp = MAX(timestamp)
          FROM   Ballots
          GROUP BY
                 username, voting_ID
        ) btm ON btm.username = bt.Username
                 AND btm.voting_ID = bt.voting_ID
                 AND btm.timestamp = bt.timestamp
于 2012-04-23T08:27:48.720 に答える
1

それを行う別の方法。

WITH sub 
     AS (SELECT *, 
                Max(timestamp) OVER (partition BY company) AS latest 
         FROM   ballots) 
SELECT * 
FROM   sub 
WHERE  latest = timestamp 
于 2015-07-19T13:42:56.887 に答える
1

多分このようなもの:

テストデータ

DECLARE @T TABLE
    (
        ID INT,
        voting_ID int, 
        username VARCHAR(100),
        timestamp VARCHAR(100),
        XMLBallot VARCHAR(100)
    )

INSERT INTO @T
VALUES
    (1,9,'voter01','23. 4. 2012 8:54:45','xmldata'),
    (2,9,'voter01','21. 4. 2012 14:00:34','xmldata'),
    (3,9,'voter02','20. 4. 2012 16:01:10','xmldata'),
    (4,11,'voter01','23. 4. 2012 8:40:45','xmldata'),
    (5,9,'voter03','19. 4. 2012 21:18:49','xmldata')

クエリ

DECLARE @voting_ID INT=9
;WITH CTE
AS
(   
    SELECT
        ROW_NUMBER() OVER(PARTITION BY username ORDER BY voting_ID) AS RowNbr,
        t.*
    FROM
        @T AS t
)
SELECT
    *
FROM
    CTE
WHERE
    RowNbr=1
    AND CTE.voting_ID=@voting_ID

編集1

多分このようなもの:

DECLARE @voting_ID INT=9
;WITH CTE
AS
(   
    SELECT
        ROW_NUMBER() OVER(PARTITION BY username ORDER BY timestamp DESC) AS RowNbr,
        t.*
    FROM
        @T AS t
    WHERE
        t.voting_ID=@voting_ID
)
SELECT
    *
FROM
    CTE
WHERE
    RowNbr=1
于 2012-04-23T08:31:33.067 に答える
-1
select  t1_4.ID, t1_4.voting_id, t1_4.timestamp, t1_4.xmlballot
from   table1 as t1_4
where   t1_4.ID in (  SELECT  id_to_select
            from   (  SELECT  (  SELECT t1_1.ID
                        from   table1 as t1_1 
                        where t1_1.id =  (    SELECT  top 1 t1_2.ID
                                  from     table1 as t1_2
                                  where   t1_2.username = t1_3.username
                                  order by timestamp desc)
                      ) as id_to_select, 
                      t1_3.username
                  from   table1 as t1_3
                  group by username)
          )
于 2012-04-23T08:47:43.757 に答える