Select inside math function - mysql

This is most likely a beginner's question in SQL. Is it possible to use a select within a math expression?
For example, I have two tables:
- table A with a column named id (primary key) and another column named val_A
- table B with a column named id (primary key) and another column named val_B
I want to do something like:
select ((select val_A from A where id = 1) +
(select val_B from B where id = 1)) as final_sum;
I'm using MySQL and it is throwing errors. I'm assuming that this is because the result of a select is a set and I want the numeric value of val_A and val_B to be make the sum.
Is there any way of doing this?
Thanks!

The query that you have:
select ((select val_A from A where id = 1) +
(select val_B from B where id = 1)
) as final_sum
is correctly formed SQL in MySQL (assuming that the table and columns exist).
However, it assumes that each subquery only returns one row. If not, you can force it using limit or a function like min() or max():
select ((select val_A from A where id = 1 limit 1) +
(select max(val_B) from B where id = 1)
) as final_sum
Or, possibly, you are trying to get the sum of all the rows with id = 1 in both tables:
select ((select sum(val_A) from A where id = 1) +
(select sum(val_B) from B where id = 1)
) as final_sum

Yes you can do that, but a more proper query format would be:
SELECT (a.val_a + b.val_b) as final_sum
FROM a INNER JOIN b ON a.id = b.id
WHERE a.id = 1

I'm not sure why it's not working, but you could try something like:
select (val_A + val_B) as final_sum from A,B where A.id=1 and B.id=1;

Break down and test your query
select 1+1
so your statement is just without the select. This would run -
select ((select sum(val_A) from A where id = 1) +
(select sum(val_B) from B where id = 1)) as final_sum;

Related

SQL - Split a column into two columns in Mysql

I have this table. Considering the id starts from 0.
Table 1
ID Letter
1 A
2 B
3 C
4 D
6 E
I need following output
Col1 Col2
NULL A
B C
D NULL
E NULL
I tried using union with id, id - 1 and id + 1, but I couldn't figure out how to get letter based on ids, also tried even odd logic but nothing worked.
Any help is appreciated.
Thank you
You didn't post the database engine, so I'll assume PostgreSQL where the modulus operand is %.
The query should be:
select o.letter, e.letter
from (
select id, letter, id as base from my_table where id % 2 = 0
) o full outer join (
select id, letter, (id - 1) as base from my_table where id % 2 <> 0
) e on e.base = o.base
order by coalesce(o.base, e.base)
Please take the following option with a grain of salt since I don't have a way of testing it in MySQL 5.6. In the absence of a full outer join, you can perform two outer joins, and then you can union them, as in:
select * from (
select o.base, o.letter, e.letter
from (
select id, letter, id as base from my_table where id % 2 = 0
) o left join (
select id, letter, (id - 1) as base from my_table where id % 2 <> 0
) e on e.base = o.base
union
select e.base, o.letter, e.letter
from (
select id, letter, id as base from my_table where id % 2 = 0
) o right join (
select id, letter, (id - 1) as base from my_table where id % 2 <> 0
) e on e.base = o.base
) x
order by base
Just use conditional aggregation:
select max(case when id % 2 = 0 then letter end) as col1,
max(case when id % 2 = 1 then letter end) as col2
from t
group by floor(id / 2);
If you prefer, you can use mod() instead of %. MySQL supports both.

Using derived subquery result in an other query

