【问题标题】:SQL Statement(s)SQL 语句
【发布时间】:2009-09-29 00:44:02
【问题描述】:

如果我有下表:

CREATE TABLE #temp (
    id int,
    num int,
    question varchar(50),
    qversion int );

INSERT INTO #temp VALUES(1, 1, 'Question 1 v1', 1);
INSERT INTO #temp VALUES(2, 1, 'Question 1 v2', 2);
INSERT INTO #temp VALUES(3, 2, 'Question 2 v1', 1);
INSERT INTO #temp VALUES(4, 2, 'Question 2 v2', 2);
INSERT INTO #temp VALUES(5, 2, 'Question 2 v3', 3);
INSERT INTO #temp VALUES(6, 3, 'Question 3 v1', 1);

SELECT *
FROM #temp;

DROP TABLE #temp;

我想要一张表格来显示最新版本的三个问题?这是在 SQL Server 2005 中

【问题讨论】:

  • +1 用于表创建和插入语句。但是,如果我正确理解了这个问题,似乎应该返回给定测试数据中的所有行。我建议包括将被正确查询排除的行。

标签: sql sql-server sql-server-2005 tsql greatest-n-per-group


【解决方案1】:
CREATE TABLE #temp (
    id int,
    num int,
    question varchar(50),
    qversion int );

INSERT INTO #temp VALUES(1, 1, 'Question 1 v1', 1);
INSERT INTO #temp VALUES(2, 1, 'Question 1 v2', 2);
INSERT INTO #temp VALUES(3, 2, 'Question 2 v1', 1);
INSERT INTO #temp VALUES(4, 2, 'Question 2 v2', 2);
INSERT INTO #temp VALUES(5, 2, 'Question 2 v3', 3);
INSERT INTO #temp VALUES(6, 3, 'Question 3 v1', 1);

WITH latest AS (
   SELECT num, MAX(qversion) AS qversion
   FROM #temp
   GROUP BY num
)
SELECT #temp.*
FROM #temp
INNER JOIN latest
    ON latest.num = #temp.num
    AND latest.qversion = #temp.qversion;

DROP TABLE #temp;

【讨论】:

  • 就是这样,但是子查询中有一个额外的')',并且num需要在group by中。 (无法编辑)
【解决方案2】:
SELECT t1.id, t1.num, t1.question, t1.qversion
FROM #temp t1
LEFT OUTER JOIN #temp t2
  ON (t1.num = t2.num AND t1.qversion < t2.qversion)
GROUP BY t1.id, t1.num, t1.question, t1.qversion
HAVING COUNT(*) < 3;

【讨论】:

    【解决方案3】:

    您使用的是 SQL Server 2005,因此至少值得探索一下 over 子句:

    select
        *
    from
        (select *, max(qversion) over (partition by num) as maxVersion from #temp) s
    where
        s.qversion = s.maxVersion
    

    【讨论】:

      【解决方案4】:

      我想要一个表格来显示每个问题的三个最新版本

      1. 假设该 qversion 随时间增加。如果这个假设是倒退的,请从答案中删除 desc 关键字。
      2. 表定义对 qversion 没有明确的非空约束。我假设应该排除空 qversion。 (注意:根据设置,声明中缺少明确的 null/not null 可能会导致 not null 约束。)如果表确实有 not null 约束,则应删除文本 where qversion is not null。如果 qversion 可以为 null,并且结果集中需要包含 null,则需要进行其他更改。

      CREATE TABLE #temp (
          id int,
          num int,
          question varchar(50),
          qversion int );
      
      INSERT INTO #temp VALUES(1, 1, 'Question 1 v1', 1);
      INSERT INTO #temp VALUES(2, 1, 'Question 1 v2', 2);
      INSERT INTO #temp VALUES(3, 2, 'Question 2 v1', 1);
      INSERT INTO #temp VALUES(4, 2, 'Question 2 v2', 2);
      INSERT INTO #temp VALUES(5, 2, 'Question 2 v3', 3);
      INSERT INTO #temp VALUES(7, 2, 'Question 2 v4', 4); 
      -- ^^ Added so at least one row would be excluded.
      INSERT INTO #temp VALUES(6, 3, 'Question 3 v1', 1);
      INSERT INTO #temp VALUES(8, 4, 'Question 4 v?', null);
      
      select id, num, question, qversion
      from (select *, 
              row_number() over (partition by num order by qversion desc) as RN
          from #temp
          where qversion is not null) T
      where RN <= 3
      

      【讨论】:

        猜你喜欢
        • 2016-03-29
        • 2014-12-05
        • 2013-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多