2012-10-25 65 views
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%')) 

表 - >如何在SQL中獲取複合數據的最大值?

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

所以我想排前三作爲輸出。至於最近一年的最新月份,我需要所有的價值。 有人可以建議更清潔的查詢嗎?

+0

對於哪個DBMS是這樣的? –

+0

以上是針對MSSQL的。 – Outflanker

+0

我不明白你的輸出要求。另外 - 樣本數據應該包含另一個人的數據,而不僅僅是JERRY的? –

回答

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 

其中給出的結果:

 
Name      Year  Month  Value  rnk 
------------------------- ----------- ----------- ----------- ------ 
FRED      2011  12   50   1 
FRED      2011  12   120   1 
JERRY      2012  9   100   1 
JERRY      2012  9   120   1 
JERRY      2012  9   130   1 
+1

我喜歡你的答案,但'ORDER BY [Year] * 100 + [Month] DESC'真的嗎?爲什麼不'ORDER BY [Year] DESC,[Month] DESC'? –

+0

這是錯誤的...過濾最近幾年的最新月份 –

+0

@Niladri:該查詢是正確的。 –

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)) 
+0

請檢查更新的答案 – AnandPhadke

+1

這將無法正常工作..最大月份將根據最大年份 –

+0

是我更新了查詢檢查 – AnandPhadke

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) 

結果

enter image description here

上述查詢的工作只是爲單個用戶。並且會失敗多個用戶,或在關係的情況下。例如,請考慮下面的情形

enter image description here

在這種情況下,所需的輸出將是

enter image description here

所以對於處理這樣的情況,我建議以下解決方案

解決方案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 

OR

溶液2(SQL服務器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服務器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) 

希望這可能有所幫助。由於

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) 


CLICK for demo

相關問題