Sql 从组返回所需年份的最大值
整个下午,我都在这里查看了许多类似的查询,但不能完全正确地理解这个查询 我试图返回与所需年份的唯一ID的一组结果的最大值相关联的标签。以下是代码+输出:Sql 从组返回所需年份的最大值,sql,sql-server,sql-server-2008,date,Sql,Sql Server,Sql Server 2008,Date,整个下午,我都在这里查看了许多类似的查询,但不能完全正确地理解这个查询 我试图返回与所需年份的唯一ID的一组结果的最大值相关联的标签。以下是代码+输出: select fs_perm_sec_id, YEAR(date) as [Date by Year], label, sales from ff_v2.ff_segreg_af where fs_perm_sec_id = 'SN9W4D-S-US' and YEAR(date) = '2013' 样
select
fs_perm_sec_id, YEAR(date) as [Date by Year], label, sales
from
ff_v2.ff_segreg_af
where
fs_perm_sec_id = 'SN9W4D-S-US'
and YEAR(date) = '2013'
样本数据:
fs_perm_sec_id Date by Year label sales
SN9W4D-S-US 2013 Japan 26592.96196
SN9W4D-S-US 2013 Europe 16445.23016
SN9W4D-S-US 2013 United States 12851.71355
SN9W4D-S-US 2013 Other Countries 10855.52867
SN9W4D-S-US 2013 Asia Pacific 9730.89435
SN9W4D-S-US 2013 China 5609.94288
因此,在本例中,我想返回fs_perm_sec_id,[日期按年份]和Label-(不需要销售值)。因此,我想回去
SN9W4D-S-US | 2013 | Japan ...as my output
请记住,原始表有多个fs_perm_sec_id和date条目。因此,最终我想突出显示与2013年所有独特条目的最大销售价值相匹配的标签
下面是表中所有字段的示例:
fs_perm_sec_id date ff_segment_type ff_segment_num adjdate currency label sales opinc assets capex dep
SN9W4D-S-US 2012-03-31 REG 1 2000-05-25 USD Japan 26729.2963 NULL 8500.71105 NULL NULL
SN9W4D-S-US 2012-03-31 REG 2 2000-05-25 USD Europe 16106.8766 NULL 670.5828 NULL NULL
SN9W4D-S-US 2012-03-31 REG 3 2000-05-25 USD United States 15390.4823 NULL 1007.4051 NULL NULL
SN9W4D-S-US 2012-03-31 REG 4 2000-05-25 USD Other Countries 9865.9442 NULL 204.08355 NULL NULL
SN9W4D-S-US 2012-03-31 REG 5 2000-05-25 USD Asia Pacific 8083.4103 NULL 450.279 NULL NULL
SN9W4D-S-US 2012-03-31 REG 6 2000-05-25 USD China 6287.7827 NULL 478.5642 NULL NULL
SN9W4D-S-US 2013-03-31 REG 1 2000-05-25 USD Japan 26592.96196 NULL 6571.06184 NULL NULL
SN9W4D-S-US 2013-03-31 REG 2 2000-05-25 USD Europe 16445.23016 NULL 568.8144 NULL NULL
SN9W4D-S-US 2013-03-31 REG 3 2000-05-25 USD United States 12851.71355 NULL 791.17976 NULL NULL
SN9W4D-S-US 2013-03-31 REG 4 2000-05-25 USD Other Countries 10855.52867 NULL 196.66976 NULL NULL
SN9W4D-S-US 2013-03-31 REG 5 2000-05-25 USD Asia Pacific 9730.89435 NULL 521.11528 NULL NULL
SN9W4D-S-US 2013-03-31 REG 6 2000-05-25 USD China 5609.94288 NULL 518.05096 NULL NULL
提前多谢。试试这个
select fs_perm_sec_id, [Date by Year], label
from
(
select
fs_perm_sec_id, YEAR(date) as [Date by Year], label, DENSE_RANK ( ) OVER (order by sales desc) as RankNo
from
ff_v2.ff_segreg_af
where
fs_perm_sec_id = 'SN9W4D-S-US'
and YEAR(date) = '2013'
) r
where RankNo = 1
最终代码是:
use FDS3
select fs_perm_sec_id, [Date by Year], label
from
(select fs_perm_sec_id, YEAR(date) as [Date by Year], label, DENSE_RANK ( )
OVER (partition by fs_perm_sec_id order by sales) as RankNo
from ff_v2.ff_segreg_af where
fs_perm_sec_id in
('SN9W4D-S-US',
'HSDCT7-S-US',
'WHQNFK-S-US',
'PRM2JP-S-US',
'R2KQ06-S-US',
'L7GJB9-S-US',
'P47Z0J-S-US')
and YEAR(date) = '2013'
) r
where RankNo = 1
欢迎使用StackOverflow:如果您发布代码、XML或数据示例,请在文本编辑器中突出显示这些行,并单击编辑器工具栏上的“代码示例”按钮(
{}
),以精确地格式化和语法突出显示它@marc_s但你不会得到2点:)@marc_s-谢谢,我不知道怎么做…以后会做的。谢谢-这对单个id有效,如果我有多个fs_perm_sec_id怎么办?你可以修改稠密的排名()使用partition by子句。谢谢,我会试试。(选择fs_perm_sec_id,年份(日期)作为[每年的日期],标签,密级()(按销售额按fs\u perm\u sec\u id订单划分)为RankNo