I want to use first subquery result in next select. But it does not see row1 as it is not a table in database. How can I solve this problem?
UPDATE transactions, (SELECT
card_id,
id,
card_sum_before,
card_sum_after,
amount,
dt
FROM transactions
WHERE status = 1 AND
card_id = 1
ORDER BY dt ASC) AS counted_table,
(SELECT
row2.id AS id,
(row1.card_sum_after - row2.amount - row2.card_sum_after) AS difference
FROM counted_table AS row1
INNER JOIN counted_table AS row2 ON row2.id = row1.id + 1
ORDER BY row1.dt ASC) selected
SET transactions.difference = selected.difference
WHERE transactions.id = selected.id`
Please do something like, Store first query output to temp table and then use that temp table in any query you want.

update row if count(*) > n

my DB has this structure:
ID | text | time | valid
This is my current code. I'm trying to find a way to do this as one query.
rows = select * from table where ID=x order by time desc;
n=0;
foreach rows{
if(n > 3){
update table set valid = -1 where rows[n];
}
n++
}
I'm checking how many rows exist for a given ID. Then I need to set valid=-1 for all rows where n >3;
Is there a way to do this with one query?
You can use a subquery in the WHERE clause, like this:
UPDATE table
SET valid=-1
WHERE (
SELECT COUNT(*)
FROM table tt
WHERE tt.time > table.time
AND tt.ID = table.ID
) > 3
The subquery counts the rows with the same ID and a later time. This count will be three or less for the three latest rows; the remaining ones would have a greater count, so their valid field would be updated.
Assuming that (id,time) has a UNIQUE constraint, i.e. no two rows have the same id and same time:
UPDATE
tableX AS tu
JOIN
( SELECT time
FROM tableX
WHERE id = #X -- the given ID
ORDER BY time DESC
LIMIT 1 OFFSET 2
) AS t3
ON tu.id = #X -- given ID again
AND tu.time < t3.time
SET
tu.valid = -1 ;
update table
set valid = -1
where id in (select id
from table
where id = GIVEN_ID
group by id
having count(1) >3)
Update: I really like dasblinkenlight's solution because is very neat, but I wanted to try also to do it in my way, a quite verbose one:
update Table1
set valid = -1
where (id, time) in (select id,
time
from (select id,time
from table1
where id in (select id
from table1
group by id
having count(1) >3)
-- and id = GIVEN_ID
order by time
limit 3, 10000000)
t);
Also in SQLFiddle
to do it for all ids, or only for one if you set a where in the a subquery
UPDATE TABLE
LEFT JOIN (
SELECT *
FROM (
SELECT #rn:=if(#prv=id, #rn+1, 1) AS rId,
#prv:=id AS id,
TABLE.*
FROM TABLE
JOIN ( SELECT #prv:=0, #rn:=0 ) tmp
ORDER BY id, TIMESTAMP
) a
WHERE rid > 3
) ordered ON ordered.id = TABLE.id
AND ordered.TIMESTAMP = TABLE.TIMESTAMP
AND ordered.text = TIMESTAMP.text
SET VALID = -1
WHERE rid IS NOT NULL

MYSQL: Return results ONLY for the first true encountered SELECT expression

Desired result:
Return results ONLY for the first true encountered SELECT expression.
Explanation:
So, I have three different SELECT expresions:
SELECT * FROM table WHERE column1 = 'sometext' AND column2='1'
SELECT * FROM table WHERE column1 = 'someothertext' AND column2='2'
SELECT * FROM table WHERE column1 = 'somethirdtext' AND column2='3'
I want to have the results from 1. If 1. is returning NULL, I would like result from select number 2. If Select number 2. is returning NULL, I would like to use select number 3. and so on.
Please note that I am expecting more than one row to be returned for each condition that is true - and I only want the result from either SELECT 1) 2) or 3) (in that order)
It is important to only return results from the one single SELECT expression, so even if 2. and 3. would return something, I would only like results from 1.
The code I have right now is following that expected logic BUT when a I have more than one rows being returned by some of the below SELECTS, it gives me error:
1242 - Subquery returns more than 1 row
The code right now:
SELECT IFNULL( (SELECT * FROM table WHERE column = 'sometext'), IFNULL( (SELECT * FROM table WHERE column = 'someothertext'), IFNULL( (SELECT * FROM table WHERE column = 'somethirdtext'), 0 ) ) )
You're looking for COALESCE function.
SELECT COALESCE(
(SELECT col FROM t WHERE `column` = 'sometext'),
(SELECT col FROM t WHERE `column` = 'someothertext'),
(SELECT col FROM t WHERE `column` = 'somethirdtext')
);
-please, note that subquery should not return more than 1 row/column.
I would approach this slightly differently, since you can only return one row per condition anyway, I would use the following to limit the number of selects done:
SELECT *
FROM table
WHERE column IN ('sometext', 'someothertext', 'somethirdtext')
ORDER BY CASE column
WHEN 'sometext' THEN 1
WHEN 'someothertext' THEN 2
WHEN 'somethirdtext' THEN 3
END
LIMIT 1;
As pointed out in the comments, you can use FIELD for the sort too:
SELECT *
FROM table
WHERE column IN ('sometext', 'someothertext', 'somethirdtext')
ORDER BY FIELD(column, 'sometext', 'someothertext', 'somethirdtext')
LIMIT 1;
I think you can get multiple rows per condition using the following:
SELECT T.*
FROM Table T
INNER JOIN
( SELECT Column
FROM Table
WHERE column IN ('sometext', 'someothertext', 'somethirdtext')
ORDER BY FIELD(column, 'sometext', 'someothertext', 'somethirdtext')
LIMIT 1
) MinT
ON MinT.Column = T.Column;
Basically the subquery MinT does the same as before, ordering by whichever condition matches. Then gets the value for the column of the first match and limits the whole table to this value.
Example on SQL Fiddle
SELECT t.*
FROM
( SELECT o.column1, o.column2
FROM
( SELECT 1 AS ord, 'sometext' AS column1, '1' AS column2 UNION ALL
SELECT 2, 'someothertext', '2' UNION ALL
SELECT 3, 'somethirdtext', '3'
) AS o
WHERE EXISTS
( SELECT 1
FROM table AS td
WHERE td.column1 = o.column1
AND td.column2 = o.column2
)
ORDER BY o.ord
LIMIT 1
) AS d
JOIN
table AS t
ON t.column1 = d.column1
AND t.column2 = d.column2 ;
MySQL isn't my daily db so I might be off on this, but can't you just use LIMIT 1 on your subqueries?

Don't return any result if the precedent query has at least 1 result?

Basically I have this query:
( SELECT * FROM tbl WHERE type = 'word1' )
UNION
( SELECT * FROM tbl WHERE type = 'word2' ) // Run this query if there are no results with type = 1
Basically I would like to run the second query only if the first hasn't any results. is it possible?
The FIRST "PreCheck" query does a count of how many records ARE of type = 1. After that, if the count is greater than 1, then return 1, otherwise return 2.
Now, THAT answer can be used in the join (which is always a single row via COUNT(*)) which will either have a 1 or 2 value. THAT value will be the second value is the EQUALITY conditon. So, if there IS an entry of 1, the result will be as if
WHERE t1.Type = 1
Thus never allowing any 2 in the test. HOWEVER, if NO entries are found, it will have a value of 2 and thus create a WHERE clause of
WHERE t1.type = 2
select t1.*
from
( select if( count(*) > 0, 1, 2 ) IncludeType
from tbl t2
where t2.type = 1 ) preCheck,
tbl t1
where
t1.type = preCheck.IncludeType
If there is an index on the "type" column, the first query should be almost instantaneous.
You could write
select * from tbl
where type = 1
union
select * from tbl
where type = 2
and not exists( select * from tble where type = 1 )
but this probably won't perform as well as just doing it in your program
It does the trick:
SELECT tbl.* FROM tbl JOIN (SELECT min(type) min_type FROM tbl WHERE type between 1 and 2 ) on min_type = type
First, it selects the lesser of these two types, if any exists, and then oins this one number table to your table. It is actually a simple filter. You can use WHERE instead of JOIN, if you want.
SELECT tbl.* FROM tbl WHERE (SELECT min(type) FROM tbl WHERE type between 1 and 2 ) = type