CREATE TABLE tmp ( col1 int, col2 int );
INSERT INTO tmp VALUES (1,3), (2,5), (3,7);
SELECT col1, col2, SUM(col2) AS Total FROM tmp; -- ???
The SELECT statement leaves me with this data set:
col1 col2 Total
1 3 15
Is there a way to allow all the rows to appear without introducing a subquery, so that the result is this:
col1 col2 Total
1 3 15
2 5 15
3 7 15
You can use a cross join to avoid a subquery:
SELECT t1.col1, t1.col2, sum(t2.col2) sum_col2
from tmp t1
cross join tmp t2
group by 1, 2
See SQL fiddle
Note that this only works if combinations of col1 and col2 are unique.
Related
I have a collection table1 with the following columns:
id (INT)
col1 (VARCHAR)
col2 (VARCHAR)
value (INT)
I want to calculate the average separately by col1 and by col2 to have a response like this:
{
averageByCol1: {col1Value1: 23, col1Value2: 44},
averageByCol2: {col2Value1: 33, col2Value2: 91}
}
Tried to use multiple columns in GROUP BY, but this combines the columns:
SELECT
CONCAT(col1, col2, AVG(value))
FROM table1
GROUP BY col1, col2
Also tried with subquery but it gives me Subquery returns more than 1 row error:
SELECT
(SELECT
CONCAT(col1, AVG(value))
FROM table1
GROUP BY col1) AS col1Averages,
(SELECT
CONCAT(col2, AVG(value))
FROM table1
GROUP BY col2) AS col2Averages;
Using Mysql v5.5.
edit with sample data:
id col1 col2 value
1 v1 b1 34
2 v2 b1 65
3 v1 b1 87
4 v1 b2 78
5 v2 b2 78
6 v1 b2 12
Want average of value by v1, v2, b1, and b2 independently.
Use a UNION for each column you want to calculate an average for
SELECT col1 as col_key, avg(value) as average
FROM test
GROUP BY col1
UNION
SELECT col2, avg(value)
FROM test
GROUP BY col2
this will work:
select avg(value),col1 from Table1 group by col1
union all
select avg(value),col2 from Table1 group by col2
sql fiddle:http://sqlfiddle.com/#!9/c1f111/5/0
If you want 2 queries for separate results:
SELECT col1, AVG(value) AS average1
from table1
GROUP BY col1
ORDER BY col1
and
SELECT col2, AVG(value) AS average2
from table1
GROUP BY col2
ORDER BY col2
This question already has answers here:
MySQL combining COUNT, MAX and SUM
(3 answers)
Closed 5 years ago.
Let's say I have a table like this:
COL1 COL2
ABC 1
DEF 2
GHI 3
And I want to have the sum of COL2 in a new column COL3:
COL1 COL2 COL3
ABC 1 6
DEF 2 6
GHI 3 6
So taking a simple sum won't work because it will return only 1 value, whereas I want the value to be repeated over all rows.
Try this:
SELECT *
,(SELECT SUM(Col2) FROM Your_Table) Col3
FROM Your_Table
Try this in SQL Server:
SELECT *
,SUM(Col2) OVER(ORDER BY(SELECT NULL)) Col3
FROM Your_Table
Since you didn't specify a dbms, this should work in most (all?) dbms's (tested in sql server):
DECLARE #T TABLE (Col1 VARCHAR(3), Col2 INT)
INSERT #T (Col1, Col2)
VALUES ( 'ABC' , 1 )
, ('DEF', 2)
, ('GHI', 3)
SELECT *
FROM #T
CROSS JOIN (
SELECT SUM(T.Col2) Col3
FROM #T AS T
) X
Though you didn't mention the Server Tag for SQL, I resolved it using MYSQL Server. Try this in MYSQL Server:
Select t.Col1, t.Col2, t1.Col3 from tbl as t join (Select Sum(Col2) as Col3 from tbl) as t1
See the demo here
In order to add the two columns you have to perform the joins query. Base on your operation rather you have to perform inner join or outer join .For more description you can see the respective databases
How can I rearrange a table to get a list of the existing combinations (in both directions) in Mysql?
For example, I have a table with two columns
col1 col2
1 5
7 1
1 2
I want to get a new table (added onto the existing table) where I flip col2 and col1.
col1 col2
1 5
7 1
1 2
5 1
1 7
2 1
This allows me to see all the values for each number, when looking both directions.
Like
1: 5, 2, 7
2: 1
5: 1
7: 1
Hopefully this makes sense.
Thanks for the help!
Use union.
select col1,col2 from tbl
union
select col2,col1 from tbl
union is used instead of union all because the latter would give duplicate rows when a symmetric pair already exists in the table (for example the combination 1,5 5,1)
Then use group_concat.
select col1,group_concat(col2)
from (select col1,col2 from tbl
union
select col2,col1 from tbl) t
group by col1
If you make use off a delivered table you don't have to create a new table.
And you can make use of one query instead of using two queries.
Query
SELECT
col1,
GROUP_CONCAT(col2) AS col2
FROM
(SELECT
col1, col2
FROM
[TABLE]
UNION
SELECT
col2, col1
FROM
[TABLE]
) table_data
GROUP BY
col1
ORDER BY
col1 ASC
Probably a big Title! sorry for that.. :(
Table 1
id col1 col2
1 10 one
2 11 two
3 10 three
Now, i would like to write a sql query to get distinct col1 from table1 which doesn't have three in col2. I need the output col1 - 11 only.
I tried like this select distinct col1 from table1 where col2 != 'three' but this gives the result as both 10 and 11. But for 10 it has corresponding row with three as col2 value. Kindly help me to find this.
Use group by and having.
select col1
from table1
group by col
having sum(case when col2='three' then 1 else 0 end)=0
If you are using MySQL, the having condition can be shortened to
select col1
from table1
group by col
having sum(col2='three')=0
as conditions are treated as booleans returning 1 for true and 0 for false.
using not in()
select distinct col1
from table1 t
where col1 not in (
select col1
from table1 i
where i.col2 = 'three'
)
or using not exists()
select distinct col1
from table1 t
where not exists (
select 1
from table1 i
where i.col1 = t.col1
and i.col2 = 'three'
)
How do I structure my query so I can count how many occurrences of a value in column 1 appears in column 2 and then store that result in a new column in the same table? (If a value is duplicated in the first column I still want to store the same value in the new column) For example if I had a table like this:
COL1 COL2
1 2
1 4
2 1
3 1
4 1
4 2
The resulting table will look like this:
COL1 COL2 COL3
1 2 3
1 4 3
2 1 2
3 1 0
4 1 1
4 2 1
Any help is appreciated I am new to sql! Thanks in advance!
Select
col1,
col2,
COALESCE(col3,0) as col3
FROM
mytable
LEFT JOIN
( Select count(*) as col3, col2
from mytable
GROUP BY col2) as temp ON temp.col2 = mytable.col1
And if you want the update (thanks Thorsten Kettner ) :
UPDATE mytable
LEFT JOIN ( Select count(*) as col3, col2
from mytable
GROUP BY col2) as temp ON temp.col2 = mytable.col1
SET mytable.col3 = COALESCE(temp.col3,0)
You can easily count on-the-fly. Don't store this redundantly. This would only cause problems later.
select
col1,
col2,
(
select count(*)
from mytable match
where match.col2 = mytable.col1
) as col3
from mytable;
If you think you must do it; here is the according UPDATE statement:
update mytable
set col3 =
(
select count(*)
from mytable match
where match.col2 = mytable.col1
);
To do that, you can try :
SELECT COL1, COL2, (SELECT COUNT(COL1) FROM `tablename` AS t2
WHERE t2.COL1 = t1.COL1) AS COL3 FROM `tablename` AS t1
Enjoy :)