I have a some set of 4 tables policy table, coverage table, vehicle table and driver tables.
Policy table example
| policy number | record lauyout |
|---|---|
| Abc | polr |
| Efg | polr |
Coverage table example
| Policy number | record layout |
|---|---|
| Abc | prop |
| Abc | |
| Efg | prop |
Vehicle table example
| Policy number | record layout | vin number |
|---|---|---|
| Abc | prp1 | 123 |
| Abc | prp1 | 123 |
| Efg | Prp1 | 456 |
Driver table example
| Policy number | record layout | vin number |
|---|---|---|
| Abc | subj | 123 |
| Abc | subj | 123 |
| Efg | subj | 456 |
I need to fetch first policy record then associated coverage then associated vehicle record then associated driver record. I have to loop all the tables and need to write the logic using SQL whileloop. Please give me some guidelines to achieve this one.
Desired output:
| Policy Number | Record Layout | VIn number |
|---|---|---|
| Abc | polr | null |
| Abc | prop | null |
| Abc | prp1 | 123 |
| Abc | subj | 123 |
| Abc | subj | 123 |
| Efg | polr | null |
| Efg | prop | null |
| Efg | prp1 | 456 |
| Efg | subj | 456 |
You don't need loops, a simple
union allquery with an additional, hard-coded value for the purpose of sorting the results will give you your desired results: