oracle - sql query - sql

I have 2 tables
table A has
id, ans_id, ans_value
1 12 social test
2 15 math test
3 14 English test
Table B has
id, quest_id, question_value
3 10 ELA
4 11 history
5 12 math counts
I know that
question id 10 from table B is equal to ans_id 14 in table A
question id 11 from table B is equal to ans_id 12 in table A
question id 12 from table B is equal to ans_id 15 in table A
so my output result should display
quest_id quest_value ans_id, ans_value
10 ELA 14 English Test
11 History 12 Social Test
12 Math Counts 15 Math Test
Please help me with the sql.
thanks

Your current schema is lacking information relating the two tables. In the absence of this, you could create a bridge table, say tablec, which links the questions in tableb to the answers in tablea, e.g.
quest_id | ans_id
10 | 14
11 | 12
12 | 15
Then you could join as follows:
SELECT t1.quest_id,
t1.question_value AS quest_value,
t3.ans_id,
t3.ans_value
FROM tableb t1
INNER JOIN tablec t2
ON t1.quest_id = t2.quest_id
INNER JOIN tablea t3
ON t2.ans_id = t3.ans_id

Related

Group Rows Together in Join [duplicate]

This question already has answers here:
How do I create a comma-separated list using a SQL query?
(11 answers)
Closed 18 days ago.
I have a table that contains a bunch of keys with different IDs. Some keys have more than 1 ID.
ID Table:
key id
a 1
a 11
a 12
b 2
c 3
c 33
2nd table:
key count location
a 17 123 Test Rd
b 10 12 Smith St
c 18 999 Fire Rd
Desired result:
key count location id
a 17 123 Test Rd 1, 11, 12
b 10 12 Smith St 2
c 18 999 Fire Rd 3, 33
I am trying to join this table with another table that I have so that the ID is carried over, but the issue I am running in to is that my join result is getting a larger number of rows than desired simply because it is creating a row for each unique ID that a key has. Is there a way to do the JOIN in a way where it groups each id together (perhaps comma delimited) in one single row?
You can use STRING_AGG:
SELECT t.key, s.count, s.location, STRING_AGG(t.id, ',') as id
FROM YourTable t
JOIN YourTable2 s
ON(t.key = s.key)
GROUP BY t.key, s.count, s.location

inserting sql with loop

I have the table PERSONAL
ID_PERS NAME
---------------
11 azerty
22 uiop
and the table TOURNE_LABEL
ID_TOUR NAME
--------------
1 w
2 p
3 v
I want to loop over all of person and then join it with tourne and insert to a new table.
I have created empty table LS_PDA
ID_PERS ID_TOURN
-------------------
11 1
11 2
11 3
22 1
22 2
22 3
how can I do that ?
SQL is all about set based operations. If you're thinking about a loop, chances are you're heading in the wrong direction. For this problem, you could cross-join the tables, thus producing all the possible combinations, and use the insert-select syntax:
INSERT INTO ls_pda
SELECT id_pres, id_tour
FROM personal
CROSS JOIN tourne_label

SQL copy from one table to another with changing ID value

I have two tables:
A
ID VALUE
----------
1 7
2 5
3 44
4 982
5 1
6 0
7 671
B
ID VALUE
---------------
1 6
2 6
3 77
4 22
How do I copy data from #B to #A to get a different ID (one bigger than the MAX in #A)? For example I need to get
ID VALUE
1 7
2 5
3 44
4 982
5 1
6 0
7 671
8 6
9 6
10 77
11 22
Either make it an IDENTITY column which auto-increments, or this:
INSERT INTO A
SELECT b.ID + (SELECT MAX(ID) FROM A) AS ID, b.Value
FROM B
DEMO
The select is slightly different if the ID in table B has gaps. Then those gaps are transferred.
If the ID column in TableA is not already set to auto-increment, do the following command:
ALTER TABLE TableA MODIFY COLUMN ID INT auto_increment
Now you can just insert all the records from TableB into TableA:
INSERT INTO TableA (VALUE)
SELECT VALUE
FROM TableB
It is not a great idea to rely on the business logic in your query to maintain the order of the ID column. Instead, let SQL take care of it for you; it was designed for this purpose.

