t_no name value
1 a 45
1 b 23
1 c 5
1 a 12
1 b 99
1 c 6
I need to show my above table as
no name value1 value2
1 a 45 12
1 b 23 99
1 c 5 6
You can't create dynamic columns in mysql alone, either in scripting language, or you can use group_concat to have them in one column:
SELECT to_no, name, GROUP_CONCAT(value)
FROM table GROUP BY to_no, name
result:
no name value
1 a 45,12
1 b 23,99
1 c 5,6
MySQL does not have a pivot function, but you can use an aggregate function with a CASE expression. Since you have multiple values for each t_no and name, then you could use user defined variables to assign a row number to each group of values:
select t_no, name,
max(case when rn=1 then value end) value1,
max(case when rn=2 then value end) value2
from
(
select t_no, name, value,
#rn:=case when #prev=t_no and #c=name then #rn else 0 end +1 rn,
#prev:=t_no,
#c:=name
from yourtable
cross join (select #rn:=0, #prev:=0, #c:=null) c
order by t_no, name
) d
group by t_no, name
order by t_no, name;
See SQL Fiddle with Demo
Related
I've two tables like this:
First :
id
num
1
a
2
b
3
c
Second:
id
first_id
value
11
1
a1
12
1
a2
13
1
a3
And I need to get result like this:
id
value
1
a1-a2-a3
I've tried with query:
SELECT first.id, (SELECT second.value FROM second
WHERE second.first_id = first.id) AS value
FROM first
But I've got #1242 error. How I can do it?
You can use group_concat()
select first_id as id,
group_concat(`value` order by `value` separator '-') as combined_values
FROM second_table
group by first_id
I have a table column like following
Name
a
b
c
d
e
f
Now I want to Divide this column into 3 columns like as
Name1 | Name2 | Name3
a | c | f
b | d | e
How will the SQL Query for this?
On MySQL 8+, you could use ROW_NUMBER for this purpose:
WITH cte AS (
SELECT Name, ROW_NUMBER() OVER (ORDER BY Name) - 1 rn
FROM yourTable
)
SELECT
MAX(CASE WHEN FLOOR(rn / 2) = 0 THEN Name END) AS Name1,
MAX(CASE WHEN FLOOR(rn / 2) = 1 THEN Name END) AS Name1,
MAX(CASE WHEN FLOOR(rn / 2) = 2 THEN Name END) AS Name3
FROM cte
GROUP BY
rn % 2
ORDER BY
rn % 2;
Demo
This approach has an advantage over a union in that it can easily be extended to support more rows and columns as you need.
I need to write a select statement that will rewrite the table in the following manner... I'm not sure how to go about this using MySQL.
Example of table
user_id date a b c
123456 2020-01-01 1 1 1
234567 2020-03-04 1 0 0
453576 2020-05-05 1 0 1
Desired result
user_id date results
123456 2020-01-01 a
123456 2020-01-01 b
123456 2020-01-01 c
234567 2020-03-04 a
453576 2020-05-05 a
453576 2020-05-05 c
In MySQL you can unpivot with union all, while filtering on 1 values:
select user_id, date, 'a' as result from mytable where a = 1
union all select user_id, date, 'b' from mytable where b = 1
union all select user_id, date, 'c' from mytable where c = 1
order by user_id, date, result
If you have a large amount of data or your "table" is really a complex query (say a subquery or view), then unpivoting is usually faster with cross join than with union all:
select t.user_id, t.date, r.result
from t cross join
(select 'a' as result union all
select 'b' as result union all
select 'c' as result
) r
where (t.a = 1 and r.result = 'a') or
(t.b = 1 and r.result = 'b') or
(t.c = 1 and r.result = 'c') ;
For a single smallish table, performance probably doesn't matter.
I have the following [table a]
id result
1 a
1 b
1 b
1 c
2 e
2 e
2 e
2 f
I'm getting the following after doing a group_concat
select id , Group_Concat(result) from [table a]
group by id
id result
1 a,b,b,c
2 e,e,e,f
BUT i want to display the no of times a value occurs before the value in the result set to avoid redundancy like the following
id result
1 a,2 b,c
2 3 e,f
How can I achieve it ?
Group by ID and result first to get the count. Then group by ID to build your strings.
select
id,
group_concat(case when cnt = 1 then result else concat(cnt, ' ', result) end) as results
from
(
select id, result, count(*)
from mytable
group by id, result
) t
group by id;
I have data in database like this
id class gender
1 A F
2 B F
3 A M
4 A F
5 A M
6 B M
7 A F
From this data I want to make select statement to produce report like this
_________________________
Gender
class M F Total
_________________________
A 2 3 5
B 1 1 2
_________________________
TOTAL 3 4 7
How can I make that select statement ?
Have a look at the following example
SQL Fiddle DEMO
SELECT class,
SUM(CASE WHEN gender = 'M' THEN 1 ELSE 0 END) `M`,
SUM(CASE WHEN gender = 'F' THEN 1 ELSE 0 END) `F`,
COUNT(1) Total
FROM Table1
GROUP BY class
To get totals for each gender:
SELECT class, gender, COUNT(*) as gender_count
FROM Gender
GROUP BY class, gender;
To get total:
SELECT class, COUNT(*) as total_count
FROM Gender
GROUP BY class;