Clickhouse, column values to array - sql

I want to make a query then turn the values for each of it's columns into arrays, I've tried finding a way to do this, but until now it has alluded me.
The query is a simple select:
SELECT a,b,c FROM X
Instead of the usual result of say (in the default format):
val_a_1, val_b_1, val_c_1
----------------
val_a_2, val_b_2, val_c_2
-----------------
val_a_3, val_b_3, val_c_3
I want to get an array for each columns, namely:
[val_a_1, val_a_2, val_a_3], [val_b_1, val_b_2, val_b_3] , [val_c_1, val_c_2, val_c_3]
Is this at all possible ?

Nevermind, this can easily be done with groupArray as:
SELECT groupArray(a), groupArray(b), groupArray(c) FROM X
It completely slipped my mind and google didn't help...
leaving this here in case there's a better option or anyone stumbles upon it when searching.

Related

Convert strings into table columns in biq query

I would like to convert this table
to something like this
the long string can be dynamic so it's important to me that it's not a fixed solution for these values specifically
Please help, i'm using big query
You could start by using SPLIT SPLIT(value[, delimiter]) to convert your long string into separate key-value pairs in an array.
This will be sensitive to you having commas as part of your values.
SPLIT(session_experiments, ',')
Then you could either FLATTEN that array or access each element, and then use some REGEXs to separate the key and the value.
If you share more context on your restrictions and intended result I could try and put together a query for you that does exactly what you want.
It's not possible what you want, however, there is a better practice for BigQuery.
You can use arrays of structs to store that information in a table.
Let's say you have a table like that
You can use that sample query to understand how to use it.
with rawdata AS
(
SELECT 1 as id, 'test1-val1,test2-val2,test3-val3' as experiments union all
SELECT 1 as id, 'test1-val1,test3-val3,test5-val5' as experiments
)
select
id,
(select array_agg(struct(split(param, '-')[offset(0)] as experiment, split(param, '-')[offset(1)] as value)) from unnest(split(experiments)) as param ) as experiments
from rawdata
The output will look like that:
After having that output, it's more convenient to manipulate the data

SQL Like filtering

Welcome!
I am currently working on some C# and I encountered a rather silly issue. I need to fill ListBox with some data from database obviously. Problem is varchar filtering. I need to filter codes and display only the right ones.
Example of codes are: RRTT, RRTR, RT12, RQ00, R100, R200, R355, TY44, GG77 etc. Four digit codes.
I managed to filter only R-codes with simple select * from table1 where code_field like 'R%' but I get both R000 and RQ10 and I need to get R ones followed by numbers only.
So from example:
RRTT, RRTR, RT12, RQ00, R100, R200, R355
Only:
R100, R200, R355
Any ideas what should I add to the like 'R%'?
In SQL Server, you can make use of wildcards. Here is one approach:
where rcode like 'R___' and -- make sure code has four characters
rcode not like 'R%[^0-9]%' -- and nothing after the first one is not a number
Or:
where rcode like 'R[0-9][0-9][0-9]'
In other databases, you would normally do this using regular expressions rather than extensions to like.
here solution using like
SELECT *
FROM (
VALUES ( 'R123'),
( 'R12T' ),
( 'RT12' )
) AS t(test)
where test like 'R[0-9][0-9][0-9]'
like 'S[0-9%]'
Friend came up with the solution, thanks anyway

SQLite, check if TEXT field has any alphabetical chars in it

