我需要找到Ten_Yr_CAPE没有0值的第一个日期。 当Ten_Yr_CAPE大于0时,输出表应具有Country列和Date Column,其中包含Date。
我写了下面的查询,但是它提取了所有包含' Ten_Yr_CAPE' <>我只需要初始日期。任何人都可以帮助我。
选择TOP(1)只返回一条记录。我需要每个国家一条记录。所以每个国家共有20条记录
select [Date],[Country] from [Tableau].[dbo].[Country_table4$] where
[Ten_Yr_CAPE] <> 0
Group by [Country], [Date]
Order by [Date] ASC
答案 0 :(得分:5)
我认为聚合是最简单的方法:
select t.[Country], min(t.[Date])
from [Tableau].[dbo].[Country_table4$] t
where [Ten_Yr_CAPE] <> 0
Group by [Country]
Order by [Date] ASC
答案 1 :(得分:4)
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY [COUNTRY]
ORDER BY [Date]) as rn
FROM [Tableau].[dbo].[Country_table4$]
WHERE [Ten_Yr_CAPE] <> 0 ) AS T
WHERE T.rn = 1
答案 2 :(得分:3)
你可以使用top(1)with ties:
select top (1) with ties [Date],[Country]
from [Tableau].[dbo].[Country_table4$]
where [Ten_Yr_CAPE] <> 0
order by row_number() over(partition by [Country] order by [Date] ASC)