使用窗口函数获取多个记录的最大值 - SQL

时间:2013-10-25 18:50:05

标签: sql sql-server-2008 window-functions

我还是SQL的新手,我在SQL中获取多个记录的最大值时遇到了问题。我通常会使用视图并将视图加入到我的存储过程中,但由于动态表名,我需要能够在没有视图的情况下执行此操作。

这是我的代码:

SELECT     rcl.CaseCaseId AS CaseId, cc.Label AS CaseName, rpt.RptDateCum, cp.Label, rpt.CorpProductId, corp.Label AS Scenario, proj.Label AS Project, 
                      ProjectScenario.ReportDate
into #CumTemp
FROM         PhdRpt.ReportCaseList_542 AS rcl INNER JOIN
                      CaseScenario AS cs ON rcl.CaseCaseId = cs.CaseCaseId INNER JOIN
                      CorpScenario AS corp ON cs.CorpScenarioId = corp.CorpScenarioId INNER JOIN
                      CaseCases AS cc ON cs.CaseCaseId = cc.CaseCaseId INNER JOIN
                      PhdRpt.RptProductTech_542 AS rpt ON rcl.ReportId = rpt.ReportID AND rcl.ReportRunCaseId = rpt.RptCaseId INNER JOIN
                      CorpProduct AS cp ON rpt.CorpProductId = cp.CorpProductId INNER JOIN
                      ProjProject AS proj ON cs.ProjProjectId = proj.ProjProjectId INNER JOIN
                      ProjectScenario ON cs.ProjProjectId = ProjectScenario.ProjProjectId AND cs.CorpScenarioId = ProjectScenario.CorpScenarioId AND 
                      corp.CorpScenarioId = ProjectScenario.CorpScenarioId AND proj.ProjProjectId = ProjectScenario.ProjProjectId AND 
                      proj.ProjProjectId = ProjectScenario.ProjProjectId
WHERE     (cs.CorpScenarioId = 1) AND (cs.Deleted = 0)
ORDER BY CaseId


Select  ROW_NUMBER() OVER(order by caseid) as Row, CaseId, Scenario,Project, ReportDate, ltrim(str(sum(Gas))) as Gas, ltrim(str(SUM(Oil))) as Oil, ltrim(str(SUM(Water))) as Water
,MAX(oil) over (partition by Project) as oilmax, MAX(gas) over (partition by Project) as gasmax, MAX(water) over (partition by Project) as watermax
from #CumTemp
pivot (max (RptDateCum) for Label in ([Gas], [Oil], [Water]))as x
group by CaseId, Scenario,Project, ReportDate, Oil
order by Row 
drop table #CumTemp

我想要完成的是比较记录并找到Gas,Oil和Water的最大值,并创建3个新列,其中包含这些值。我不想使用分组的原因是因为我仍然需要访问每个单独的记录。所有记录都属于同一个项目。

我想要的输出看起来像这样。

id      oil    gas    water   oilmax    gasmax    watermax   
----------------------------------------------------------
 1       25     12       6      51        98         37
 2       36     98      19      51        98         37
 3       51     19      22      51        98         37
 4       45     77      37      51        98         37
 5       22     64      31      51        98         37

当我使用我的代码时,每个记录只需要一行时,它会为每条记录提供多行。是否有另一种方法可以在不使用视图或窗口函数的情况下完成此操作?

1 个答案:

答案 0 :(得分:2)

因此,由于您的查询非常大,我将展示如何解决您的问题并将其放入查询中。这是我的测试用例:

create table testcase(
    id integer,
    oil integer,
    gas integer,
    water integer
);

插入

insert into testcase values ( 1, 25, 12,  6 );
insert into testcase values ( 2, 36, 98, 19);
insert into testcase values ( 3, 51, 19,  22);
insert into testcase values ( 4, 45, 77,  37);
insert into testcase values ( 5, 22, 64,  31);

的Sql

select * 
  from testcase as test,
       (select max(oil) maxoil, 
               max(gas) maxgas, 
               max(water) maxwater 
          from testcase) as maxes 
 order by test.id

这可以根据需要为您提供数据。 看看小提琴http://sqlfiddle.com/#!3/be2b7/3/0