Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT statement expression value reuse for other expression

I have a table with huge row count in mysql (though I am looking for generic SQL solution)

very_big_table(INT a, INT b, INT c, ...)

I wanted SELECT statement

SELECT a, 
    (b + c) as expression1,
    (b + c + a) AS expression2 -- basically (expression1 + a)
FROM very_big_table
WHERE ...
GROUP BY a
ORDER BY a DESC

this looks good and easily readable as long as the expression1 is simple.
But when CASE-WHEN/IFNULL()/SUM()/MIN()/STRCAT() or some Operator comes into play in these expressions its difficult to read and debug.

I have gone through some of the already asked questions
assigning mysql value to variable inline
Use value of a column for another column (SQL Server)?
How to use conditional columns values in the same select statement?

But if I use the approaches described something like

SELECT a, 
    expression1,
    (expression1 + a) AS expression2
FROM 
    (SELECT a,
        (b + c) AS expression1
    FROM very_big_table
    WHERE ...
    GROUP BY a) as inner_table
ORDER BY a DESC

this works fine, but this query is taking some 70x more time to execute. Atleast when i fired it, though only once.
what if I have multiple levels of the expressions in the output columns?

Is there any elegant way to deal with this, without compromising readability?

BTW why isnt this expression reuse or alias reference in select statement not supported by SQL standards or vendors? (supposing there are no cyclic evaluation in the single SELECT statement expressions. in that case the compiler fails)

like image 703
user918953 Avatar asked Aug 23 '12 21:08

user918953


People also ask

How do you reuse a case statement in SQL?

To reuse a case expression value in multiple places within a SQL statement, you would have to define and create a User defined function that outputs that case statement value. @user2391001 You may be able to use a common table expression, but that would depend on the query.

Can a stored function be used in different clauses of a select statement?

Since stored functions may be used in expressions, they may be included wherever expressions are allowed in a query, including: The SELECT clause. The WHERE clause. The GROUP BY and HAVING clauses.

Which clause is used to retrieve values with similar matches?

The SQL LIKE clause is used to compare a value to similar values using wildcard operators.

What is the use of select * from?

The query retrieves all rows from the Book table in which the price column contains a value greater than 100.00. The result is sorted in ascending order by title. The asterisk (*) in the select list indicates that all columns of the Book table should be included in the result set. SELECT * FROM Book WHERE price > 100.


1 Answers

You can use User-Defined Variable to solve your problem. Your SQL can be rewritten as:

SELECT a, 
    @expr1 := (b + c) as expression1,
    (@expr1 + a) AS expression2
FROM very_big_table
WHERE ...
GROUP BY a
ORDER BY a DESC

You can refer to this post.

like image 95
Henry Luo Avatar answered Sep 30 '22 18:09

Henry Luo