Postgres how to avoid repetitive if then in select statement

user1071182

What's the easiest way to combine a few columns of a table to set up variables for another query?

I want to avoid constantly repeating the same if then statements in my query

SELECT 
CASE
WHEN team_id = away_team_id THEN away_score * 5
WHEN team_id = home_team_id THEN home_score * 5
END AS team_score_times_five, 
CASE
WHEN team_id = away_team_id THEN away_score - 5
WHEN team_id = home_team_id THEN home_score - 5
END AS team_score_minus_five
FROM t1

Instead I would rather set up one variable

CASE
WHEN team_id = away_team_id THEN away_score
WHEN team_id = home_team_id THEN home_score
END AS team_score

and then query it much more cleanly

SELECT team_score * 5 AS team_score_times_five, team_score - 5 AS team_score_minus_five FROM t1
Adam Bethke

You might also try common table expressions:

WITH common_table_expression AS (
    SELECT 
        CASE
          WHEN team_id = away_team_id THEN away_score
          WHEN team_id = home_team_id THEN home_score
          END AS team_score
    FROM t1
)
SELECT 
      team_score * 5 AS team_score_times_five
    , team_score - 5 AS team_score_minus_five
FROM common_table_expression

CTEs allow you to stack multiple queries that would be nested in ways that tend to be more readable, and more easily modified in the future (depending on the situation) if the business requirement changes.

This article has a great section entitled "Use Common Table Expressions for extremely readable SQL" that makes additional arguments for why you'd want to use a CTE.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to avoid repetitive function parameters in clojure?

How to define WINDOWING function in Spark SQL query to avoid repetitive code

How to avoid repetitive code in redux (ducks approach)?

How to select a UUID from a prepared statement in Postgres?

How can I use a postgres variable in my SELECT statement?

How to format output of a select statement in Postgres back into a delimited file?

How to avoid repetitive long generic constraints in Rust

How can I avoid repetitive commands, such as cd in a target rule?

SQL Query repetitive `AND` query, how do I avoid it?

How to avoid repetitive codes in cypress

How to avoid repetitive CASE statements in SQL WHILE loop doing INSERT

How to avoid repetitive JUnit tests

How to evaluate expression in select statement in Postgres

How to make this repetitive Update statement in MySQL shorter?

Loop to avoid repetitive code

How to SUM in JOIN, to avoid issue with two SUM in SELECT statement

How do I avoid repetitive code in this event handler?

How to avoid multiple node processes doing repetitive things?

How to avoid repetitive naming in python packages?

Alias a POSTGRES select statement

How to avoid repetitive XAML?

Avoid repetitive xml code

How to use CASE or IF-statement in Postgres to select from different table?

How to avoid the extra column used for ordering in the SELECT statement to the final output?

SELECT STATEMENT QUERY POSTGRES

How to avoid repetitive syscalls and moves in MIPS Assembly?

How to use CASE in postgres function to return Select statement?

List in Python help needed: how to Avoid calling repetitive code

How to avoid repetitive conditions in authorization policies in .NET Core