How can I convert multiple raw values in single column - sql

My data is like this:
But I want to arrange all raw value in one column for a single mobile number like this:

You don't specify the database type you are using, but you do specify excel as a tag. Is this a database question? You also don't provide any names for columns so it's difficult to help.
Oracle has a listagg function which can be used to pivot row values into a list
SELECT column1,
LISTAGG(column2, ', ') WITHIN GROUP (ORDER BY column2) as list_of_values
FROM your_table
group by column1;
Here's a SQLFiddle of it in action.

If you are using a new version of Excel, you may use TEXTJOIN:
Assuming that your data is at range B1:B15, use below formula:
=TEXTJOIN(",",0,B1:B15)

Related

how to get comma separated values in one column for multiple rows that have same id

before
after
tried few functions but didnt work
Some databases (like MySql or MariaDb) support a group_concat() function that can be used in conjunction with GROUP BY:
select
id,
group_concat(distinct value separator ',')
from table
group by id
Some other databases may have a similar function with a different name (STRING_AGG() for example), try to check your db documentation...

Split and Concat Unique SQL comma separated values in column, and then group by

I am trying to write a SQL query that helps me find out the unique amount of "Numbers" that show up in a specific column. Example, in a select * query, the column I want can look like this
Num_Option
9000
9001
9000,9001,9002
8080
8080,8000,8553
I then have another field of "date_available" which is a date/time.
Basically, what want is something where I can group by the "date_available" while combing all the Num_Options on that date, so something like this..
Num_Option date_available
9000,9001,9002,8080 10/22/2020
9000,9002,8080,8000,8553 10/23/2020
I am struggling to figure this out. I have gotten to the possible point of using a python script and matplotlib instead... but I am hoping there is a SQL way of handling this as well.
In Postgres, you can use regexp_split_to_table() in a lateral join to turn the csv elements to rows, then string_agg() to aggregate by date:
select string_agg(x.num, ',') num_option, t.date_available
from mytable t
cross join lateral regexp_split_to_table(t.num_option, ',') x(num)
group by date_available
Of course, this assumes that you want to avoid duplicate nums on the same data (otherwise, there is not need to split, you can directly aggregate).
You may just be able to use string_agg():
select date_available, string_agg(num_option, ',')
from t
group by date_available;
first you have to split the strings into multiple rows with something like split_part('9000,9001,9002',',',1) etc. (use UNION ALL to append the 2nd number etc.), then group them back by availability date with string_agg
if you don't want to hardcode split_part part there is an answer here on how to dynamically split strings in Redshift, look for it

How to get CSV values using sqlite?

I want
How to get this CSV Values in SQLite?
SQLite supports a GROUP_CONCAT function:
SELECT
ModifierId,
GROUP_CONCAT(ModifierOptionName) AS OptionsCSV
FROM yourTable
WHERE
ModifierId = 2
GROUP BY
ModifierId;
Demo
Note: It is not clear why you expect Mod2 for the modifier name, when the three records being aggregated all have different modifier names. If you really expect this, you should explain the logic behind it.

T-SQL using how to use PIVOT function

I have the following table structure in SQL (using T-SQL):
sqlfiddle: http://sqlfiddle.com/#!6/e5edc/1/0
The data would look something like this:
Now I would like to transpose the structure so I get the following:
columns [01_amount] to [12_amount] and columns [01_active] to [12_active] as rows instead of columns
All rows of [initials] to be separate columns
Should look like this:
How would I go about this? The Pivot function looks rather complicated as I'm new to SQL. Can someone help me in the right direction? :-)
Ok you will need first to unpivot your data, which is done in cte. Then you will need to pivot again:
;with cte as(select initials, v, col from main
unpivot(v for col in([01_amount], [02_amount])) u)
select * from cte
pivot(max(v) for initials in([rw],[nb]))p
In unpivot part just add all 24 column names for amounts and active bits. In pivot part just add all possible values for initials.
But if you don't want to manually list all possible values for initials then you will need to make some dynamic query with unpivoting and pivoting.
Here is demo for 2 columns and you will easily expand it http://sqlfiddle.com/#!6/4cf36/2

Unpivot columns from another table [duplicate]

This question already has an answer here:
unpivot with dynamic columns plus column names
(1 answer)
Closed 8 years ago.
I have used Unpivot to get data from a table I am trying to manipulate. I use this query to rearrange my columns to rows;
SELECT Id, ownername, ownervalue
FROM Contacts UNPIVOT (ownervalue FOR ownername IN (column1, column2, column3)) unpiv;
This works great. However I would prefer to get my column names from another table instead of hard-coding them in the query. Ideally i would like this, but it does not work;
SELECT Id, ownername, ownervalue
FROM Contacts UNPIVOT (ownervalue FOR ownername IN (SELECT * FROM ColumnsTable)) unpiv;
Is it possible to get my list of columns from another table like this?
As far as I know (please correct me if I'm wrong) it is not possible to use dynamic columnnames without the use of a dynamic query, which is executed with for example exec.
Take a look at the following question unpivot with dynamic columns plus column names