0

我还是 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 个新列,其中包含这些值。我不想使用 group by 的原因是我仍然需要访问每个单独的记录。所有记录都属于同一个项目。

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

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

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

4

1 回答 1

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

于 2013-10-25T19:26:35.197 回答