从数据表中每年返回一行

时间:2013-09-29 07:24:49

标签: sql sql-server sql-server-2008

我需要根据以下数据仅显示每年的单行。

AlbumID     AlbumIcon                   Year
2       53575dd7-9e6a-4230-afe7-eb34a9952ef5.jpg    2010
6       41c6cefe-aa9a-40af-86bf-0e78784fc9be.jpg    2011
7       20472dc8-abff-4cb1-80a4-ec264a91025e.jpg    2011
9       a2185c73-debf-4058-afee-dc54caf674d2.jpg    2012
10      3e3de287-b966-40cc-8895-9c9e98f1b23d.jpg    2012
11      f0743e3e-26d8-44ea-97ad-a7bd33259550.JPG    2007
12      dafd1242-19d6-4f66-8b23-75143f107743.JPG    2012
13      d623e134-3d69-4b86-86fe-4c5a2ab89086.JPG    2012
14      9c6b981e-e696-4c85-be6d-3b04559a6cd4.JPG    2012
15      b6b867c0-5478-4503-940c-ce75e863bee8.JPG    2012
16      3cc0d762-f1e1-425a-8923-0e39b4ed0eca.jpg    2012

我的表结构就像这样

Album_ID int
Album_Name varchar(100)
Album_Description varchar(500)
Album_Date Datetime
Album_Icon varchar(60)

我正在使用SELECTAlbum_ID, Album_Name,Album_Icon, DATEPART(YYYY,Album_Date) AS Year FROM Album_Name我不知道如何修改查询以获得每年的单行,并且最新的年份行会很棒。

我想在不使用CTE查询的情况下实现这一目标

分组与DISTINCT生成错误

SELECT Album_ID, Album_Name,Album_Icon, DISTINCT(DATEPART(YYYY,Album_Date)) AS Year FROM Album_Name  GROUP BY DATEPART(YYYY,Album_Date) , Album_ID, Album_Icon

对此方面的帮助表示赞赏。

解决方案基于巴丹的回复。

SELECT  *
FROM 
(
    SELECT  x.*,  
           --ROW_NUMBER() OVER(PARTITION BY x.[Album_Date] ORDER BY x.Album_ID DESC) AS RowNum
              ROW_NUMBER() OVER(PARTITION BY DATEPART(YYYY,x.[Album_Date]) ORDER BY x.Album_Date DESC) AS RowNum
    FROM    Album_Name x
) src
WHERE src.RowNum = 1

2 个答案:

答案 0 :(得分:1)

SELECT  *
FROM 
(
    SELECT  x.Album_ID, x.Album_Name, ...,  
            ROW_NUMBER() OVER(PARTITION BY x.[Year] ORDER BY x.Album_ID DESC) AS RowNum
            -- or ROW_NUMBER() OVER(PARTITION BY x.[Year] ORDER BY x.Album_Date DESC) AS RowNum
    FROM    dbo.MyTable x
) src
WHERE src.RowNum = 1

编辑#1:

DECLARE @MyTable TABLE
(
    Album_ID int,
    Album_Name varchar(100),
    Album_Description varchar(500),
    Album_Date Datetime,
    [Year] smallint,
    Album_Icon varchar(60)
);

INSERT @MyTable (Album_ID, Album_Icon, [Year])
SELECT 2 , '53575dd7-9e6a-4230-afe7-eb34a9952ef5.jpg', 2010
UNION ALL SELECT  6 , '41c6cefe-aa9a-40af-86bf-0e78784fc9be.jpg', 2011
UNION ALL SELECT  7 , '20472dc8-abff-4cb1-80a4-ec264a91025e.jpg', 2011
UNION ALL SELECT  9 , 'a2185c73-debf-4058-afee-dc54caf674d2.jpg', 2012
UNION ALL SELECT  10, '3e3de287-b966-40cc-8895-9c9e98f1b23d.jpg', 2012
UNION ALL SELECT  11, 'f0743e3e-26d8-44ea-97ad-a7bd33259550.JPG', 2007
UNION ALL SELECT  12, 'dafd1242-19d6-4f66-8b23-75143f107743.JPG', 2012
UNION ALL SELECT  13, 'd623e134-3d69-4b86-86fe-4c5a2ab89086.JPG', 2012
UNION ALL SELECT  14, '9c6b981e-e696-4c85-be6d-3b04559a6cd4.JPG', 2012
UNION ALL SELECT  15, 'b6b867c0-5478-4503-940c-ce75e863bee8.JPG', 2012
UNION ALL SELECT  16, '3cc0d762-f1e1-425a-8923-0e39b4ed0eca.jpg', 2012;

SELECT  *
FROM 
(
    SELECT  x.Album_ID, x.Album_Name, Album_Icon, 
            ROW_NUMBER() OVER(PARTITION BY x.[Year] ORDER BY x.Album_ID DESC) AS RowNum
            -- or ROW_NUMBER() OVER(PARTITION BY x.[Year] ORDER BY x.Album_Date DESC) AS RowNum
    FROM    @MyTable x
) src
WHERE src.RowNum = 1

答案 1 :(得分:0)

当您每年寻找一行时,您可以使用最大(或最小)功能每年获得一个相册ID,如下所示 -

SELECT Album_ID, Album_Name, Album_Icon, DATEPART(YYYY, Album_Date) AS Year
  FROM Album_Name a1
 where album_ID = (select max(a2.album_Id)
                     from album_name a2
                    where a2.album_year = a1.album_year)