SQL - transpose all categorical rows to columns and fill na - sql

I have a database with data for different exams, for example:
Student Test Grade
--------------------
St1 T1 A
St1 T2 B
St2 T2 B
St2 T3 C
St3 T3 B
Then I would like to transpose column TEST, which has three category: T1 T2 and T3 to three columns and fill NA on not exist row
Student T1 T2 T3
----------------------
St1 A B na
St2 na B C
St3 na na B
I don't know how to fill na on non-exist category
Any help is appreciated!

Related

SQL JOINS just similar to excel vlook up but with conditions

I am trying to do 2 tables in SQL to form a third table. Example and the expected output is below
Table1
Date CHI HOS
1/1/1 1 a
1/1/1 2 b
2/1/1 1 a
3/1/1 3 c
Table2
Date CHI CRN
1/1/1 2 b
1/1/1 1 a
2/1/1 2 b
3/1/1 3 d
Table1 _new
Date CHI HOS Hosp_new
1/1/1 1 a a
1/1/1 2 b b
2/1/1 1 a a
3/1/1 3 c d
The conditions are,
the unique key is CHI,
where table1.Hos = table2.CRN then table1.HOS
Where table1.HOS <> table2.CRN then table2.CRN
Where vlook up column in table1 CHI is not found in table2 then table1.HOS
Create a new column HOSP_new and populate with the above
Its a 12 million rows file and I don’t want to have duplicate rows
You can try this:
select distinct t1.date, t1.chi, t1.hos,
case when t1.hos != t2.hos then t2.hos
else t1.hos end as Hosp_new
from table1 t1
left join table2 t2 on t1.chi = t2.chi and t2.date = t1.date

How to Join 3 tables based on 2 columns from one table in presto?

I Have 3 tables as below. In table1, if value in column id is present then join that column with other tables, if null I want to join with ref column from table 1 with other tables. What is the best way to do this
table1
Name id ref
abc 123
abc 456
edc 345 432
asd 678
table 2
city id ref
NY 123
WA 875
CA 345 432
SA 678
table 3
city orders
NY 78954
WA 123546
CA 789
SA 1
I want below result: ABC has 123 in id hence NY. ASD has 678, so considered ref column in table1 to join with other 2 tables
Name city order
ABC NY 78954
EDC CA 789
ASD SA 1
You can use left joins for this purpose:
select t1.*, t3.city, t3.count
from table1 t1 left join
table2 t2
on t1.id = t2.id left join
table2 t2r
on t1.ref = t2.ref and t1.id is null left join
table3 t3
on t3.city = coalesce(t1.city, t2.city);
Note: This returns all rows from table1, even those with no match in either column. If this is an issue, add:
where t2.id is not null or t2.ref is not null

SQL Server : select from multiple tables and calculate percentages

I have four tables and I would need to extract data from them to calculate the percentage
Table1
ID FK1 FK2
------------------
1 1 1
2 2 2
3 3 3
Table2
ID Name
------------------
1 L1
2 A1
3 B
Table3
ID FK3
------------------
1 1
2 2
3 3
Table4
ID Name
------------------
1 BA
2 N
3 CE
Now I need to get a Name from table4, which will be displayed as individual rows and then a Name from table2, which will be listed as individual columns and the value will then be a percentage of the record from table4:
Name L1 A1 B
---------------------------
BA 20(%) 40(%) 40(%)
N 30(%) 20(%) 30(%)
CE 15(%) 15(%) 70(%)
Because there are links, I'll give an example of what question I have now
select t3.Name
from table1 t1 (nolock)
join table2 t2 (nolock) on t1.FK1 = t2.ID
join table3 t3 (nolock) on t1.FK2 = t3.ID
join table4 t4 (nolock) on t2.FK3 = t4.ID
Does anyone have any idea how to do this? Thank you very much

SQL - join tables on multiple columns

I want to join few tables:
table1:
A B_key B_version C D
123 abc 1 ccc 11
123 abc 2 ddd 11
456 dfg 1 rrr 22
789 vvv 1 55
table2:
A E F
123 s 5
456 r
111 t 2
table3:
B_key B_version G
abc 1 aa
abc 1 bb
abc 2 aa
abc 2 cc
dfg 1 aa
so the result would look like this:
A B_key B_version C D E F G
123 abc 1 ccc 11 s 5 aa
123 abc 1 ccc 11 s 5 bb
123 abc 2 ddd 11 s 5 aa
123 abc 2 ddd 11 s 5 cc
456 dfg 1 rrr 22 r aa
789 vvv 1 55
Version can go as high as 8.
IF I don't have A, B_key or B_version - the line is useless. Otherwise I need to keep all the information I do have.
In reality I have many more columns.
I've tried:
SELECT table1.A, table 1.B_key, table 1.B_version, table 1.C, table 1.D,
table2.E, table2.F,
table3.G
FROM table1
LEFT JOIN table2
ON table1.A = table2.A
LEFT JOIN table3
ON table1.B_key = table3.B_key AND
table1.B_version = table3.B_version
and the same with FULL JOIN.
It ends up the same: for every B_key only the highest B_version is kept, while the others disappear.
How can I avoid loosing information?
You can use left joins among tables as below :
select t1.A, t1.B_key, t1.B_version, t1.C, t1.D, t2.E, t2.F, t3.G
from table1 t1
left join table2 t2 on t2.A = t1.A
left join table3 t3 on t3.B_key = t1.B_key and t3.B_version = t1.B_version
Demo
in order to bring also the rows for unmatched values for join conditions.
If I understand correctly, you want all the b_keys and b_versions from table1 and table3. Then you want to bring in the other data. That suggests left joins
select . . .
from ((select B_key, B_version
from table1
) union -- on purpose to remove duplicates
( select B_key, B_version
from table3
)
) bb left join
table1 t1
on t1.b_key = bb.b_key and
t1.b_version = bb.b_version left join
table2 t2
on t2.a = t1.a left join
table3 t3
on t1.b_key = bb.b_key and
t1.b_version = bb.b_version;

Update table row with data from another table

I'm quite new to sql and i need your help to solve this problem. I have 2 tables. What I want is to update a row from one table with data from another one but only if the values are different where the id is the same.
Like this:
Table A
ID DESC
1 asd
2 aaa
3 asda
Table B
ID DESC
1 asd33
2 aaa22
3 asda
And what I want is this update DESC table B with data from DESC A only if the values are different
Table B
ID DESC
1 asd
2 aaa
3 asda
UPDATE B
SET B.DESC = A.DESC
FROM TABLEB B
LEFT JOIN TABLEA A on A.ID = B.ID
Try this
Update TableB
Set TableB.desc = TableA.desc
From TableB INNER JOIN TableA ON TableB.ID = TableA.ID
Where TableB.desc NOT IN
(Select ISNULL(TableA.desc,'') From TableA)