SQL Server:按组

时间:2018-03-21 02:07:25

标签: sql-server tsql max

我有一个包含以下列的表: item(varchar) 日期(日期) 修订(日期时间) 值(数字)

我有这个SQL语句,它找到每个项目的最新修订版本:

select item, max(revision) as 'last_revision' 
from [mytable] 
group by item 
ORDER BY last_revision ASC

但是我希望得到每个项目的最后2个版本:

项| last_revision | previous_revision

我已经尝试过这个惨遭失败

select TOP 2 item, revision as 'last_revision' 
from [myTable] 
group by item 
ORDER BY last_revision ASC
  

专栏' myTable.revision'在选择列表中无效,因为它是   不包含在聚合函数或GROUP BY子句中。

3 个答案:

答案 0 :(得分:1)

尝试此查询。他们两个都应该工作

        <!-- Method 1 -->
        <!-- Copy dependency libraries jar files to a separated LIB folder -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <excludeTransitive>false</excludeTransitive> 
                <stripVersion>false</stripVersion>
            </configuration>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Add LIB folder to classPath -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>


        <!-- Method 2 -->
        <!-- Package all libraries classes into one runnable jar -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
        </plugin>            

或者

select
    item, revision
from (
    select 
        item, revision
        , rn = row_number() over (partition by item order by revision desc)
    from [mytable] 
    group by item , revision
) t
where rn <= 2

答案 1 :(得分:0)

使用ROW_NUMBER

SELECT item, revision AS last_revision
FROM
(
    SELECT item, revision,
        ROW_NUMBER() OVER (PARTITION BY item ORDER BY revision) rn
    FROM [myTable]
) t
WHERE rn <= 2;

SQL Server的TOP运算符仅限制为整个结果集返回的记录数,而不是聚合中的每个组。你真的应该在这里使用分析函数。

答案 2 :(得分:0)

您似乎希望top (4)ties

select top (4) with ties item, revision  
from mytable t
order by row_number() over (partition by item order by revision desc)