I'm trying to merge different old values in one single column.
I have this table
id code langtype desc duration
232 1104466 1 IT text 10
233 1104466 2 EN text 10
234 1104466 6 other desc 10
235 1104466 1 Other IT text(different row) 10
236 1104466 2 Other EN text(same row of previous) 10
And i would like to obtain a result like this
id code desc duration
232 1104466 “IT” = “IT TEXT”, EN=”EN TEXT”, “ES”=”Other desc” 10
It is possibile is mysql?
Thank you
Yes, it is possible. Something like this should achieve this effect:
select group_concat(concat(langtype, ' = ', desc))
from table
group by code
Here we first form a value to be extracted from each row (concat(langtype, ' = ', desc)) and then concatenate it for each grouped row (group_concat). You can change delimiters, or add quotes where you need - look at GROUP_CONCAT and CONCAT docs.
Related
Given a table like this:
id number anotherNumber
1 1 10
2 1 20
3 2 20
4 2 10
5 3 10
If I run the query:
Select *, GROUP_CONCAT(number)
FROM docs
GROUP BY number
I will get:
id number anotherNumber GROUP_CONCAT(number)
1 1 10 1,1
3 2 20 2,2
5 3 10 3
see sql fiddle
However I want to get:
id number anotherNumber GROUP_CONCAT(number)
1 1 20 1,1
3 2 20 2,2
5 3 10 3
Basically I want the numbers in the anotherNumber column to be sorted in DSEC order - it should always be the highest one.
I know you can put an ORDER BYin the GROUP_CONCAT but this will only affect the concatenated values, not the "merged ones". So is there a simple way?
You could use:
Select MIN(id) AS ID, number, MAX(anotherNumber) AS anotherNumber,
GROUP_CONCAT(number)
FROM docs
GROUP BY number;
SQLFiddle Demo
You should always wrap columns that are not specified in GROUP BY with aggregate function(unless they are functionally dependent on GROUP BY columns and your RDBMS supports ANSI-99 Optional feature T301, Functional dependencies)
The following code is the easy way to get the desired result:
SELECT id, number, MAX(anotherNumber) AS anotherNumber, GROUP_CONCAT(number)
FROM docs
GROUP BY number;
I have also attached my SQLFiddle.
You can check it. Good Luck!
using this command i can get TOP four values but some of the values are duplicate . so then wanted to swap based on serial number of table . using this bellow command to get the top four values now i want to swap any logic.
INPUT
SN Set name columname
1 100 Randy 25
2 100 many 22
3 100 sanny 22
4 100 nanny 35
Output
SN Set name columname
2 100 many 22
3 100 sanny 22
1 100 Randy 25
4 100 nanny 35
select top 4 * from filename where Set=100 order by columname DESC
Sort it based on column name then swap it based on serial number.
Wrap your query returning the wanted 4 rows up in a derived table, then do the opposite ORDER BY to get the wanted order:
select *
from
(
select top 4 * from filename where Set=100 order by columname DESC
) dt
order by columname ASC
There are few rows:
Id marks
1 15
1 16
1 17
2 6
2 15
3 9
3 10
I want to merge all the id into a single row, with
output like below:
Id marks1 marks2 marks3
1 15 16 17
2 6 15
3 9 10
Concatenating fields into a single field is made really easy in mysql. there is function for it. CONCAT(). Better yet if you and want to delimit them by the same string you can use CONCAT_WS(). which is concatenate with separator, the first parameter being the separator and any other argument after being a field you wish to join.
http://www.w3resource.com/mysql/string-functions/mysql-concat_ws-function.php
SELECT id, CONCAT_WS(' ', marks1, marks2, marks3) as 'mark1 | mark2 | mark3' from my_table group by id;
UPDATE
I think I Misunderstood your schema structure. I think what you actually are looking for is GROUP_CONCAT().
http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php
SELECT id, GROUP_CONCAT(marks) as 'All Marks' from my_table group by id;
I have in Sql a column name like that:
SpecificationParagraph
1
2
3
4
5
...
179
With my program i insert between row 1 and 2 a new row. My column are now :
SpecificationParagraph
1
3
4
5
...
179
2
When i try to order asc like that:
SELECT SpecificationParagraph
FROM CP_Sequence
ORDER BY SpecificationParagraph
My column get that order:
1
10
100
101
102
...
99
I want to order form 1 to 180. My logical to my program in vb is: when I insert a new row like 2, replace current 2 from SpecificationParagraph and after increment +1 all following lines.
you have to change datatype of the field to int, tinyint or bigint
You have probably created SpecificationParagraph field as a varchar or text, therefore the order by dorts it as text, not as a number. Change the data type to an integer type and the ordering will be fine.
In MySQL I have one particular cell with data something like this
5,6,7,8,9
If I need to search for specific 2 numbers one after another I do a query with LIKE statement for those 2 particular numbers. For ex. I need to check if there's a row with numbers 6 & 7 *in a row* I do
SELECT * FROM table WHERE column LIKE '%,6,7,%' OR column LIKE '%,6,7' OR column LIKE '6,7,%'
It's little redundant and clumsy. If I 'convert' those numbers into multiple rows, for ex. every number would become it's own row with column 'numbers' ordered with 'sort' column so I know the order of rows.
id numbers sort
55 8 4
56 6 2
57 5 1
58 7 3
59 9 5
...
What's the identical query for this case? So I would have the same result as with the query above. I need to order the query with sort column and check if the numbers 6,7 are occurring one after another with that sorting.
Should be something like this. (if I understood right your problem). It will return nothing if the 2 numbers are not in sequence by the sort column.
select *
from table t1
join table t2 on t1.sort=t2.sort+1
where t1.numbers=6 and t2.numbers=7
if you do not know which one should be first you can use it like this:
select *
from table t1
join table t2 on t1.sort=t2.sort+1 or t1.sort+1=t2.sort
where t1.numbers=6 and t2.numbers=7
Are the numbery always incremented by one or can they have any value?
I'm proposing the following table structure
id col1 col2 rowid
1 1 2 1
2 2 3 1
3 1 4 2