0
SELECT "Name""Month","Year","Value" 
from Table
WHERE 
    "Name" LIKE '%JERRY%'
AND "Year" = 
    (SELECT MAX("Year") FROM Table where "Name" LIKE '%JERRY%')
AND "Month"= 
    (SELECT MAX("Month") FROM Table 
     where 
        "Name" LIKE '%JERRY%' 
    AND "Year"= (SELECT MAX("Year") FROM Table where "Name" LIKE '%JERRY%'))

表 -->

Name  | Year | Month | Value
-----------------------------
JERRY   2012    9        100           
JERRY   2012    9        120         
JERRY   2012    9        130           
JERRY   2012    8         20        
JERRY   2011    12        50           

所以我想要前三行作为输出。至于最近一年的最近一个月,我需要所有的值。有人可以建议一个更好的更清洁的查询吗?

4

4 回答 4

2

如果表中还有其他人,那么您可能想要使用排名功能:类似于:


DECLARE @sample TABLE (
      [Name] VARCHAR(255)
    , [Year] INT
    , [Month] INT
    , [Value] INT
    )

INSERT @sample ([Name], [Year], [Month], [Value])
VALUES ('JERRY', 2012, 9, 100)           
     , ('JERRY', 2012, 9, 120)
     , ('JERRY', 2012, 9, 130)
     , ('JERRY', 2012, 8, 20)
     , ('JERRY', 2011, 12, 50)
     , ('FRED', 2011, 12, 50)
     , ('FRED', 2011, 12, 120)
     , ('FRED', 2011, 7, 150)

SELECT *
FROM (
    SELECT *
         , RANK() OVER (PARTITION BY [Name] ORDER BY [Year] DESC, [Month] DESC) AS [rnk]
    FROM @sample
    )
    AS samp
WHERE
    samp.[rnk] = 1

这给出了结果:

名称 年 月 值 rnk
------------- ----------- ----------- --- -------- ------
弗雷德 2011 12 50 1
弗雷德 2011 12 120 1
杰瑞 2012 9 100 1
杰瑞 2012 9 120 1
杰瑞 2012 9 130 1
于 2012-10-25T11:39:16.293 回答
1
select * from @t where
[Year] = (select max([year]) from @t) and 
[Month] = (select max([Month]) from @t where [Year]=(select max([year]) from @t))
于 2012-10-25T11:22:54.970 回答
1
DECLARE @t Table(Name Varchar(30),[Year] Int, [Month] Int,Value Int)
Insert Into @t Values('JERRY' ,  2012,    9,        100 )
Insert Into @t Values('JERRY',   2012,    9 ,       120)         
Insert Into @t Values('JERRY' ,  2012,    9 ,       130)           
Insert Into @t Values('JERRY',   2012 ,   8 ,        20)        
Insert Into @t Values('JERRY',   2011,    12 ,       50) 


Declare @LatestYr Int 
Declare @LatestMonth Int

Select @LatestYr= Max([Year])From @t
Select @LatestMonth = Max([Month]) From @t Where [Year] = @LatestYr

Select * From @t
Where ([Year] = @LatestYr And [Month] = @LatestMonth)

结果

在此处输入图像描述

上述查询仅适用于单个用户。并且对于多个用户将失败,或者在平局的情况下。例如,考虑以下场景

在此处输入图像描述

在这种情况下,所需的输出将是

在此处输入图像描述

因此,为了处理这种情况,我提出以下解决方案

解决方案 1

Select t.* 
From @t t
Join
(
    Select x.Name,x.Max_Year,y.Max_Month
    From
        (   SELECT Name,Max_Year = Max([Year])
            From @t
            Group By Name
        )x
    Join
        (   SELECT Name,[Year],Max_Month= Max([Month])
            From @t
            Group By Name,[Year]
        )y On x.Name = y.Name And x.Max_Year = y.[Year]
)x
On t.Name = x.Name 
And t.[Year] = x.Max_Year 
And t.[Month] = x.Max_Month

或者

解决方案 2 (Sql Server 2005+)

Select Name,[Year],[Month],Value
From
(
    Select *,Rn = Rank() Over(Partition By Name Order By [Year] desc, [Month] Desc) 
    From @t
)X Where X.Rn =1

解决方案 3(Sql Server 2005+)

Select Name,[Year],[Month],Value
From
(
    Select *,Rn = Dense_Rank() Over(Partition By Name Order By [Year] desc, [Month] Desc) 
    From @t
)X Where X.Rn =1

ddl如下

DECLARE @t Table(Name Varchar(30),[Year] Int, [Month] Int,Value Int)
Insert Into @t Values('JERRY' ,  2012,    9,        100 )
Insert Into @t Values('JERRY',   2012,    9 ,       120)         
Insert Into @t Values('JERRY' ,  2012,    9 ,       130)           
Insert Into @t Values('JERRY',   2012 ,   8 ,        20)        
Insert Into @t Values('JERRY',   2011,    12 ,       50) 
Insert Into @t Values('FERRY' ,  2010,    9,        100 )
Insert Into @t Values('FERRY',   2010,    9 ,       120) 
Insert Into @t Values('FERRY',   2010,    8 ,       120) 
Insert Into @t Values('JERRY1' ,  2012,    9,        100 )
Insert Into @t Values('JERRY1',   2012,    9 ,       120)         
Insert Into @t Values('JERRY1' ,  2012,    9 ,       130)           
Insert Into @t Values('JERRY1',   2012 ,   8 ,        20)        
Insert Into @t Values('JERRY1',   2011,    12 ,       50)

希望这可能会有所帮助。谢谢

于 2012-10-25T11:23:09.257 回答
0
select * 
from    your_table
where   "Name" LIKE '%JERRY%' 
and     year*100+month 

in(
    select top 1 year*100+month 
    from your_table
    where 
    "Name" LIKE '%JERRY%' 
    order by cast(year*100+month as int) desc)


点击演示

于 2012-10-26T02:38:44.553 回答