I have two tables
People
PersonId | Device1 | Device2 | Device3
---------+---------+---------+--------
1 10 20 30
Device
DeviceId | Name | Description | PicklingCoefficient
---------+------+-------------+--------------------
10 AA Whatever 1
20 BB Who knows -1
30 CC Blah blah 100
And a query that joins the first to the second three times:
select p.PersonId, d1.*, d2.*, d3.* from People p
left join Device d1 on d1.DeviceId = p.Device1
left join Device d2 on d2.DeviceId = p.Device2
left join Device d3 on d3.DeviceId = p.Device3
That produces the table headers in the format (people + device + device + device) where the Device
table fields are just repeated three times:
PersonId | Device1 | Device2 | Device3 | DeviceId | Name | Description | PicklingCoefficient | DeviceId | Name | Description | PicklingCoefficient | DeviceId | Name | Description | PicklingCoefficient
I can technically build a string generator that would produce some unique names for each of the d(n)
tables before I submit my query, but is there a way to simplify it?
Possibly by appending something to the first line of the query to give me the format (people + device1 + device2 + device3):
PersonId | Device1 | Device2 | Device3 | DeviceId1 | Name1 | Description1 | PicklingCoefficient1 | DeviceId2 | Name2 | Description2 | PicklingCoefficient2 | DeviceId3 | Name3 | Description3 | PicklingCoefficient3
I was hoping something like this would work:
select p.PersonId, d1.* + '1', d2.* + '2', d3.* + '3' from People p