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
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 have the following table ("Have").
What would be a proper way to SELECT a table where you sum the value of C where A and B are equal. So that you get the result displayed in "Want".
use group by then sum.
select A, B, sum(c) from your_table group by A, B
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
Lets say I have 2 columns, a and b.
Column A contains these values:
a
b
c
d
Column B contains these values:
e
d
c
b
a
What can I do to get the absolute distinct values from BOTH columns?
Meaning if i want to have a count on both columns, the result is to be 5. Because a, b, c and d are repeated in column B but not e.
SELECT A
FROM YourTable
UNION
SELECT B
FROM YourTable
So here's how I select distinct rows by a combination of multiple columns (a, b and c):
select distinct a,b,c from my_table
This is good, but I need yet another column retrieved for these rows (d) which I can't add to the select part, because then it also plays a role in determining row uniqueness which I don't want.
How can I retrieve an additional column without it affecting row uniqueness?
You can do this using a group by. In MySQL, you can do:
select a, b, c, d
from my_table
group by a, b, c
This chooses an arbitrary value for "d", which would typically (but not guanteed!) be the first value encountered. This uses a feature of MySQL called Hidden Columns.
For code that works in MySQL and other databases, you need to be more explicit:
select a, b, c, min(d)
from my_table
group by a, b, c
Getting an actual random value for d in MySQL is a bit trickier and requires more work. Here is one way:
select distinct a, b, c,
(select d from my_table mt2
where mt.a = mt2.a and mt.b = mt2.b and mt.c = mt2.c
order by rand()
limit 1
) d
from my_table mt
Looks like a job for a join... probably a LEFT JOIN. Something like this:
SELECT DISTINCT L.a, L.b, L.c FROM `my_table` L
LEFT JOIN (
SELECT a, b, c, d FROM `my_table`
) R ON L.a=R.a AND L.b=R.b AND L.c=R.c