how to achieve concatenation using group by - mysql

Suppose there is a table named 'a' with following data:
col1, col2
-----------
1 1
1 2
1 3
2 2
2 3
3 4
then to achieve following results:
col1, col2
--------------
1 6
2 5
3 4
i can run query like :
select col1, sum(col2) from a group by col1.
But suppose my table is:
col1, col2
---------
1 a
1 b
1 c
2 d
2 e
3 f
here col2 is of varchar type not of numeric type.
what will be the sql query to give following results???
col1, col2
------------
1 a,b,c
2 d,e
3 f
i have tried group by on col1 but how to concatenate values in col2???
the problem is that col2 is of varchar type.

In case of MySQL you can use GROUP_CONCAT like this:
SELECT
col1,
GROUP_CONCAT(col2) as col2
FROM demo
GROUP BY col1;
Here is the sqlfiddle.
In case of SQL Server you can use STUFF like this:
SELECT t1.col1,
stuff((SELECT ',' + CAST(t2.col2 as VARCHAR(10))
FROM demo t2 WHERE t1.col1 = t2.col1
FOR xml path('')),1,1,'') col2
FROM demo t1
GROUP BY t1.col1;
Here is the sqlfiddle.

You can use group_concat function in mysql
select
col1,
group_concat(col2) as col2
from table_name
group by col1

Here is a good example, I ran into a similar issue whilst coding up a schedule (working example: www.oldiesplus.com/schedule/)
Here is the link to my question with answer: https://stackoverflow.com/a/27047139

Related

GROUP BY clause ignoring first column value if second and third are the same

Database server is MySQL
I have an SQL query that does a select with a "GROUP BY" clause as follows:
SELECT col1, col2, col3, SUM(col4), SUM(col5) where col6 = 20 GROUP BY col1, col2, col3
All works well when col2 and col3 have different values but when col2 and col3 have the same value as follows
col1 col2 col3 col4 col5
a 1 2 1 1
b 1 2 2 2
b 1 2 3 3
c 1 2 4 4
the query result is
c 1 2 10 10
I was expecting
a 1 2 1 1
b 1 2 5 5
c 1 2 4 4
Why does the "GROUP BY" ignore the fact that the col1 values are not all the same and just displays that last one it finds?
Thanks in advance
User error - turn out I was aliasing col1 and there was a column of the same name in the table so the GROUP BY was using the column name and not the alias. The column name is question was not even part of the query.
The pitfalls of working on a system that someone else wrote.
Thanks for all your suggestions/comments

Adding a new column in SQL which calculates value from other columns

I was trying to create a new column in SQL which has calculated values from other columns in the same table.
Please see the case below
col1 col2
A 1
A 2
B 1
B 2
B 3
I need to create a new column 'col3' that finds the max of col2 grouped by col1. So the result i want is
col1 col2 col3
A 1 2
A 2 2
B 1 3
B 2 3
B 3 3
I would like to know if a query can be something along the below lines
ALTER TABLE table1 ADD col3 AS (SELECT max(col2) GROUPBY col1)
You don't need an ALTER statement and can get the extra column in your SELECT query like
select col1, col2, max(col2) as col3
from tbl1
group by col1

SQL - Return all unique pairs of values from one column, for each value in another column

Suppose the table looks like
col1 col2
---- ----
1 a
2 a
3 a
4 b
5 b
6 b
and I want to get all pairs from col1 for each value in col2, meaning the result should look like:
col1a col1b col2
---- ----- ----
1 2 a
1 3 a
2 3 a
4 5 b
4 6 b
5 6 b
I have tried to use
temp1 cross join temp2 where temp1.col1 < temp2.col1 order by temp1.col1, temp2.col1
as part of the full query, but it's not returning all the possible combinations. Also I'm not sure how I should write the "for each" part of the command, as in "for each value in col2, create all pairs from the value in col1". Any guidance will be much appreciated.
Maybe this is what you are looking for?
select t1.col1 as col1a, t2.col1 as col1b, t1.col2
from t as t1
join t as t2 on t1.col2 = t2.col2
where t1.col1 < t2.col1
A sample SQL Fiddle gives the same output as your example.

SQL to select distinct values from one column where another column has varying values

I have a huge amount of data. To explain the issue, consider this ultra-minimal set of data:
id col1 col2
-------------------
1 ab 12
2 ab 12
3 ab 12
4 cd 34
5 cd 43
6 ef 34
7 ef 56
8 ef 34
What I need is to select all distinct values in col1 where there is more than one value in col2. So the results from the above would be something like this:
col1
----
cd
ef
Or even better, a row for each unique corresponding value in col2:
col1 col2
------------
ab 12
cd 34
cd 43
ef 34
ef 56
You can do this with a group by and having:
select col2
from t
group by col2
having min(col2) <> max(col2);
If you just want the distinct values, use select distinct:
select distinct col1, col2
from t;
First one, return col1 values having at least two different col2 values:
select col1 from
tablename
group by col1
having count(distinct col2) >= 2
Second, return col1 with col2, when col1 has at least two different col2 values:
select * from tablename
where col1 in (
select col1 from
tablename
group by col1
having count(distinct col2) >= 2)

Comma-separated columns in SQL Server

I have a problem: I have two tables
Table1 which has two columns
Col1 Col2
---- ------
a value1
b value1
b value1
And Table2
Col1 Col2
---- ------
1 a,b
2 a,c
3 a,b,c
I want result
Col1 Col2
----- -----
a 1,2,3
b 1,3
c 2,3
WITH C AS
(
SELECT T2.Col1,
S.Item
FROM Table2 AS T2
CROSS APPLY dbo.SplitStrings(T2.Col2, ',') AS S
)
SELECT C1.Item AS Col1,
(
SELECT ','+CAST(C2.Col1 AS VARCHAR(10))
FROM C AS C2
WHERE C1.Item = C2.Item
ORDER BY C2.Col1
FOR XML PATH(''), TYPE
).value('substring(text()[1], 2)', 'VARCHAR(MAX)') AS Col2
FROM C AS C1
GROUP BY C1.Item
SQL Fiddle
Try this:
NOTE: Not tested
select col1, [col2],
(select col1+',' from Table2 where Col2=ID
group by col1 for xml path('')) AS Col2
From Table1