How to get CSV values using sqlite? - sql

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.

Related

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 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 - Where clause: comparing a column with multiple values

Is there any generic command or syntax in SQL to allow one to have multiple values in a where statement without using OR?
OR just gets tedious when you have many values to choose from and you only want say half of them.
I want to return only columns that contain certain values. I am using Cache SQL, but as I said, a generic syntax might be helpful as well because most people are unfamiliar with Cache SQL. Thanks!
You should use IN:
... where column_name in ('val1', 'val2', ...);
Use the 'IN' clause.
SELECT * FROM product WHERE productid IN (1,2,3)
I believe you are looking for the IN clause.
Determines whether a specified value matches any value in a subquery or a list.

MySQL implementation of an iterator

is it a way to implement an iterator in mysql which could return a column as
"1/10"
"2/10"
...
"10/10"
Lets say the form is a/b, as input we have only b. Is it possible to solve this without procedure?
EDIT
An example. We have a relation PRINTS (id, work_id, edition_no) and EDITIONS (id, work_id, size, edition_size). For web-interface I am trying to render a select with possible options for prints. Html-options should look like
<option value="1">1/10</option>
<option value="2">2/10</option>
All my select list I render with SQL from database, but in this case I do not see any possibility to get it from database in way I need it. Sql should return 2 columns with option-value and option-text. That is a question.
SQL is all about relations, that is, tables. The natural way would be to create a one-column table with 10 (or however much you want) consecutive numbers, index it by that column, and select enough numbers from there, using order by and limit.
Use the CONCAT function to concatenate strings in MySQL:
SELECT CONCAT('<option value="', p.edition_no, '">', p.edition_no. '/', e.edition_size, '</option>')
FROM PRINTS p
JOIN EDITIONS e ON e.work_id = p.work_id