基于多列中的最大值检索SQL中的行

时间:2021-09-18 15:41:34

I have a SQL table with the following fields:

我有一个包含以下字段的SQL表:

  • Company ID
  • 公司ID
  • Company Name
  • 公司名
  • Fiscal Year
  • 财政年度
  • Fiscal Quarter
  • 财政季度

There are multiple records for various fiscal years and fiscal quarters for each company. I want to retrieve the rows for each company based on Maximum Fiscal Year and Maximum Fiscal Quarter. For example, if the table has the following:

每个公司的财政年度和财政季度都有多个记录。我想根据最高财政年度和最高财政季度检索每家公司的行。例如,如果表具有以下内容:

Company ID  |  Company Name  |  Fiscal Year | Fiscal Quarter 
1           |  Test1         |  2017        | 1
1           |  Test1         |  2017        | 2
1           |  Test1         |  2018        | 1
1           |  Test1         |  2018        | 2
2           |  Test2         |  2018        | 3
2           |  Test2         |  2018        | 4

The query should return the following (Only the record with the maximum fiscal year and maximum fiscal quarter for that year):

该查询应返回以下内容(仅具有该年度的最高会计年度和最大会计季度的记录):

Company ID  |  Company Name  |  Fiscal Year | Fiscal Quarter 
1           |  Test1         |  2018        | 2
2           |  Test2         |  2018        | 4

I am able to use the below query to get the records with the maximum fiscal year but not sure how to further select the maximum quarter within the year:

我可以使用以下查询来获取具有最大会计年度的记录,但不确定如何进一步选择一年中的最大季度:

SELECT fp.companyId, fp.companyname, fp.fiscalyear,fp.fiscalquarter
FROM  dbo.ciqFinPeriod fp
  LEFT OUTER JOIN dbo.ciqFinPeriod fp2
    ON (fp.companyId = fp2.companyId AND fp.fiscalyear < fp2.fiscalyear)
WHERE fp2.companyId IS NULL

Thank you so much for any assistance!

非常感谢你的帮助!

3 个解决方案

#1


3  

If you have a list of companies, I would simply do:

如果您有公司列表,我会这样做:

select fp.*
from Companies c outer apply
     (select top (1) fp.*
      from dbo.ciqFinPeriod fp
      where fp.companyId = c.companyId
      order by fp.fiscalyear desc, fp.fiscalquarter desc
     ) fp;

If not, then row_number() is probably the simplest method:

如果没有,那么row_number()可能是最简单的方法:

select fp.*
from (select fp.*,
             row_number() over (partition by fp.companyId order by order by fp.fiscalyear desc, fp.fiscalquarter desc) as seqnum
      from dbo.ciqFinPeriod fp
     ) fp
where seqnum = 1;

Or the somewhat more abstruse (clever ?):

或者更深奥(聪明?):

select top (1) with ties fp.*
from dbo.ciqFinPeriod fp
order by row_number() over (partition by fp.companyId order by order by fp.fiscalyear desc, fp.fiscalquarter desc)

#2


0  

I've had some success with the following, same output as you.

我在以下方面取得了一些成功,与您相同。

create table #table
(
CompanyID int,
CompanyName varchar(200),
Year int,
Quater int
)

insert into #table (CompanyID,CompanyName,Year,Quater)
VALUES

('1','Test1','2017','1'),
('1','Test1','2017','2'),
('1','Test1','2018','1'),
('1','Test1','2018','2'),
('2','Test2','2018','3'),
('2','Test2','2018','4')

SELECT CompanyID,CompanyName,Year,Quater  
FROM
(

Select CompanyID,CompanyName,Year,Quater
, ROW_NUMBER() OVER(PARTITION BY CompanyID ORDER BY Year desc,Quater DESC)         
as RowNum
from #table

) X  WHERE RowNum = 1

drop table #table

#3


0  

Select Company I'd, company name,Max(year),Max(quarter) group by 1,2

选择公司我,公司名称,最大(年),最大(季度)组1,2

#1


3  

If you have a list of companies, I would simply do:

如果您有公司列表,我会这样做:

select fp.*
from Companies c outer apply
     (select top (1) fp.*
      from dbo.ciqFinPeriod fp
      where fp.companyId = c.companyId
      order by fp.fiscalyear desc, fp.fiscalquarter desc
     ) fp;

If not, then row_number() is probably the simplest method:

如果没有,那么row_number()可能是最简单的方法:

select fp.*
from (select fp.*,
             row_number() over (partition by fp.companyId order by order by fp.fiscalyear desc, fp.fiscalquarter desc) as seqnum
      from dbo.ciqFinPeriod fp
     ) fp
where seqnum = 1;

Or the somewhat more abstruse (clever ?):

或者更深奥(聪明?):

select top (1) with ties fp.*
from dbo.ciqFinPeriod fp
order by row_number() over (partition by fp.companyId order by order by fp.fiscalyear desc, fp.fiscalquarter desc)

#2


0  

I've had some success with the following, same output as you.

我在以下方面取得了一些成功,与您相同。

create table #table
(
CompanyID int,
CompanyName varchar(200),
Year int,
Quater int
)

insert into #table (CompanyID,CompanyName,Year,Quater)
VALUES

('1','Test1','2017','1'),
('1','Test1','2017','2'),
('1','Test1','2018','1'),
('1','Test1','2018','2'),
('2','Test2','2018','3'),
('2','Test2','2018','4')

SELECT CompanyID,CompanyName,Year,Quater  
FROM
(

Select CompanyID,CompanyName,Year,Quater
, ROW_NUMBER() OVER(PARTITION BY CompanyID ORDER BY Year desc,Quater DESC)         
as RowNum
from #table

) X  WHERE RowNum = 1

drop table #table

#3


0  

Select Company I'd, company name,Max(year),Max(quarter) group by 1,2

选择公司我,公司名称,最大(年),最大(季度)组1,2