Skip to main content
trim noise, clean up, add tag, better title
Source Link
Erwin Brandstetter
  • 182.1k
  • 28
  • 457
  • 620

Conditionaly Make query depend on input in postgresqlPostgreSQL function

I have a simple query below :

CREATE OR REPLACE FUNCTION companies_all_teams(                                                                                                                                                                                                                        
  IN  _company_name text = NULL,                                                                                                                                                                                                                                                      
  IN  _company_id int = NULL,                                                                                                                                                                                                                                                                                                  
  OUT _result text                                                                                                                                                                                                                                                             
)                                                                                                                                                                                                                                                                              
AS                                                                                                                                                                                                                                                                             
$$                                                                                                                                                                                                                                                                             
  BEGIN                                                                                                                                                                                                                                                                        
  _result := (SELECT json_agg(distinct(teams.name))                                                                                                                                                                                                                            
                     FROM jobs                                                                                                                                                                                                                                                 
                     JOIN companies                                                                                                                                                                                                                                            
                       ON companies.id = jobs.company_id                                                                                                                                                                                                                       
                     JOIN teams                                                                                                                                                                                                                                                
                       ON teams.id = jobs.team_id                                                                                                                                                                                                                              
                     WHERE lower(companies.name) = lower(_company_name)                                                                                                                                                                                                        
                       AND jobs.is_disabled = false                                                                                                                                                                                                                            
             );                                                                                                                                                                                                                                                                
  END;                                                                                                                                                                                                                                                                         
CREATE OR REPLACE FUNCTION companies_all_teams(
  IN  _company_name text = NULL,
  IN  _company_id int = NULL,
  OUT _result text
)
AS
$$
  BEGIN
  _result := (SELECT json_agg(distinct(teams.name))
                     FROM jobs
                     JOIN companies
                       ON companies.id = jobs.company_id
                     JOIN teams
                       ON teams.id = jobs.team_id
                     WHERE lower(companies.name) = lower(_company_name)
                       AND jobs.is_disabled = false
             );
  END;
$$ LANGUAGE plpgsql;

There are two inputs, but only one of those is being used in the query. What is the simplest way that we can doto run the query conditionally based on which input we have  ?

Thanks

Conditionaly query in postgresql function

I have a simple query below :

CREATE OR REPLACE FUNCTION companies_all_teams(                                                                                                                                                                                                                        
  IN  _company_name text = NULL,                                                                                                                                                                                                                                                      
  IN  _company_id int = NULL,                                                                                                                                                                                                                                                                                                  
  OUT _result text                                                                                                                                                                                                                                                             
)                                                                                                                                                                                                                                                                              
AS                                                                                                                                                                                                                                                                             
$$                                                                                                                                                                                                                                                                             
  BEGIN                                                                                                                                                                                                                                                                        
  _result := (SELECT json_agg(distinct(teams.name))                                                                                                                                                                                                                            
                     FROM jobs                                                                                                                                                                                                                                                 
                     JOIN companies                                                                                                                                                                                                                                            
                       ON companies.id = jobs.company_id                                                                                                                                                                                                                       
                     JOIN teams                                                                                                                                                                                                                                                
                       ON teams.id = jobs.team_id                                                                                                                                                                                                                              
                     WHERE lower(companies.name) = lower(_company_name)                                                                                                                                                                                                        
                       AND jobs.is_disabled = false                                                                                                                                                                                                                            
             );                                                                                                                                                                                                                                                                
  END;                                                                                                                                                                                                                                                                         
$$ LANGUAGE plpgsql;

There are two inputs, but only one of those being used in the query. What is the simplest way that we can do the query conditionally based on which input we have  ?

Thanks

Make query depend on input in PostgreSQL function

I have a simple query:

CREATE OR REPLACE FUNCTION companies_all_teams(
  IN  _company_name text = NULL,
  IN  _company_id int = NULL,
  OUT _result text
)
AS
$$
  BEGIN
  _result := (SELECT json_agg(distinct(teams.name))
                     FROM jobs
                     JOIN companies
                       ON companies.id = jobs.company_id
                     JOIN teams
                       ON teams.id = jobs.team_id
                     WHERE lower(companies.name) = lower(_company_name)
                       AND jobs.is_disabled = false
             );
  END;
$$ LANGUAGE plpgsql;

There are two inputs, but only one of those is being used in the query. What is the simplest way to run the query conditionally based on which input we have?

