Re-arrange or Merge multiple rows in SQL - sql

Assuming I have a table containing the following information:
ID
Column A
Column B
Column C
1
A
NULL
NULL
1
NULL
B
NULL
1
NULL
C
NULL
1
NULL
NULL
D
1
NULL
NULL
E
1
NULL
F
NULL
2
NULL
X
NULL
2
NULL
Y
NULL
2
NULL
NULL
Z
is there a way I can perform a select on the table to get the following
ID
Column A
Column B
Column C
1
A
B
D
1
NULL
C
E
1
NULL
F
NULL
2
NULL
X
Z
2
NULL
Y
NULL

Assuming you want to get rows where column B is not null:
SELECT *
FROM yourTable t
WHERE t.ColumnB is not null

Related

Replace value in cell from table A with values from table B, if id matches

I have
-- table_a --
id value
1 a
2 b
3 c
-- table_b --
id value
1 NULL
2 NULL
3 NULL
4 d
5 e
6 f
7 g
I want the result set to look like
-- result_set --
id value
1 a
2 b
3 c
4 d
5 e
6 f
7 g
I know that I can do an
UPDATE table_b SET value =
CASE
WHEN id = 1 THEN 'a'
WHEN id = 2 THEN 'b'
WHEN id = 3 THEN 'c'
END;
But, what if I have many rows to update? Is there an automatic way?
You can join the tables in the UPDATE statement:
UPDATE table_b b
SET value = a.value
FROM table_a a
WHERE a.id = b.id AND b.value IS NULL;

How to set unique ID with ID of group in it?

I have this table:
ID
Value
Gr_Id
Gr_Value
NULL
A
1
A
NULL
B
1
A
NULL
C
2
B
NULL
D
2
B
NULL
E
2
B
NULL
F
3
C
And I need id like this
ID
Value
Gr_Id
Gr_Value
101
A
1
A
102
B
1
A
201
C
2
B
202
D
2
B
203
E
2
B
301
F
3
C
I need unique ID for each row but with ID of group on beginning
if maximum number per group is 99 then :
select gr_id * 100 + row_number() over (partition by Gr_id order by gr_value) as Id , *
from tablename

Copying missing data between tables

I have a table Alpha
A
B
C
2
4
3
1
5
1
4
3
null
I have a reference of table like BETA of one column
like
a
1
2
3
4
5
I want to copy the data missing in Alpha with respect to Beta to another table Gamma such that
The expected result is as follows
A
B
C
3
1
2
5
2
4
null
null
5
It has to refer to the beta table as it is not always in order,
eg. beta table can be 2,3,5 and alpha table has 2 and 3 so the missing value is just 5
PS: this is a minimal representation, in real there are more than 20 columns in Alpha but only one column in beta
The table Alpha and expected result table are same
I have to put my crystal ball into overdrive.
I think you wanted to generate the missing value for each column in table Alpha based on the list of value in table `Beta'
What the below query doing is find out the missing value for each column (A, B, C). After that PIVOT it
; with missing as
(
select col = 'A', V = A
from Beta b
where not exists (select * from Alpha a where a.A = b.A)
union all
select col = 'B', V = A
from Beta b
where not exists (select * from Alpha a where a.B = b.A)
union all
select col = 'C', V = A
from Beta b
where not exists (select * from Alpha a where a.C = b.A)
)
select [A], [B], [C]
from (
select *, rn = row_number() over (partition by col order by V)
from missing
) m
pivot
(
max(V)
for col in ([A], [B], [C])
) p
PS : if you really have 50 columns in table Alpha, you need to do the union all query 50 times, each for one of the column.
I think you can use several insert statement for this problem
INSERT GAMMA(A)
SELECT BETA.a FROM BETA
LEFT JOIN ALPHA ON BETA.a = ALPHA.A
WHERE ALPHA.A IS NULL
Change the A to B or another column name.
But this table will produce table like this
A
B
C
3
null
null
5
null
null
null
1
null
null
2
null
null
null
2
null
null
4
This might not be the answer you want, but i hope it gives you alternatives.

how to combine two rows in sql having same values in different columns

need to combine rows
current data
id v1 v2 v3
1 null null a
1 null null b
1 null c null
1 null null c
Expected output
id v1 v2 v3
1 null null a
1 null null b
1 null c c
I think this does what you want:
select id, v1, max(v2), max(v3)
from t
group by id, v1, coalesce(v2, v3);

Update a table with data from another not related table

I have three tables A,B and C.
A and B are connected a foreign key A.category_id=B.id ,
A and C have the same number of rows.
A
id category_id value1 value2
1 null 'A' null
2 null 'B' null
3 null 'C' null
4 null 'D' null
B
id category
1 0
2 1
C
id category
1 0
2 1
3 1
4 0
Expected result:
A
id value
1 1
2 2
3 2
4 1
I would like to receive updated A table where category_id will be id from table B based on table C category.
I have tried
UPDATE A SET
A.category_id = (
select B.id from A
left JOIN C
ON A.id = C.id
left join B on B.category=C.category
)
WHERE A.id IN (SELECT C FROM C WHERE A.id = C.id);
but then I received ORA-01427 single-row subquery returns more than one row tips
You are missing a correlation clause in the subquery:
update A
set category_id = (select B.id
from C join
B
on B.category = C.category
where A.id = C.id
)
where exists (select 1 from C where A.id = C.id);
To correlated a query, you refer to the outer reference typically in a where condition. You don't repeat the table reference in the from clause.
Note that an outer join is unnecessary. If there is no match, the value will be NULL.