sqlquery insert data from one table to other with id from columnname

I have an old database with some complex joining of the data. As given below
Subjects
Id Name
-------------------------------
1 Math
2 Science
3 English
Results
Id StudentId Math MathMax Science ScienceMax English EnglishMax TotalMarks Max
-----------------------------------------------------------------------------------------
1 81 5 10 6 10 3 10 14 30
2 82 8 10 8 10 9 10 25 30
3 83 7 10 8 10 7 10 22 30
Now I am trying to convert it to more easy and readable database. So I come up with the tables like
Results
ResultId StudentId TotalMarks MaxMarks
-------------------------------------------
1 81 14 30
2 82 25 30
3 83 22 30
ResultDetails
Id ResultId SubjectId Marks MaxMarks
--------------------------------------------------------
1 1 1 5 10
2 1 2 6 10
3 1 3 7 10
& so one
Now the real question I can insert data in the new Results table but I am confused on the second table. I can't understand that how to pass column name of one table and get the id of that name from second table and insert it in the third one.
I am trying on but can't understand the right commands to achieve this. My database already have 50000+ records and I have to merge them according to this new tables.
Assuming this is a one-off conversion of data, and you've already populated your new Results table, something as simple as the following should work:
INSERT INTO ResultDetails(ResultId, SubjectId, Marks, MaxMarks)
SELECT
R.StudentId,
1 AS SubjectId,
OldR.Math AS Marks,
OldR.MathMax AS MaxMarks
FROM Results R
INNER JOIN OldResults OldR ON R.StudentId = OldR.StudentId
INSERT INTO ResultDetails(ResultId, SubjectId, Marks, MaxMarks)
SELECT
R.StudentId,
2 AS SubjectId,
OldR.Science AS Marks,
OldR.ScienceMax AS MaxMarks
FROM Results R
INNER JOIN OldResults OldR ON R.StudentId = OldR.StudentId
INSERT INTO ResultDetails(ResultId, SubjectId, Marks, MaxMarks)
SELECT
R.StudentId,
3 AS SubjectId,
OldR.English AS Marks,
OldR.EnglishMax AS MaxMarks
FROM Results R
INNER JOIN OldResults OldR ON R.StudentId = OldR.StudentId
It's not a very elegant solution, but it doesn't need to be for a one-off conversion.

Keeping track of auto increment columns in a cursor

I wrote a cursor where I needed to copy some rows and insert it into a table with only 1 column different. Say for example I have a table A like this
Id | CID | Country
1 X A
2 X B
3 X C
'Id' in this table is auto increment and primary key and Country is the countries visited by that CID. So when I was asked to update the table where every country visited by CID 'X' has also been visited by CID 'Y'. I wrote a cursor and did an insert into with CID 'Y' and the Country. After I executed my cursor, table A became this:
Id | CID | Country
1 X A
2 X B
3 X C
4 Y A
5 Y B
6 Y C
Now there is another table B that is as follows, where AId is the foreign key referencing Id in A.
AId | AgeOfKids
1 20
1 23
1 28
2 21
2 24
2 29
3 22
3 25
I want to be able to add rows to this table such that it becomes:
AId | AgeOfKids
1 20
1 23
1 28
2 21
2 24
2 29
3 22
3 25
4 20
4 23
4 28
5 21
5 24
5 29
6 22
6 25
To explain in words, the AIds that have the same countries in table A must have the same BeIds in table B. Is it possible to accomplish this? Is so, how can I accomplish this?
Use the OUTPUT clause:
declare #t table (Id int not null,Country char(1) not null)
insert into TableA (CID,Country)
output inserted.id,inserted.Country into #t
select 'Y',Country from TableA where CID = 'X'
And then (should be part of the same batch as the above):
insert into TableB (AId,AgeOfKids)
select t.ID,b.AgeOfKids
from #t t
inner join
TableA a
on
t.Country = a.Country and
a.CID = 'X'
inner join
TableB b
on
a.ID = b.AId
To populate table B.