Oracle SELECT - alias of one column as an input to another [duplicate] - sql

This question already has answers here:
Oracle: Using Pseudo column value in the same Select statement
(3 answers)
Closed 9 years ago.
Folks I found similar but not exact questions on this forum - pardon me if i have not done enough searching for them. This is my question..in Oracle
select ( t.value*2) as inst2, (inst2 * 3) as inst3
from table t;
the thinking behind is if f() = t.value*2 is an expensive call, then we do not need to make that twice..or is there an alternative query structure i could use (I am trying to achieve this in CTAS)
thanks in advance.

Another option:
with cte as (
select t.value*2 as inst2
)
select
cte.inst2,
(cte.inst2*3) as inst3
from cte
This is actually the same as in bluefeet's reply, but I would consider it easier to understand with the with-syntax.

If you want to use an alias in the second calculation, then you will want to use a subquery:
select inst2,
(inst2 * 3) as inst3
from
(
select t.value*2 as inst2
from table t
)

Related

Any way to nest WITH..AS? [duplicate]

This question already has answers here:
How to use multiple CTEs in a single SQL query?
(4 answers)
Closed 5 years ago.
I'm trying to do something like:
WITH megalodon_view AS (
-- 200 lines of gibberish
)
WITH RECURSIVE traverse_table AS (
-- big queries with multiple uses of megalodon_view for recursive traversing
)
Observe it's not a mather of defining 2 CTEs, but to use on within the scope of the other.
I'd like to use it in production, so I want preferrably not to create anything physically
You can specify this as:
WITH recursive megalodon_view AS (
-- 200 lines of gibberish
),
traverse_table AS (
-- big queries with multiple uses of megalodon_view for recursive traversing
)
select . . .;
The with recursive is needed only once, for the first CTE (even though that one is not recursive).
Without exact query it is hard to say what are you trying to achieve. Anyway you could nest WITH like:
WITH RECURSIVE t(n) AS (
SELECT *
FROM (WITH cte AS (SELECT 1 )
SELECT * FROM cte
) sub
UNION ALL
SELECT n+1 FROM t WHERE n < 100
)
SELECT COUNT(*) FROM t;
DBFiddle Demo

Why can we not use the Where Clause on a Grouped statement? [duplicate]

This question already has answers here:
SQL - WHERE Condition on SUM()
(4 answers)
Closed 5 years ago.
For a simplified example of my issue, why could I not do this?
select id_number, sum(revenue)
from table A
where sum(revenue)>1000
group by id_number
(In case this causes any confusion, why can I not only return the results that have over 1000 in revenue?)
Disclaimer, I'm somewhat new to SQL but couldn't find any documentation regarding this.
Thanks,
This is by design of SQL. By using WHERE You filter the source table. And the sequence of statement fragments is as written. That means You would like to filter the SUM which is applied on filtered table. That means You must use filter on already grouped result using HAVING clause. Use
select id_number, sum(revenue)
from table A
group by id_number
having sum(revenue) > 1000
Simple answer is because the WHERE clause is evaluated before the aggregation clause. Therefore, you are trying to filter based on something that doesn't exist yet. However, you can solve that problem by making it exist first. Write a subquery, then select from that:
WITH RevenueTotals AS (SELECT id_number, sum(revenue) AS Rev_Total
FROM table A
GROUP BY id_number)
SELECT id_number, Rev_Total
FROM RevenueTotals
WHERE Rev_Total > 1000

MS Access select distinct random values

How can I select 4 distinct random values from the field answer in MS Access table question?
SELECT TOP 4 answer,ID FROM question GROUP BY answer ORDER BY rnd(INT(NOW*ID)-NOW*ID)
Gives error message:
Run-time error '3122': Your query does not include the specified
expression 'ID' as part of an aggregate function.
SELECT DISTINCT TOP 4 answer,ID FROM question ORDER BY rnd(INT(NOW*ID)-NOW*ID)
Gives error message:
Run-time error '3093': ORDER BY clause (rnd(INT(NOWID)-NOWID))
conflicts with DISTINCT.
Edit:
Tried this:
SELECT TOP 4 *
FROM (SELECT answer, Rnd(MIN(ID)) AS rnd_id FROM question GROUP BY answer) AS A
ORDER BY rnd_id;
Seems to work sofar..
I suggest:
SELECT TOP 4 answer
FROM question
GROUP BY answer
ORDER BY Rnd(MIN(ID));
I don't think the subquery is necessary. And including the random value on the SELECT doesn't seem useful.
I've creted a simple quiz application 2 years ago, and this is the query that I use to get a random question from the table.
SELECT TOP 4 * FROM Questions ORDER BY NEWID()

using row from first part of SQL union in second part

To implement a multiple choice quiz I want to select an answer from my answers table (the "correct" answer) and two "incorrect" answers. Can I do it in one query. I feel I should be able to but I'm not quite getting it. Here is what I have so far:
SELECT correct.answer
FROM (
SELECT answer
FROM answers
ORDER BY random()
LIMIT 1
) correct
UNION
SELECT answer
FROM (
SELECT DISTINCT answer
FROM answers
WHERE answer != correct.answer
ORDER BY random()
LIMIT 2
);
The database engine is SQLite 3 which gives me:
Error: no such column: correct.answer
I can do it with two separate queries but like I said, one should be possible no?

Multiple result-sets using WITH clause [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Multiple Select Statements using SQL Server 2005 “WITH” Statement
There's any way to get multiple result-sets from a query using WITH clause?
I'm using MS SQL 2005.
;with temp as
(
SELECT '1' as [Sample]
)
--SELECT COUNT(*) FROM temp
SELECT * FROM temp
This works with each select (count or *), but I need to return both result-sets from the same "temp".
Is that possible?
Use UNION ALL:
;with temp as
(
SELECT '1' as [Sample]
)
SELECT COUNT(*) FROM temp
UNION ALL
SELECT * FROM temp
I'd suggest you need to re-work whatever was meant to consume this pair of result sets so that it doesn't need to know how many results are going to be returned before the results arrive. If that's not the case, I can't think of where else you'd need to retrieve the COUNT of the result set as well as the result set itself.