【问题标题】:Return Maximum Value from Group for Year Required从组中返回所需年份的最大值
【发布时间】:2014-03-13 17:59:31
【问题描述】:

我整个下午都在这里查看了许多类似的查询,但不能完全正确地得到这个查询。

我正在尝试返回与唯一 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'

样本数据:

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、[Date by Year] 和 Label -(不需要销售值)。因此,我想返回

SN9W4D-S-US | 2013 | Japan ...as my output

请记住,原始表有多个 fs_perm_sec_id 和日期条目。因此,最终我想突出显示与年份为 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

非常感谢。

【问题讨论】:

  • 欢迎使用 StackOverflow:如果您发布代码、XML 或数据示例,PLEASE 在文本编辑器中突出显示这些行并单击“代码编辑器工具栏上的示例”按钮 ({ }) 以很好地格式化和语法突出显示它!
  • @marc_s 但是你不会得到 2 分 :)
  • @marc_s - 谢谢,我不知道该怎么做……以后会做的。

标签: sql sql-server sql-server-2008 date


【解决方案1】:

试试这个

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

【讨论】:

  • 谢谢 - 这适用于单个 id,如果我有多个 fs_perm_sec_id 怎么办?
  • 您可以修改 DENSE_RANK() 使用 partition by 子句。
  • 谢谢,会试一试的。
  • (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
【解决方案2】:

最终代码是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    相关资源
    最近更新 更多