This question already has answers here:
Converting MySQL code to Access: GROUP_CONCAT and a triple JOIN
(1 answer)
Combine values from related rows into a single concatenated string value
(1 answer)
Closed 5 years ago.
I have a the following data set built from query
col 1 col 2 col 3 col 4
a 1 1 1
a 1 1 2
c 1 2 3
c 1 2 4
C 1 5 4
What I would like my export to look like (am trying to move it to excel) is as follows:
col 1 col 2 col 3 col 4
a 1 1 1,2
c 1 2,5 3,4
Is there a way to do this?
Thank you
Related
This question already has answers here:
MySQL transpose columns sum into rows with column names as a value [duplicate]
(1 answer)
MySQL converting columns to rows
(1 answer)
Closed 4 months ago.
I have this table:
ColumnA
ColumnB
ColumnC
ColumnD
1
2
1
3
3
4
3
4
I want a to do a select like this:
Column id
sum
ColumnA_
4
ColumnB_
6
ColumnC_
4
ColumnD_
7
The porpuse of this is to get a column with the comumn id and another with the sum of each column
I've tried to get the sum of the columns but I couldn't get it like vertically
SELECT SUM(ColumnA ) as ColumnA_, SUM(ColumnB ) as ColumnB_, SUM(ColumnC ) as ColumnC_, SUM(ColumnD ) as ColumnD_ FROM table_name
and I get this (not what I want):
ColumnA_
ColumnB_
ColumnC_
ColumnD_
4
6
4
7
Environment :
MySQL 5.7.x
Spring MVC
Table Data (name: TableA)
seq
level
name
order
parent_seq
1
1
name1
1
0
2
1
name2
2
0
3
2
sub1-1
1
1
4
2
sub1-2
2
1
5
2
sub2-1
1
2
6
3
third-2-1
1
5
7
3
third-1-1
1
3
Expected Result
seq
level
name
order
parent_seq
next_level
1
1
name1
1
0
2
3
2
sub1-1
1
1
3
7
3
third-1-1
1
3
2
4
2
sub1-2
2
1
1
2
1
name2
2
0
2
5
2
sub2-1
1
2
3
6
3
third-2-1
1
5
1 (last default value: 1)
Now I'm genenrating expected result with nested for statement(JAVA).
Is there any way to generate expected result only with MySQL Query?
The data stacked in random order in the table is sorted by ASC based on the level column, but check the parent_seq column so that it is sorted under the parent data. And if there are multiple data of the same level, sort by ASC based on the sort column value.
Thanks in advance!
++
EmbraceNothingButFuture's answer was great, but the query seems to work on MySQL 8. I'm using MySQL 5.7. Is there any way to use the query on MySQL 5.7?
Summary:
Use REGEXP_SUBSTR(name,"[0-9]+\-?[0-9]*") to extract the numbers and sort the datas using the numbers.
For MySQL v8 above, you can use LEAD() to generate the "next_level" column based on the "level" column
COALESCE() function for the last default value = 1
SELECT
t1.*,
COALESCE(LEAD(t1.level, 1) OVER(ORDER BY REGEXP_SUBSTR(name,"[0-9]+\-?[0-9]*")), 1) AS next_level
FROM TableA t1
ORDER BY REGEXP_SUBSTR(name,"[0-9]+\-?[0-9]*"), t1.level
See db<>fiddle
I am using 1x3 matrix to store data in mysql like below:-
ID parentID directID
1 0 0 <- This is first ID (So parent is no one),
2 1 1
3 1 1
4 - 3
5 - 4
6 1 1
7 2 1
8 2 1
9 2 1
10 6 1
11 6 2
I am working on a MLM (multi level marketing) project with 1x3 matrix plan.
As I am using 1x3 matrix structure, I have to find the child which doesnot has 3 child below it, given the top ID as 1 (1 is directID of new ID under which it has joined). The New ID that joins the team has to assign a parentID.
So I have a table on a sub report that on the first page has 3 rows of data like this.
(I would post pictures but it wont let me)
Column Column 2 Column 3
1 4 7
2 5 8
3 6 9
Then on the next page the table looks like this.
Column Column 2 Column 3
1 4 7
2 5 8
3 6 9
1 4 7
2 5 8
3 6 9
And so on. Each page has the 3 rows repeated an extra time. The data is all correct at least. Anyone know what could be causing this to happen?
*edited the data to be a little easier to understand
This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 9 years ago.
I am trying to use the stuff function in MS SQL to stuff certain info. Here is the example:
Number Value
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
I would like to stuff the column so that only one record will display as following:
Value Number
1 1,2,3
2 1,2,3
3 1,2
Please note that there are a like n-Numbers and n-Values.
You can use GROUP_CONCAT for this. For example:
SELECT `Value`, GROUP_CONCAT(DISTINCT `Number` ORDER BY `Number`)
FROM `yourTable`
GROUP BY `Value`