Using an Alias in SQL Calculations
Asked Answered
P

5

81

Why won't this query work?

SELECT 10 AS my_num, my_num*5 AS another_number
FROM table

In this example, I'm trying to use the my_num alias in other calculations. This results in unknown column "my_num"

This is a simplified version of what I am trying to do, but basically I would like to use an alias to make other calculations. My calculations are much more complicated and thats why it would be nice to alias it since I repeat it several times different ways.

Pyoid answered 16/1, 2010 at 13:38 Comment(0)
H
197

Simply wrap your reused alias with (SELECT alias) :

SELECT 10 AS my_num, 
       (SELECT my_num) * 5 AS another_number
FROM table
Honour answered 2/8, 2013 at 15:38 Comment(7)
I am wondering where that functionality is documented. I've searched in the whole MySQL documentation and I have not found it. I don't know how this "alias reusing" works and I am curious about portability or performance impact. If someone knows more about this I will appreciate a link. Thanks you.Jeavons
this just made my week.. maybe month. super useful especially with aliases built from flow control statements like IF(), CASE(), etcCurtis
@CarlosGant: The (SELECT alias) part is treated as a "dependent subquery", at least according to MySQL's EXPLAIN and Visual Explain functions.Vita
Nevermind, it didn't work for me. I got "unknown column".Oliviaolivie
SELECT kabupaten.nama AS kabupaten, kecamatan.nama AS kecamatan, SUM(pihaks.luas) AS target_luas, SUM(realisasi.luas) AS realisasi_luas, SELECT(realisasi_luas)/SELECT(target_luas)*100 AS persentase_luas. Tried like this but not workingCoccidiosis
This doesn't work for me if I try to use SELECT multiple times. SELECT Users.Id, SUM(Posts.Score) AS ScoreSum, SUM(Posts.ViewCount) AS ViewSum, (SELECT(ViewSum) / SELECT(ScoreSum)) AS ViewScoreRatio says "Incorrect syntax near SELECT" for the second SELECT.Hogtie
In terms of portability this does not appear to work in postgresql 14Adjutant
M
11

You'll need to use a subselect to use that aliases that way

SELECT my_num*5 AS another_number FROM
(
    SELECT 10 AS my_num FROM table
) x
Mccallion answered 16/1, 2010 at 13:42 Comment(1)
@RyanHeneise, it's a subselect alias; that way you could go with x.my_numMccallion
H
6

Aliases in sql are not like variables in a programming language. Aliases can only be referenced again at certain points (particularly in GROUP BY and HAVING clauses). But you can't reuse an alias in the SELECT clause. So you can use a derived query (such as suggested by Rubens Farias) which lets you basically rename your columns and/or name any computed columns.

Or you could use a VIEW if your formulas are generally fixed

CREATE VIEW table10 AS SELECT 10 AS my_num FROM table;
SELECT my_num * 5 AS another_number FROM table10;

I believe that will be slightly faster than using a derived query but it probably depends a lot on your real query.

Or you could just duplicate the work:

SELECT 10 AS my_num, 10 * 5 AS another_number FROM table;

Which might be convenient in something like php/perl:

my $my_num = 10;
my $query = "SELECT $my_num AS my_num, $my_num * 5 AS another_number FROM table";
Hemophilia answered 16/1, 2010 at 18:22 Comment(0)
R
0

``Another option is to use the APPLY operator

SELECT my_num, my_num*5 AS another_number
FROM table
CROSS APPLY 
(SELECT 5 AS my_num) X
Recalcitrate answered 30/8, 2016 at 19:34 Comment(1)
Note that CROSS APPLY does not appear to be available in MySQL (yet).Arabic
M
0

Easy peasy. You can use variables.

SELECT @my_num := 10 AS my_num, @my_num*5 AS another_number;
SHOW WARNINGS;

This will throw a warning: "Setting user variables within expressions is deprecated and will be removed in a future release. Consider alternatives: 'SET variable=expression, ...', or 'SELECT expression(s) INTO variables(s)'."

Microreader answered 11/9, 2023 at 1:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.