SQL Query that leaves duplicate fields blank? - sql

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

Related

how to pull all records in hive based on another column

If this is what my table looks like below:
my_id my_words my_people my_number
100 need more info? Jim 1
100 now Mary 2
100 what's that? Jim 3
101 okay now Jim 1
101 sounds good Mary 2
102 still hungry? Jim 1
102 now I'm thirsty though Mary 2
102 I don't understand Jim 3
102 no I'm not hungry Mary 4
103 are you there? Jim 1
103 I don't know Mary 2
103 That's okay Jim 3
How can I get this output?
my_id my_words my_people my_number
100 need more info? Jim 1
100 now Mary 2
100 what's that? Jim 3
102 still hungry? Jim 1
102 Now I'm thirsty though Mary 2
102 I don't understand Jim 3
right now I have: SELECT my_id, my_words, my_people, my_number from table where my_people="Mary" AND lower(my_words) like 'now%';
But I don't only want to return those rows, I also want to return Jim's comment right before and right after Mary's (before/after based on my_number column)
Maybe this is unrelated, but ultimately, I'm going to want this in Excel with this format:
my_id Jim_words Mary_words Jim_next_words
100 need more info? now what's that?
102 still hungry? now I'm thirsty though I don't understand
Could you please try below code? Code has explanations as comment.
WITH cte as (SELECT my_id, my_words, my_people, my_number
row_number() over( partition by my_id order by my_number) as rn --giving a unique row number for a my id
from table)
SELECT
distinct mytab.my_id, chosentab.my_words jims_words, mytab.my_people, mytab.my_number,
case when case when lower(mytab.my_words) like 'now%' then mytab.rn+1 end = chosentab.rn then chosentab.my_words end jims_words_after_marys_now,
case when case when lower(mytab.my_words) like 'now%' then mytab.rn-1 end = chosentab.rn then chosentab.my_words end jims_words_before_marys_now
FROM
cte mytab,
cte chosentab
where
mytab.my_id=chosentab.my_id and
case when lower(mytab.my_words) like 'now%' then mytab.rn+1 end = chosentab.rn and -- selecting jims rows where mary said now after jim
case when lower(mytab.my_words) like 'now%' then mytab.rn-1 end = chosentab.rn -- selecting jims rows where mary said now before jim
Now I created the SQL based on our discussion. Could you please validate and let me know it if worked?

proc sql function to find mulitple LIKE matches?

I'm having trouble with a LIKE function in proc sql.
PROC SQL;
CREATE TABLE NAMES_IDS AS
SELECT DISTINCT
T1.*
,T2.NAMES
,T2.NAME_ID
FROM WORK.table1 T1
LEFT JOIN data.table2 T2 ON T2.NAMES like T1.NAMES1
;QUIT;
I have several names in t2, lets say for example theres John 1, John 2, John 3, John 4, etc and in t1.Names1 there is %John%
proc sql is just pulling in the first match, John 1 and its associated ID, and applying it to all the data in T1, instead of duplicated a match for all matching names (this is what I want to achieve).
So the end table would have something like
COLUMN A COLUMN B
John John 1
John John 2
John John 3
John John 4
But instead, what I get is:
COLUMN A COLUMN B
John John 1
John John 1
John John 1
John John 1
Hopefully this makes some sort of sense...
I think I figured it out, I added TRIM to my code and I guess there may have been some erroneous spaces somewhere because that seems to fix my issue. Thanks for your responses!

Track which columns were modified by an Update

How do I track the list of updated columns in a table
Table1
Name Place Email Id
John US john#gmail.com 1
Sam US sam#gmail.com 2
Now if I update Name,place columns for John and place,email for Sam like
Name Place Email Id
Johnny UK john#gmail.com 1
Sam UK sammy#gmail.com 2
I want to store the list of updated attributes in another table as following
Id ListOfModifiedAttributes Status othercolumns
1 name,place success
2 place,email success
How to achieve the above in plsql.

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