سؤال

I am still new to SQL and I am having trouble getting a maximum value over multiple records in SQL. I would normally use a view and join the view to my stored procedure but I need to be able to do this without views because of dynamic table names.

Here is my code:

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

What I am trying to accomplish is to compare the records and find the max value of Gas, Oil, and Water, and create 3 new columns that will have those values in it. The reason I do not want to use a group by is because I still need to access each individual record. All records belong to the same project.

My desired output would look something like this.

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

When I use my code it is giving me multiple rows per record when I only want one row per record. Is there another way of accomplishing this without using views or windowfunctions?

هل كانت مفيدة؟

المحلول

So, as your query is very large I will show how to solve your problem and you put it in your query. This is my test case:

Table

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

Inserts

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

This should give you your data as you want. Look at fiddle http://sqlfiddle.com/#!3/be2b7/3/0

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top