Choose first non-null cell from two columns in PostgreSQL - sql

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;

Related

Return based on values selected

I really don't know how to phrase the title correctly, so please excuse me if the title is confusing.
Here's the scenario I am facing:
I have a table...assuming it contains the follow rows.
Name | Value
----- | ----
John | 1
Mary | 2
Jack | 3
Jim | 4
Here's the PL/SQL requirement:
If John exists, return John and his value.
If John does not exist, but Mary does, return Mary and her value.
If neither John nor Mary exist, return either Jack or Jim whichever
has the higher value.
I was able to use cursor to traverse the table and test each row, but am wondering if there are other more efficient way.
Thanks!
No need for a cursor and a loop. You can do this in a single query, using a conditional sort and a fetch clause:
select *
from mytable
order by
case name when 'John' then 1 when 'Mary' then 2 else 3 end,
value desc
fetch first row only
Or if you are a pre-12c version of Oracle, where the fetch clause is not available:
select name, value
from (
select t.*,
row_number() over(order by
case name when 'John' then 1 when 'Mary' then 2 else 3 end,
value desc
) rn
from mytable t
) t
where rn = 1

Filter DB2 Group based on criteria

I have a data set for multiple versions of a record. In the structure below:
ID Version Date ActionedBy
1 1 20/08/2018 James
1 2 20/08/2018 Samuel
2 1 20/08/2018 Tom
3 1 20/08/2018 Tom
3 2 20/08/2018 Tom
3 1 20/08/2018 Dave
4 1 20/08/2018 Tom
4 2 20/08/2018 Mike
5 1 20/08/2018 Dave
I need a query to return all records that Tom has actioned but not include the ones actioned by Dave. So my result should be:
ID
2
4
I've grouped on ID but am struggling with the Having clause or sub query to not include records with Dave.
My query is:
Select * from events where ActionedBy='Tom' Group By ID Having "NOT SURE WHAT GOES HERE"
One solution could be
SELECT ID FROM events e
WHERE ActionedBy = 'Tom'
AND NOT EXISTS (SELECT 1 FROM events WHERE ActionedBy = 'Dave' AND ID = e.ID)
You do not necessarily need the GROUP BY if Tom occurs only once in a single ID. If not just add it....
I would use group by with a having clause:
select id
from t
where actionedby in ('Tom', 'Dave')
having min(actionedby) = 'Tom' and max(actionedby) = 'Tom';
Actually min() is sufficient in this case, because 'Tom' > 'Dave'.

BigQuery get column as comma seperated values

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

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