vim
vim

Reputation: 874

Default null value in join query

I have below join query.

select
  SD.id,
  SD.Scenario,
  PR.Type,
  PR.StationName,
  max(if(PARAM='minH',Value,' ')) as 'Min H',
  max(if(PARAM='maxQ',Value,' ')) as 'Max Q',
  max(if(PARAM='avQ',Value,' ')) as 'AV Q',
  dsd.Dam_Min_Level,
  dsd.Dam_Max_Level
from sgwebdb.param_reference as PR
  Inner join sgwebdb.scenario_data as SD
    ON PR.Param_Id = SD.Param_Id
  INNER JOIN sgwebdb.qualicision_detail as Q
    ON SD.SCENARIO = Q.Alternative
  INNER JOIN sgwebdb.dam_station_detail as dsd
    ON dsd.Station_Id = PR.Station_Id
where PR.Type = 'Reservoirs'
    and Q.Alternative = 'C'
GROUP BY PR.Id;

and output is below

ID   Scenario     Type      StationName  Min H  Max Q   AV Q   Dam_Min_Level   Dam_Max_Level
3   C   Reservoirs  Beris   82.1461            23            33
7   C   Reservoirs  Muda    90.6169            4.8           4.4
11  C   Reservoirs  Beris       1.49           23            33
15  C   Reservoirs  Muda        0          4.8           4.4
19  C   Reservoirs  Pedu            36.262     23            33

How can I put 0 number in place of blank cells of Min H ,Max Q and AV Q columns.

Upvotes: 0

Views: 28

Answers (1)

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

You can do it like this

select
  SD.id,
  SD.Scenario,
  PR.Type,
  PR.StationName,
  max(if(PARAM='minH',Value,0)) as 'Min H',
  max(if(PARAM='maxQ',Value,0)) as 'Max Q',
  max(if(PARAM='avQ',Value,0)) as 'AV Q',
  dsd.Dam_Min_Level,
  dsd.Dam_Max_Level
from sgwebdb.param_reference as PR
  Inner join sgwebdb.scenario_data as SD
    ON PR.Param_Id = SD.Param_Id
  INNER JOIN sgwebdb.qualicision_detail as Q
    ON SD.SCENARIO = Q.Alternative
  INNER JOIN sgwebdb.dam_station_detail as dsd
    ON dsd.Station_Id = PR.Station_Id
where PR.Type = 'Reservoirs'
    and Q.Alternative = 'C'
GROUP BY PR.Id;

the only thing you need to do is pass 0 as second parameter in IF condition instead of empty string

Upvotes: 1

Related Questions