BigQuery get column as comma seperated values - google-bigquery

Is there a Bigquery equivalent of SQLServer STUFF function to get a field as comma seperated values instead of multiple rows?
For example
**Table 1**
id name
1 John
2 John
3 Tom
1 Harry
4 Harry
5 Harry
**Table 2**
id group
1 group1
2 group2
3 group3
4 group4
5 group5
I want the result to be
name group
John group1,group2
Tom group3
Harry group1,group4,group5
Thanks in advance for the help

With standard SQL:
#standardSQL
SELECT name, STRING_AGG(DISTINCT `group` ORDER BY `group`)
FROM (table or sub-select doing join)
GROUP BY name

Related

Active Record Sort by 2 conditions

How can I sort by number first and further sort same number names by alphabet?
Example:
Score | Name
-----------
12 John
11 Paul
10 Dave
9 Adam
9 Ben
9 David
Just use the SQL syntax for ordering by multiple columns:
order by Score, Name
Select * from Table Order by Score , Name

Finding distinct count of combination of columns values in sql

Currently I have a table this :
Roll no. Names
------------------
1 Sam
1 Sam
2 Sasha
2 Sasha
3 Joe
4 Jack
5 Jack
5 Julie
I want to write a query in which I get count of the combination in another column
Required output
Combination distinct count
-----------------------------
2-Sasha 1
5-Jack 1
5-Julie 1
Basically, you could group by these columns and use a count function:
SELECT rollno, name, COUNT(*)
FROM mytable
GROUP BY rollno, name
You could also concat the two columns:
SELECT CONCAT(rollno, '-', name), COUNT(*)
FROM mytable
GROUP BY CONCAT(rollno, '-', name)

Choose first non-null cell from two columns in PostgreSQL

From a table like this one:
id name alias
0 John Null
1 Null Paul
2 Null George
3 Ringo Null
4 Pete Pete
How can I select the first non-Null value between name and alias columns, and put it into its own results field, so that the output would be:
id result
0 John
1 Paul
2 George
3 Ringo
4 Pete
You are basically describing the COALESCE function:
https://www.postgresql.org/docs/9.6/static/functions-conditional.html
In your case:
SELECT id, COALESCE(name, alias) AS result FROM yourtable;

SQL. How to take value from one column and pass It to another column

I'm using SQL-Server 2008. How to take value from one column and pass It to another column?
As you see in sample data below there are 4 columns. Where I need to take column name (in this case UserName) and pass It to FieldName column and value from UserName column pass to Value column.
SAMPLE DATA
GroupId UserName FieldName Value
1 John Smith Foo 28
1 John Smith Bar 2
1 John Smith FooBar 11
1 John Smith Bizz 22
2 Peter Jones Foo 4
2 Peter Jones Bar 13
2 Peter Jones FooBar 27
2 Peter Jones Bizz 23
DESIRED RESULTS
GroupId FieldName Value
1 Foo 28
1 Bar 2
1 FooBar 11
1 Bizz 22
1 UserName John Smith
2 Foo 4
2 Bar 13
2 FooBar 27
2 Bizz 23
2 UserName Peter Jones
How could I achieve It? By using PIVOT? But I'm not sure how to merge pivoted data to existing column. Have you any ideas? If something unclear - ask me, I'll try provide more details.
select GroupId, FieldName, Value
from table
union
select distinct GroupId, 'username', UserName
from table
No need to PIVOT , just a simple UNION ALL will do the job :
SELECT DISTINCT t.groupID,'USERNAME',t.userName
FROM YourTable t
UNION ALL
SELECT s.groupID,s.FieldName,s.Value
FROM YourTable s
SELECT DISTINCT t.*
FROM tbl
CROSS APPLY (
VALUES
(GroupId, FieldName, Value),
(GroupId, 'UserName', UserName)
) t(GroupId, FieldName, Value)
Also check a small information about UNPIVOT and VALUES from my post

SQL Query that leaves duplicate fields blank?

What I would like is if my data is this:
ID Name
0 Jim
1 Dave
1 Bob
1 John
2 Ann
My query returns this:
ID Name
0 Jim
1 Dave
Bob
John
2 Ann
Is there a simple way to do this?
Edit:
The query which returns the original 2 columns of data would be something like this:
SELECT ID, NAME
FROM TestTable
ORDER BY ID