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

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

Related

How to fetch a range of numbers using like operator

Currently I'm trying to fetch the records which are matching my condition.
I'm using wildcard operator but it's not fetching the records as I expect.
I have multiple records in my table and I'm using the query below:
select *
from My_table
where RegNum like '117[15-24]%'
I thought above query will fetch the records from 11715 to 11724, but currently it's fetching records from 11710 to 11719. I got to know that % wildcard operator will consider single digits only.
Is there any other way to use two digit number in wildcard operator or is there any other solution to fetch the records what I'm looking for?
I speculate that you are using SQL Server. When comparing numerical ranges, the best thing to do is to just use an inequality. If your RegNum column is text, then cast it to integer first and then compare:
SELECT *
FROM My_table
WHERE (CAST RegNum AS int) BETWEEN 11715 AND 11724;
If you want to use LIKE, we might be able to try:
SELECT *
FROM My_table
WHERE RegNum LIKE '1171[5-9]' OR RegNum LIKE '1172[0-4]';
The logic you want is perhaps better captured by:
where left(regnum, 5) between '11715' and '11724'
Not all databases support left(), in those, use the substring function instead.
You have a logic fallacy in wanting to use like for numeric ranges. like is for strings. If you want numeric ranges, use numbers. You could do this with the above condition:
where cast(left(regnum, 5) as int) between 11715 and 11724
But this is logically equivalent to the original string comparison.

SQL Query to pull rows starting with a certain 3 digits

I have a numbers column in my sql table and I want to pull all the numbers that start with 3 specific digits; how would I query this?
Use the "LIKE" query:
123 is the prefix. I think you have to store the numbers as strings though, just try it out on your data set :-)
SELECT * from TableName Where ColumnName LIKE '123%'
See also this Q&A: In MySql, find strings with a given prefix

How can I convert multiple raw values in single column

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)

SQL - just view the description for explanation

I would like to ask if it is possible to do this:
For example the search string is '009' -> (consider the digits as string)
is it possible to have a query that will return any occurrences of this on the database not considering the order.
for this example it will return
'009'
'090'
'900'
given these exists on the database. thanks!!!!
Use the Like operator.
For Example :-
SELECT Marks FROM Report WHERE Marks LIKE '%009%' OR '%090%' OR '%900%'
Split the string into individual characters, select all rows containing the first character and put them in a temporary table, then select all rows from the temporary table that contain the second character and put these in a temporary table, then select all rows from that temporary table that contain the third character.
Of course, there are probably many ways to optimize this, but I see no reason why it would not be possible to make a query like that work.
It can not be achieved in a straight forward way as there is no sort() function for a particular value like there is lower(), upper() functions.
But there is some workarounds like -
Suppose you are running query for COL A, maintain another column SORTED_A where from application level you keep the sorted value of COL A
Then when you execute query - sort the searchToken and run select query with matching sorted searchToken with the SORTED_A column

SQL Replace comma in results without using replace

I feel like this should be simple enough to do, but have not found any solutions that didn't use replace so far. I have the following select statement I am running, and for some of the columns there are commas separating the values. I would like to replace these commas with semicolons, however I only want to do it in the select statement. I don't want it to alter the values in the tables at all. This is not a one off statement either, or I'd just replace all the commas with semicolons and then revert back.
SELECT a.Category_Id, a.Category_Name, ISNULL(b.Category_Alias, '') as Category_Alias,
ISNULL(b.SUPPORT_NAMES, '') as SUPPORT_NAMES
FROM Categories a
INNER JOIN CategoryInfo b on b.Category_Id=a.Category_Id
For the Category_Alias column, the records are actually stored like CS, Customer Support and I want that to show up as CS; Customer Support just for the select statement.
I believe you may be confused as to what the REPLACE function is doing. You can use REPLACE within your SELECT statement without altering the data in the database:
SELECT REPLACE(MyField, ',', ';') AS NewFieldName
FROM MyTable
I believe you don't want to replace the value physically in the table, but ok to replace on select
So you can
Select REPLACE(ColumnName,',',';')
From TableName
Most SQL servers implement an inline replace function. Most of them are named replace(), and can also be used in a select statement.
Example from MySQL:
SELECT field, REPLACE(field,',',';') FROM my_table;