I have five columns in a table:
id, a, b, c, d
My main query is:
SELECT MAX(a * (b/100)) AS result FROM mytable;
I'm trying to also display columns c and d that are relative to the row where MAX is pulled from. I'm finding it hard because the MAX query is using two columns rather than a single one. Any help is appreciated. thanks!
You could use a where on subselect
select *
from mytable
cross join (
SELECT MAX(a * (b/100)) AS result
FROM mytable
) t where (a * (b/100)) = t.result
Related
I have a table with 3 dimensions- A, B, and C.
I essentially want values of all possible combinations for these dimensions and populate all measures(M) as 0 when a combination isn't present.
Suppose I have the table-
If I do this I get -
select a,b,c from sum(m) fact group by a,b,c
But I would like all possible combinations, -
Currently, I a doing a cross join like below, but is there some faster way to do this (as my table has about ~1M records)? -
select * from (
select distinct f1.a, f2.b, f3.c
from fact f1
cross join fact f2
cross join fact f3 ) all
left join
( select a,b,c from sum(m) fact group by a,b,c) s
on all.a=s.a and all.b=s.b and all.c=s.c
If this is Oracle Database, then this is exactly what cube is for.
select a, b, c, sum(m)
from my_table
group by cube(a,b,c)
MySQL:
GROUP BY a,b,c will produce 1 row per combination that exists in the table.
If you want all possible combinations, you need to build 1 (or 3) more tables to list all the possible values, then do a LEFT JOIN from them. You may also want COALESCE(col, 0) to turn NULLs into zeros.
I want to multiply two columns in a query
Example : Column A * Column B and have the result be displayed in Column C
I assume you have a table YourTable containing the columns A and B and that the columns A and B also should be displayed in the resulting query.
So here it is:
Select A, B, A * B As C From YourTable
If not, use just this:
Select A * B As C From YourTable
Let's say I have columns a, b in a table in a MySQL database. What I'm trying to do is to select the distinct values of a with an arbitrary value of b - let's say the first one, but I actually don't care which one.
Something like the query below will give me all distinct values on both columns, so it is not good for me (too many results in my case).
SELECT DISTINCT a, b
FROM my_table;
Any suggestions?
In case I want 2 values of b for each a value, how is that possible?
Use the GROUP BY feature, like:
SELECT a, b
FROM my_table
GROUP BY a;
See my SQL Fiddle.
UPDATE
No DISTINCT is needed at all.
Thanks to dnoeth for the suggestion.
This is just a guess from what I think your'e trying to do:
SELECT DISTINCT a as distinct,
( SELECT b FROM my_table WHERE c = a LIMIT 1 ) as arbitary
FROM my_table;
I have the following query
SELECT *
FROM(
(SELECT
MAX(c.start_time) as start_1
FROM
c1 c)
UNION ALL
(SELECT
MAX(cc.created_at) as ccmax
FROM
cc1)
) as t
I'd like to have the result in a table with 2 columns start_1 and cmax instead of the single column I get with all the different results listed.
How should I do it? I ended up in a subselect believing this would have done the job.
For the data to be in two columns you would have to use a sub select.
SELECT
MAX(c1.start_time) as start_1, (SELECT MAX(cc1.created_at) FROM cc1) as ccmax
FROM c1
I am trying to join the outputs of two or more mysql queries. So if Query 1 has N columns and Query 2 has M columns, then the output should have N+M columns.
As an example:
select * from (select 1,2,3) as X,
(select 4,5) as Y;
The output here is :
1 2 3 4 5
Now the issue is that my second query may produce no results. This case results in no output at all:
select * from (select * from table_0) as X,
(select * from table_1) as Y;
If table_1 returns no matches, the combined output returns no rows.
I would still like to get the entries of my first table returned.
While I have a workaround, it involves individual queries for each of the M Columns.
I also do not want to create temporary tables and join them.
A LEFT JOIN should do it:
select *
from (select * from table_0) as X
left join (select * from table_1) as Y on 1;