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`
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
This question already has answers here:
Count boolean field values within a single query
(4 answers)
Closed 4 months ago.
Suppose a table have a column with values like:
column
3
1
1
3
4
1
2
I want to retrieve that how many times there is 1 in column and how many times > 1.
Is there anyway to do this within one query?
I want the data to be fetched like this:
one | greater_than_one
3 | 4
Thanks in advance.
Simple:
select sum(col1=1) as one ,
sum(col1>1) as greater_than_one
from test_tbl;
https://dbfiddle.uk/348f5YOK
This question already has answers here:
Select max value of each group
(8 answers)
How to select a maximum value row in mysql table
(7 answers)
Closed 4 years ago.
So I have a database that has some values like this:
Level , Indicator
4 1
3 2
4 3
3 4
4 5
3 6
What I want to do is to select the highest level value in every indicator.
Is there any sql query that I can use that will generate a result like this?
Level , Indicator
4 1
4 3
4 5
If not, can you help me out using php and mysqli? Thank you so much.
To get Indicators having just highest level value -
select distinct Indicator, level
from your_table
where level = (select max(level) from your_table)
Also, you can use group by to get highest level for each Indicator value -
select Indicator, max(Level) from your_table group by Indicator
This question already has an answer here:
MySQL : Multiple row as comma separated single row
(1 answer)
Closed 9 years ago.
Here is my table information
Id Name
1 aaa
1 bbb
2 ccc
2 ddd
Ids are the repeated one. Now I want the result like
1 aaa,bbb
2 ccc,ddd
How can I do that in Mysql ?
Use GROUP_CONCAT()
select id, group_concat(name) as names
from your_table
group by id
This question already has an answer here:
Mysql query to select a distinct column and the count of a value in another column where column like distinct coumn?
(1 answer)
Closed 9 years ago.
My data table is as below:
ID WEEK RESULT
1 13 GOOD
2 13 BAD
3 13 GOOD
4 13 WORST
5 14 GOOD
6 14 BAD
7 14 WORST
8 15 BAD
9 15 WORST
I need a sql query to create an array as below:
WWEK GOOD_RESULT BAD_RESULT WORST_RESULT TOTAL
13 2 1 1 4
14 1 1 1 3
15 0 1 1 2
Can anyone please help me to find an appropriate mysql query?
SELECT
WEEK,
SUM(RESULT='GOOD') As GOOD_RESULT,
SUM(RESULT='BAD') As BAD_RESULT,
SUM(RESULT='WORST') AS WORST_RESULT,
COUNT(*) As TOTAL
FROM YourTable
GROUP BY
WEEK
Please see fiddle here.