选择包含分组

时间:2017-04-28 14:30:46

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

例如:

create table #t
(
    [ID] int,
    [Date] date,
    [Name] varchar(5)
)

insert into #t
values
(1, getdate(),'1-1'),
(2, dateadd(D,-10,getdate()),'2-1'),
(2, dateadd(D,-5,getdate()),'2-2'),
(1, dateadd(M,-1,getdate()),'1-2')

select * from #t

我需要为每个[ID]选择[名称],最大[数据]。

这样的事情:

select [1], [2]
  from ( select ID, [Date] from #t ) y
  pivot (
    max(y.[Date])
    for y.ID in ([1],[2])
  ) pvt;

输出:

     1            2
2017-04-28   2017-04-23

,而不是[日期],我想看[名称]

我想要查看的内容

 1          2
1-1        2-2

请帮忙。谢谢。

4 个答案:

答案 0 :(得分:0)

请尝试以下代码

foreach

答案 1 :(得分:0)

您可以将row_nubmber()与日期desc和pivot一起使用,如下所示:

;with cte as (
select id, RowN = row_number() over (partition by id order by date desc), name from #t
) select * from 
(select id, name  from cte where rown = 1 ) s
pivot (max(name) for id in ([1],[2])) p

答案 2 :(得分:0)

您可以尝试以下操作:

SELECT          [1], [2]
FROM            (SELECT         y.ID, 
                                t.Name
                 FROM           (SELECT     ID, 
                                            MAX([Date]) AS [Date]
                                 FROM       #t
                                 GROUP BY   ID ) y
                INNER JOIN      #t t ON y.[Date] = t.[Date]
                ) x
PIVOT
(
    MAX(x.Name)
    FOR     x.ID IN ([1],[2])
) pvt;

你可以在这里看到 - > http://rextester.com/ZGQGSC94965

希望这有帮助!!!

答案 3 :(得分:0)

尝试这个。

                CREATE TABLE #t
                    (
                      [ID] INT ,
                      [Date] DATE ,
                      [Name] VARCHAR(5)
                    )

                INSERT  INTO #t
                VALUES  (1, getdate(),'1-1'),
                (2, dateadd(D,-10,getdate()),'2-1'),
                (2, dateadd(D,-5,getdate()),'2-2'),
                (1, dateadd(M,-1,getdate()),'1-2')

                SELECT  *
                FROM    #t;
                WITH    CTE
                          AS ( SELECT   ID ,
                                        MAX(Date) [Date]
                               FROM     #t
                               GROUP BY ID
                             ),
                        CTE2
                          AS ( SELECT   cte.ID ,
                                        cte.Date ,
                                        t.name
                               FROM     CTE
                                        OUTER APPLY ( SELECT TOP 1
                                                                name
                                                      FROM      #t
                                                      WHERE     (ID = cte.ID AND Date = cte.Date) 
                                                    ) T
                             )
                    SELECT  MAX([1]) [1] ,
                            MAX([2]) [2]
                    FROM    ( SELECT    ID ,
                                        [Date] ,
                                        NAME
                              FROM      CTE2
                            ) y PIVOT ( MAX(y.NAME) FOR y.ID IN ( [1], [2] ) ) pvt

结果

        ID          Date       Name
        ----------- ---------- -----
        1           2017-05-02 1-1
        2           2017-04-22 2-1
        2           2017-04-27 2-2
        1           2017-04-02 1-2

        (4 row(s) affected)

        1     2
        ----- -----
        1-1   2-2


        (1 row(s) affected)