Home > Net >  adding a figure to a sum query output
adding a figure to a sum query output

Time:07-09

is it possible to add a value to a select sum statements output?

i have a query that is summing a field based on the year, however i need the year 2022 to have an additional value of 1500 added to the sum.

SELECT YEAR(rcyl_in) as Year, 
        SUM(case when a_RECLM.kgs_cred > a_RECLM.net_wt then a_RECLM.net_wt/2
                  else a_RECLM.kgs_cred/2 end) as 'Total Recovered'
FROM a_reclm  
GROUP BY  YEAR(rcyl_in)

result

2019    49208.450000
2020    11014.900000
2021    34092.400000
2022    1094.800000

however I need 2022 to be 2594.80000

thanks in advance for any help

CodePudding user response:

One option is apply an outer query with another case condition.

Below query is not tested, try and let me know:

SELECT Year,case when Year=2022 then Total_Recovered 1500 else Total_Recovered end as new_Total_Recovered
FROM (
         SELECT YEAR(rcyl_in) as Year, 
                SUM(case when a_RECLM.kgs_cred > a_RECLM.net_wt then a_RECLM.net_wt/2
                          else a_RECLM.kgs_cred/2 end) as 'Total_Recovered'
         FROM a_reclm  
         GROUP BY  YEAR(rcyl_in)
      ) as t1 ;
  • Related