对结果进行分组并在SQL中为每个条件仅获取一条记录

时间:2016-10-24 19:34:21

标签: sql-server tsql sql-server-2012 group-by distinct

我有这样的结果:

enter image description here

正如你所看到的,我得到了多个相同的结果,我希望每个条件只能获得一行,但我无法实现。

这是我正在使用的查询:

SELECT
      frld.[SegAmortisation],
    SUM(frld.CurrentLB) over (Partition By frlc.FundReportCategoryId) AS '% of Fund',
    frlc.FundReportCategoryId
  FROM [dbo].[FundReportLoanData] frld
  INNER JOIN [dbo].[FundReportCategories] frlc
  ON frld.SegAmortisation = frlc.Description
    group by  frld.SegAmortisation, 
         frlc.FundReportCategoryId, frld.CurrentLB

任何想法我怎么能做一些像结果不同的东西,每个FundReportCategoryId只能获得一条记录?

1 个答案:

答案 0 :(得分:2)

SELECT
      frld.[SegAmortisation],
      SUM(frld.CurrentLB) AS '% of Fund',
      frlc.FundReportCategoryId
FROM [dbo].[FundReportLoanData] frld
INNER JOIN [dbo].[FundReportCategories] frlc
ON frld.SegAmortisation = frlc.Description
group by  
     frld.SegAmortisation, 
     frlc.FundReportCategoryId