SQL - Several rows into one - sql

There are 12 rows in the table in total. Every unique record contains of 4 rows. I want the result on 3 rows/records in total having 4 Fields/columns each.
Example Input:
TEST
455
688
987
Texter
567
53
878
Julgranar
765
454
989
Exampel output/result:
Column1 Column2 Column3 Column4
TEST 455 688 987
Texter 567 53 878
Julgranar 765 454 989

There are a few ways depending on some data rules that you have not included, but here is one way using what you gave.
SELECT
t1.Field1,
t2.Field2
FROM Table1 t1
LEFT JOIN Table1 t2 ON t1.FK = t2.FK AND t2.Field1 IS NULL
Another way:
SELECT
t1.Field1,
(SELECT Field2 FROM Table2 t2 WHERE t2.FK = t1.FK AND Field1 IS NULL) AS Field2
FROM Table1 t1

Related

SQL inner join and group by to get comma separated values

I have following three tables, from which I would like to get a specific result
TableA
key1 key2
-------------
121 4
131 4
141 5
151 3
161 3
171 6
181 6
191 6
... ...
TableB:
key1 key3
-------------
121 1001
131 1111
141 1111
151 1222
161 1222
171 1234
181 1001
191 1111
... ...
TableC:
key3 key4
-------------
1001 "aa"
1111 "gg"
1222 "hh"
1234 "jj"
... ...
I want a SQL query (which could use inner join) to give me the following result:
key2 key4
-------------------------
3 "hh"
4 "aa", "gg"
5 "gg"
6 "aa", "gg", "jj"
Microsoft SQL Server 2012
You can use string_agg():
select t1.key2, string_agg(t3.key4, ',') key4
from table1 t1
inner join table2 t2 on t2.key1 = t1.key1
inner join table3 t3 on t3.key3 = t1.key3
group by t1.key2
Please try with below SQL query:
SELECT key2, string_agg(key4, ",")
FROM TableA JOIN TableB USING (key1) JOIN TableC USING (key3)
GROUP BY key2

How can I find duplicate records \ values after left join?

I have:
TABLE1 with ID, and Name.
TABLE2 with ID, and Address.
I want to get ALL TABLE1 records and add a STATUS column:
If this record exists in TABLE2 - 'OK'.
If this record is not exist in TABLE2, then research a match by last 2 digits, else 'NO_RECORD'.
If this record has duplicate records in TABLE2, then if the duplicate records has the same Address choose one record - 'OK', and if they doesn't has the same Address - 'DUPLICATE'.
Meanwhile, I've started with this:
SELECT t1.id,
t1.name,
t2.Address,
iif(Address is null, 'No_RECORD', 'Ok') as 'status'
FROM Table1 as t1
left join Table2 as t2 on t1.id = t2.id
For example:
Table1
id Name
111 aaa
222 bbb
333 ccc
444 ddd
555 eee
666 fff
777 ggg
888 hhh
999 iii
Table2:
id Address
111 rr
922 hfh
444 vbv
444 vbv
555 xxa
555 plo
555 plo
666 wqq
777 gyt
999 ree
999 ree
My accepted results are:
id name Address 'status'
111 aaa rr Ok
222 bbb hfh Ok
333 ccc No_RECORD
444 ddd vbv Ok
555 eee Duplicate
666 fff fff Ok
777 ggg wqq Ok
888 hhh No_RECORD
999 iii ree Ok
444 is not duplicate because the 2 table2's records address are match,
555 is duplicate because the 3 table2's records address are mismatch,
999 is not duplicate because the 2 table2's records address are match.
222 is ok because tha 2 last digits of it's id are exist in tabl2: "922".
How can I continue? (I use sql query in access).
I have no idea what "match by last five digits" is supposed to mean. It has nothing to do with your sample data, so I'm just ignoring that part of the question.
What you want to do is to aggregate on table2 before doing the join:
select t1.id, t1.name,
iif(multiple_addresses = 0, address, null) as address,
switch(t2.id is null, "No_Record",
multiple_addresses = 1, "Duplicate",
1=1, "OK"
) as status
from table1 as t1 left join
(select id, min(address) as address
iif(min(address) = max(address), min(address), 0, 1) as multiple_addresses
from table2
group by id
) as t2
on t1.id = t2.id;
You can use a case statement to get this status:
SELECT
t1.id,
t1.name,
t2.Address as add2,
case
when t2.Address is null and exists (select 1 from Table2 temp where temp.id = t1.id) then 'DUPLICATE'
when t2.Address is null then 'NO_RECORD'
else 'OK' end
as 'status'
FROM Table1 as t1
left join Table2 as t2 on t1.id = t2.id and t1.name = t2.Address
order by t1.id
As you can see I added and t1.name = t2.Address to the join clause to make sure you have non null Table2 values only when you want so.
About the case when, the first condition checks if 1) no corresponding record was found 2) there are records for the same id, meaning 'DUPLICATE'. The second condition checks that there are no corresponding record, and we already know there are no duplicates (as it would have fallen into the first case).
Working SQLFiddle.