Okay, so I have a huge list of entries, and in one of the columns (for simplicity let's call it num there's a number, something like 123456780000 (they are all the same length and format), but sometimes there are fields that look something like this
12345678E000
or
12345678H000
Now, I need to delete all the rows in which the num column is not entirely numeric. The type of num is TEXT, not INTEGER. So the above examples should be deleted, while 123456780000 should not.
I have tried two solutions, of which one works but is inelegant and messy, and the other one doesn't work at all.
The first thing I tried is
DELETE FROM MY_TABLE WHERE abs(num) == 0.0
Because according to the documentation, abs(X) returns exactly 0.0 if a TEXT value is given and is unconvertable to an real number. So I was thinking it should let all the "numbers-only" pass and delete the ones with a character in it. But it doesn't do a thing, it doesn't delete even a single row.
The next thing I tried is
DELETE FROM MY_TABLE WHERE num LIKE "%A%" OR "%B%" OR "%C%"
Which seems to work, but the database is large and I am not sure which characters can appear, and while I could just do "%D%" OR "%E%" OR "%F%" OR ... with the entire alphabet, this feels inelegant and messy. And I actually want to learn something about the SQLite language.
My question, finally, is: how do I solve this problem in a nice and simple way? Perhaps there's something I'm doing wrong with the abs(X) solution, or is there another way that I do not know of/thought of?
Thanks in advance.
EDIT:
According to a comment I tried SELECT abs(num) FROM MY_TABLE WHERE num like '%A%'
and it returned the following
12345678.0
That's strange. It seems it has split the number where the alphabetical appeared. The documentation claimed it would return 0.0 if it couldn't convert it to a number. Hmm..
You can use GLOB in SQLite with a range to single them out:
SELECT *
FROM MY_TABLE
WHERE num GLOB '*[A-Za-z]*'
See it in use with this fiddle: http://sqlfiddle.com/#!7/4bc21/10
For example, for these records:
num
----------
1234567890
0987654321
1000000000
1000A00000
1000B00000
1000c00000
GLOB '*[A-Za-z]*' will return these three:
num
----------
1000A00000
1000B00000
1000c00000
You can then translate that to the appropriate DELETE:
DELETE
FROM MY_TABLE
WHERE num GLOB '*[A-Za-z]*'

How can I SELECT DISTINCT on the last, non-numerical part of a mixed alphanumeric field?

I have a data set that looks something like this:
A6177PE
A85506
A51SAIO
A7918F
A810004
A11483ON
A5579B
A89903
A104F
A9982
A8574
A8700F
And I need to find all the ENDings where they are non-numeric. In this example, that means PE, AIO, F, ON, B and F.
In pseudocode, I'm imagining I need something like
SELECT DISTINCT X FROM
(SELECT SUBSTR(COL,[SOME_CLEVER_LOGIC]) AS X FROM TABLE);
Any ideas? Can I solve this without learning regexp?
EDIT: To clarify, my data set is a lot larger than this example. Also, I'm only interested in the part of the string AFTER the numeric part. If the string is "A6177PE" I want "PE".
Disclaimer: I don't know Oracle SQL. But, I think something like this should work:
SELECT DISTINCT X FROM
(SELECT SUBSTR(COL,REGEXP_INSTR(COL, "[[:ALPHA:]]+$")) AS X FROM TABLE);
REGEXP_INSTR(COL, "[[:ALPHA:]]+$") should return the position of the first of the characters at the end of the field.
For readability, I'd recommend using the REGEXP_SUBSTR function (If there are no performance issues of course, as this is definitely slower than the accepted solution).
...also similar to REGEXP_INSTR, but instead of returning the position of the substring, it returns the substring itself
SELECT DISTINCT SUBSTR(MY_COLUMN,REGEXP_SUBSTR("[a-zA-Z]+$")) FROM MY_TABLE;
(:alpha: is supported also, as #Audun wrote )
Also useful: Oracle Regexp Support (beginning page)
For example
SELECT SUBSTR(col,INSTR(TRANSLATE(col,'A0123456789','A..........'),'.',-1)+1)
FROM table;

SQL select statement

in a string array i have a variable amount of values.
how will an sql statement look if i want to select all the records that are equal with the array variables.
it will look something this:
SELECT * FROM users WHERE username='"+ variable amount of elements in the String array like: Steven, Mike, John ..... +"'"
You might be looking for the IN( ) operator.
SELECT * FROM users WHERE username IN ('chris', 'bob', 'bill');
This? (obviously the "OR username='xxx' is repeated for as many items as you require)
SELECT * FROM users WHERE username='item1' OR username='item2' OR username='item3'
Every one above me is corrrect! There are multiple ways of doing this! A simple google search for 'mysql statement variables' yield tons of help full results including:
http://www.daniweb.com/forums/thread126895.html
Just treat your array like a standard varible with [a number] on the end!
Two tips come from this:
Google / Search your problem first 9 times out of 10 someone else has had the same problem and found a working solution!
And be prepared to look at the answers on here with in 5 mins. This forums probably the quickest you'll ever see. The only thing that slows the community down is typing ! :P
Hope that Helps,
Andy
Another approach, although less efficient, can be used if parsing the string apart is problematic...
DECLARE #listOfNames VARCHAR(2000)
SET #ListOfNames = 'JOE,KELLIE,PETE'
SELECT * FROM users WHERE charindex(","+userName+",",","+#listOfNames+",") > 0
This approach is slower than either of the other answers, but can save you from using dynamic SQL to build a SQL query based on the list of names passed in...