Home > Archive > PostgreSQL SQL > February 2006 > Sub-Select Variable Column Name









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sub-Select Variable Column Name
google.com@pederick.com

2006-02-19, 8:37 pm

Hi all,

I'm stuck on how to compose a particular query. I need to return a
particular single column that varies depending on the parent SELECT's
result.

The tables are:
Quarries
------------------
QuarryId | Melway
------------------
2 | 101 H02

Distances
--------------------------------------------------
QuarryId | Map | A01 | A02 | A03 | ... | K11 | K12
--------------------------------------------------
2 | 101 | 15 | 15 | 16 | ... | 24 | 24



So I'm select the appropriate field from the Distances table based on
the Melway specified in the Quarry table:

SELECT Quarries.Melway,
(
SELECT SUBSTR(Quarries.Melway,5,3)
FROM Distances
WHERE (QuarryId=Quarries.QuarryId)
AND (Map=SUBSTR(Quarries
.Melway,1,3))
)
FROM Quarries;


Of course this just returns the grid reference (H02) - how do I tell it
that I want the column name of 'x' rather than the literal string?

Thanks in advance for any help. :)


Scott

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com