How to Make Critera for Data migration - sql

Goal:
Load the destination table c with data from table a and b.
Problem:
I have two tables, a and b with that need to be migrated into same table c.
Need to load one table at a time. When loaded to table c, the criteria för table a need to define that criteria most be access 1 and for the table b the access most be 2.
The question is:
How do you make criteria for the for the table c?
Requested result for table c:
table c
----------
number access gender
--------------------
1 1 2
1 2 2
2 2 2
3 1 1
4 2 1
5 1 2
5 2 2
table a
----------
number access gender
--------------------
1 1 2
2 1 2
3 1 1
4 1 1
5 1 2
table b
----------
number access gender
--------------------
1 2 2
2 2 2
3 2 1
4 2 1
5 2 2
table c
----------
number access gender
--------------------
1 1 null
1 2 null
2 2 null
3 1 null
4 2 null
5 1 null
5 2 null

I think I understand what you are trying to do, try using a couple of MERGE statements like so:
MERGE c AS target
USING a AS source
ON (target.number = source.number, target.access = source.access)
WHEN MATCHED THEN
UPDATE SET target.gender = source.gender
WHEN NOT MATCHED BY target THEN
INSERT (number, access, gender)
VALUES (source.number, source.access, source.gender)
MERGE c AS target
USING b AS source
ON (target.number = source.number, target.access = source.access)
WHEN MATCHED THEN
UPDATE SET target.gender = source.gender
WHEN NOT MATCHED BY target THEN
INSERT (number, access, gender)
VALUES (source.number, source.access, source.gender)

Related

Hive - Group by with respect to following values

I have a table with rows:
id
a
b
0
1
1
1
1
2
2
2
1
3
1
1
I need to get sum of field "b" values grouped by "a" with respect to changes in "a".
For my example i want to get:
a
b
1
3
2
1
1
1

Create a table with unknown columns SQL

I have a table that looks like this
ID
Steps
Letters
1
1
a
1
2
e
1
3
b
2
1
c
2
2
d
3
1
b
3
2
a
And a query that consists of the output
a
b
d
My goal is to create a table/ modify the first one to get rid of the letter column, and instead, have N additional columns (where N is the number of rows in the second query above) and the output is 1 if the last step for that ID was that specific letter, 0 if that letter was in any step, and NULL if it never was. Making a table like this
ID
a
b
d
1
0
1
NULL
2
NULL
NULL
1
3
1
0
NULL
I assume pivoting makes sense as a way to approach it, but I don't even know where to begin

PLSQL UPDATE BASED ON ROWS FROM OTHER TABLE

I have following tables in APEX A and B.
A has columns:
ID_A;
VALUE;
B has columns:
ID_C_FK;
ID_A_FK
I want to update the VALUE column in table A in rows where ID_A equals ID_A_FK in selected rows from table B where ID_C_FK equal x
For example: A has rows (
ID_A value
------------
1 1
2 1
3 0
4 0
5 0
Table B has rows
ID_C_FK ID_A_FK
------------------
8 4
9 4
9 5
I want to update VALUE in table A only for those rows that have ID_A in rows selected from B and condition to select rows from B is that ID_C_FK equals x = 9; and as a result, table A should end up having rows:
ID_A value
------------
1 1
2 1
3 0
4 1
5 1
How to write such update in PL/SQL?
Thank you for considering my request.
I think this is what you want:
update a
set value = 1
where exists (select 1
from b
where b.id_a_fk = a.id_a and b.id_c_fk = 9
);

Selecting rows and filler (null data)

I have a table that looks like this:
ReportID | TeamID | Inning | Runs
1 A 1 3
1 A 2 3
1 A 5 7
1 B 1 3
1 B 3 2
1 B 6 1
I need to select all of that data, plus null data for the missing innings. It also need to stop at the max Inning for both teams (i.e. teamB's highest inning is 6, so I would collect 6 rows for both teamA and teamB yielding 12 total rows.)
For a visual, I need the output of the query to look like this:
ReportID | TeamID | Inning | Runs
1 A 1 3
1 A 2 3
1 A 3 NULL
1 A 4 NULL
1 A 5 7
1 A 6 NULL
1 B 1 3
1 B 2 NULL
1 B 3 2
1 B 4 NULL
1 B 5 NULL
1 B 6 1
Is there anyway to do this with just a query? Modifying the original table to add the null values is not an option.
Self join to generate the permutations of reports and teams
Left self join to generate hits which might be nullable.
This is probably a lot more efficient if it's done outside of SQL
SELECT ins.ReportID, teams.TeamID, ins.inning, score.Runs
FROM games as ins
JOIN games AS teams
ON ins.ReportID = teams.ReportID
LEFT JOIN games AS score
ON ins.ReportID = score.ReportID
AND teams.TeamID = score.TeamID
AND ins.inning = score.inning
GROUP BY ins.ReportID, teams.TeamID, ins.inning;

Matching two variables to create a new ID

I'm trying to create an SQL statement to match either an id number or a postcode and then assign a new id number
What I want to end up with is ‘newid’ that correctly recognizes that the first four records are the same person (even though the postcode for record 2 is different).
record id postcode newid
--------------------------
1 1 1 1
2 1 2 1
3 1 1 1
4 2 1 1
5 3 3 2
Any suggestions would be appreciated greatly.
Going based on your example:
SELECT RECORD,
(SELECT MIN (ID)
FROM users u2
WHERE users.id IN (u2.id, u2.postcode)
OR users.postcode in (u2.id, u2.postcode)
) AS newid
FROM users
This results with the following data:
RECORD NEWID
------------------
1 1
2 1
3 1
4 1
5 3
Here is the SQLFiddle