Reputation: 1
P1 (Master table) Cols:Teamname TeamID
Data: ABC 1
DEF 2
P2 (Fieldmapping) Cols:FieldDesc Fieldname TeamID:
Data:Goals Text1 1,
Player Text2 1,
Shots Text1 2,
Batsman Text2 2,
Runs Text3 2,
P3 (Values) Cols:TeamID text1 text2 text3 text4 text5 text6 text7 text8 text9 text10
Data: 1 3 John
1 2 Mark
1 1 Stu
2 10 Bart 14
2 3 Mary 6
2 50 Ba 100
Expected Output: If the user selects the ABC then the output should be
O/p: Col: Team Goal Player
Data: ABC 3 John
ABC 2 Mark
ABC 1 Stu
Expected Output: If the user selects the DEF then the output should be
O/p: Col: Team Shots Batsman Runs
Data: DEF 10 Bart 14
DEF 3 Mary 6
DEF 50 Ba 100
How can i write this in SQL ?
Upvotes: 0
Views: 27
Reputation: 7503
You need to learn how to use join in sql queries. Here you can join table p1
and table p3
on teamId
to get the desired results.
select
teamname,
shots,
batsman,
coalesce(runs, 0) as runs
from p1
join p3
on p1.teamId = p3.teamId
where teamname = 'DEF'
order by
teamname
Output:
| teamname | shots | batsman | runs |
| -------- | ----- | ------- | ---- |
| DEF | 10 | Bart | 14 |
| DEF | 3 | Mary | 6 |
| DEF | 50 | Ba | 100 |
Upvotes: 0