deleted 1 character in body
Source Link
CREATE OR REPLACE FUNCTION companies_all_teams(                                                                                                                                                                                                                        
  IN  _company_name text = NULL,                                                                                                                                                                                                                                                      
  IN  _company_name_company_id idint = NULL,                                                                                                                                                                                                                                                                                                  
  OUT _result text                                                                                                                                                                                                                                                             
)                                                                                                                                                                                                                                                                              
AS                                                                                                                                                                                                                                                                             
$$                                                                                                                                                                                                                                                                             
  BEGIN                                                                                                                                                                                                                                                                        
  _result := (SELECT json_agg(distinct(teams.name))                                                                                                                                                                                                                            
                     FROM jobs                                                                                                                                                                                                                                                 
                     JOIN companies                                                                                                                                                                                                                                            
                       ON companies.id = jobs.company_id                                                                                                                                                                                                                       
                     JOIN teams                                                                                                                                                                                                                                                
                       ON teams.id = jobs.team_id                                                                                                                                                                                                                              
                     WHERE lower(companies.name) = lower(_company_name)                                                                                                                                                                                                        
                       AND jobs.is_disabled = false                                                                                                                                                                                                                            
             );                                                                                                                                                                                                                                                                
  END;                                                                                                                                                                                                                                                                         
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION companies_all_teams(                                                                                                                                                                                                                        
  IN  _company_name text = NULL,                                                                                                                                                                                                                                                      
  IN  _company_name id = NULL,                                                                                                                                                                                                                                                                                                  
  OUT _result text                                                                                                                                                                                                                                                             
)                                                                                                                                                                                                                                                                              
AS                                                                                                                                                                                                                                                                             
$$                                                                                                                                                                                                                                                                             
  BEGIN                                                                                                                                                                                                                                                                        
  _result := (SELECT json_agg(distinct(teams.name))                                                                                                                                                                                                                            
                     FROM jobs                                                                                                                                                                                                                                                 
                     JOIN companies                                                                                                                                                                                                                                            
                       ON companies.id = jobs.company_id                                                                                                                                                                                                                       
                     JOIN teams                                                                                                                                                                                                                                                
                       ON teams.id = jobs.team_id                                                                                                                                                                                                                              
                     WHERE lower(companies.name) = lower(_company_name)                                                                                                                                                                                                        
                       AND jobs.is_disabled = false                                                                                                                                                                                                                            
             );                                                                                                                                                                                                                                                                
  END;                                                                                                                                                                                                                                                                         
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION companies_all_teams(                                                                                                                                                                                                                        
  IN  _company_name text = NULL,                                                                                                                                                                                                                                                      
  IN  _company_id int = NULL,                                                                                                                                                                                                                                                                                                  
  OUT _result text                                                                                                                                                                                                                                                             
)                                                                                                                                                                                                                                                                              
AS                                                                                                                                                                                                                                                                             
$$                                                                                                                                                                                                                                                                             
  BEGIN                                                                                                                                                                                                                                                                        
  _result := (SELECT json_agg(distinct(teams.name))                                                                                                                                                                                                                            
                     FROM jobs                                                                                                                                                                                                                                                 
                     JOIN companies                                                                                                                                                                                                                                            
                       ON companies.id = jobs.company_id                                                                                                                                                                                                                       
                     JOIN teams                                                                                                                                                                                                                                                
                       ON teams.id = jobs.team_id                                                                                                                                                                                                                              
                     WHERE lower(companies.name) = lower(_company_name)                                                                                                                                                                                                        
                       AND jobs.is_disabled = false                                                                                                                                                                                                                            
             );                                                                                                                                                                                                                                                                
  END;                                                                                                                                                                                                                                                                         
$$ LANGUAGE plpgsql;
Source Link

Conditionaly query in postgresql function

I have a simple query below :

CREATE OR REPLACE FUNCTION companies_all_teams(                                                                                                                                                                                                                        
  IN  _company_name text = NULL,                                                                                                                                                                                                                                                      
  IN  _company_name id = NULL,                                                                                                                                                                                                                                                                                                  
  OUT _result text                                                                                                                                                                                                                                                             
)                                                                                                                                                                                                                                                                              
AS                                                                                                                                                                                                                                                                             
$$                                                                                                                                                                                                                                                                             
  BEGIN                                                                                                                                                                                                                                                                        
  _result := (SELECT json_agg(distinct(teams.name))                                                                                                                                                                                                                            
                     FROM jobs                                                                                                                                                                                                                                                 
                     JOIN companies                                                                                                                                                                                                                                            
                       ON companies.id = jobs.company_id                                                                                                                                                                                                                       
                     JOIN teams                                                                                                                                                                                                                                                
                       ON teams.id = jobs.team_id                                                                                                                                                                                                                              
                     WHERE lower(companies.name) = lower(_company_name)                                                                                                                                                                                                        
                       AND jobs.is_disabled = false                                                                                                                                                                                                                            
             );                                                                                                                                                                                                                                                                
  END;                                                                                                                                                                                                                                                                         
$$ LANGUAGE plpgsql;

There are two inputs, but only one of those being used in the query. What is the simplest way that we can do the query conditionally based on which input we have ?

Thanks