Replacing null values with join

I have two tables
Table1: Table2:
ID: Type: Amount: ID: Amount:
165 Red 300 188 425
167 Red 100 189 500
168 Blue 250 222 129
188 Grey NULL 333 247
189 Grey NULL 369 328
I am trying to replace the null values from table 1 by joining on table 2.
My code results in two amount columns.
LEFT JOIN table2
ON table2.pk = table1.pk
AND table1.Type IS NULL
I think you want:
select t1.id, t1.type, coalesce(t1.amount, t2.amount)
from table1 t1 left join
table2 t2
on t1.id = t2.id;
Hope this helps for your needs
select t1.id,t1.type,isnull(t1.amount,t2.amount) 'Amount' from table1 t1 left join table2 t2 on t1.id = t2.id
you can use isnull() function so that if the first table amount is null then it will take second table value.

Finding IDs in one table not in another

I have two tables:
table1
ID | Name | Code
1 Joe 123
2 Sam 674
3 Mike 321
table2
ID | User Name| Code
1 Joe 123
2 Sam 674
3 Mike 321
4 John 457
5 Tim 235
Desired result:
4|John|457
5| Tim|235
Tabe1 and table2 code is identical. Table 1 code is a new field added thus contains no data for any record. Using the IDs as keys I took the codes from table2 and populated them in table1. However table1 has considerably less IDs then table2 so table2 has more codes then table1. I want to query which codes did not get transferred to table1. I thought it would be as simple as:
select *
from table2 t2
where t2.Code is not null and
t2.Code not in (select t1.Code from table1 t1 where t1.Code is not null);
This returns nothing which is strange to me. What do I need to adjust in this query? This is for oracle.
You might try the following:
SELECT t2.*
FROM table2 t2
WHERE NOT EXISTS ( SELECT 1 FROM table1 t1
WHERE t1.code = t2.code );

how to change the column name dynamically?

In a database there are two tables.
Table1 and Table2
Table1:
column1 column2 column3
271 211 111
301 333 333
Table2:
ColumnNo Value Desc
1 271 aaa
3 111 bbb
2 211 ccc
2 333 ddd
1 301 eee
Here columnNo refers to the 1st table>> 1=Column1,2=column2,3=Column3.
so i have to update the 1st table column value with the Desc.
so the updated table1 will be
column1 column2 column3
aaa ccc bbb
eee ddd ddd
like this.
As there are lots of column and value so how can i change the column name dynamically and update the value?
Try this
UPDATE T1
SET T1.column1 = T2.Desc,T1.column2 = T3.Desc,T1.column3 = T4.Desc
FROM Table1 T1 Inner Join Table2 T2 ON T1.column1 = T2.Value
Inner Join Table2 T3 ON T1.column2 = T3.Value
Inner Join Table2 T4 ON T1.column3 = T4.Value
FIDDLE DEMO