Pyspark: How do I covert dataframe column values into a comma separated string? - apache-spark-sql

I am running this on Databricks. My goal is to make a select statement with all the values in the column comma separated.
Content of my df:
For example, I want to see SELECT business_key,brand_key,brand_id,... etc.
Thanks in advance.

Related

How can I replace multiples value using regex_repalce in sql?

I have column in which different values are there like
refer_nO
Post-factos022110
P0st-fact04433
Postfact0s304004
Postfact202934
Now I want to keep the numbers and replace all non-numbers values with blank, Wanting something like this
refer_nO
022110
04433
304004
202934
To replace one single value I can use
select regex_replace(column1,"Post-factos"," ") from table
How can I replace mutiples values ?
Use regex [^0-9]+ to remove all non-numeric characters:
select regexp_replace(column1,'[^0-9]+','') from table

Combine several columns in one with Teradata

I have 10 columns and their values could be either null, or a name of a fruit.
I would like to add another column with all the fruits that every row has. I have used Concat(column1 , column2,..., column10) as name.
Issue : There are no commas coming on the result and if I add the comma before concatenating, we are having them together, the last word is also a comma.
Any ideas?
Thanks!
You can use the standard concatenation (||) in conjunciton with COALESCE function, which returns the value of the first non-null argument.
Example:
select coalesce(column1||',', '')||coalesce(column2||',', '')|| ... ||coalesce(column10||, '');

PostgreSQL query to split based on Strings & Concatenate them into new individual columns

I have a table Rulebook with a column "Rules" like this:
I want a output like this:
I want a postgreSQL query that will split each rule based on the '|' & put them into new columns as Rule 1 to 'n'. Trying out couple of case statements, need your help to achieve the output. Interested to know the MSSQL Server query too. Thanks!
Use string_to_array to split the string into multiple elements that can be accessed individually:
select rules[1] as rule_1,
rules[2] as rule_2,
rules[3] as rule_3,
rules[4] as rule_4,
rules[5] as rule_5,
rules[6] as rule_6
from (
select string_to_array(rules, '|') as rules
from rulebook
) t

Need to separate the string from a column to multiple columns which is separated by ';' delimeter in bigquery

I have the below scenario
My column values looked like below :
loc=adhesion;pos=31;refresh=559;tag=post;tag=article;tag=business;
I want to separate all the values on the basis of ';' delimiter.
Please suggest the code to generate the below result in bigquery.
Example : My output should look like below :
Number of columns should get created with the below values :
col1 : loc=adhesion
Col2 : pos=31
col3 : refresh=559
col4 : tag=post
and so on
Thank you for your help.
You can do this using the SPLIT and NTH functions. See the link below for an example of how to use it.
BigQuery: SPLIT() returns only one value

Format the double precision value in Postgresql

I want to get the value from postgresql with formating. The formatting is user option like they want comma separator or not and with decimal place is 2,3 or 4 like.
So now i wrote query like this.
Select to_char(rate,'FM999.00') as BasicSaleRate from table1
Its return ans 220.00. How to write the query with comma separator value like this '#,0.00'. That means the value more than thousands return with comma separator or not.
Am using postgresql 9.3
Thank you
See this reference http://www.postgresql.org/docs/9.3/static/functions-formatting.html
to_char(rate,'FM999,999.00')