How to represent the result in Hive - sql

I have two fields like above image.
The above three rows to be represented as single row as mentioned in same image.
Can someone let me know how to produce the above result in Hive without using UDF?

You can use concat_ws:
select
concat_ws(',', collect_list(concat_ws(':', col1, col2))) as output
from mytable

Related

Match specific string format - REGEXP_CONTAINS - GBQ Language

I am trying to write a query that will only match table names that match a specific format, that format being as follows: FirstWord1_SecondWord2_ThirdWord3.
So all I am trying to get are table names that match the format of three alphanumeric words separated by underscores.
I've been struggling to workout the exact way to use REGEXP_CONTAINS to get the results I want. Below is the closest i've been able to get to, but it just wont return any results, despite the fact that I know there are tables that match the format I want to query for.
SELECT table_name as tablenames
FROM project.dataset.INFORMATION_SCHEMA.TABLES
WHERE (
REGEXP_CONTAINS(table_name, '^([[:alnum:]]+_[[:alnum:]]+_[[:alnum:]])$')
)
Any assistance with this would be greatly appreciated!
Your last [[:alnum:]] is missing a + to indicate 1 or more matched characters.
SELECT table_name as tablenames
FROM project.dataset.INFORMATION_SCHEMA.TABLES
WHERE (
REGEXP_CONTAINS(table_name, '^([[:alnum:]]+_[[:alnum:]]+_[[:alnum:]]+)$')
)
or
SELECT table_name as tablenames
FROM project.dataset.INFORMATION_SCHEMA.TABLES
WHERE (
REGEXP_CONTAINS(table_name, '^[[:alnum:]]+_[[:alnum:]]+_[[:alnum:]]+$')
)
Let me know if this works for you.

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.

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

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