I have same problem with this stopped thread.
http://www.mail-archive.com/[email protected]/msg28070.html
QUERY-1
SELECT
r.id,
(
SELECT
rl.reminder_header,
rl.reminder_footer
FROM reminder_levels AS rl
WHERE rl.lookup =
(
SELECT MAX(reminder_level_lookup)
FROM reminders
WHERE customer_id = r.customer_id
)
)
FROM reminders AS r
Postgresql replied that:
ERROR: subquery must return only one column
QUERY-2
SELECT
r.id,
(
SELECT
rl.reminder_header
FROM reminder_levels AS rl
WHERE rl.lookup =
(
SELECT MAX(reminder_level_lookup)
FROM reminders
WHERE customer_id = r.customer_id
)
) AS reminder_header,
(
SELECT
rl.reminder_footer
FROM reminder_levels AS rl
WHERE rl.lookup =
(
SELECT MAX(reminder_level_lookup)
FROM reminders
WHERE customer_id = r.customer_id
)
) AS reminder_footer
FROM reminders AS r
id | reminder_header | reminder_footer
----+-------------------+--------------------
1 | hogehoge | fugafuga
... which works, but runs twice the same subselect block.
This makes performance kill.
(but, this result table is what I want.)
QUERY-3
SELECT
r.id,
(
SELECT
ROW(rl.reminder_header, rl.reminder_header)
FROM reminder_levels AS rl
WHERE rl.lookup =
(
SELECT MAX(reminder_level_lookup)
FROM reminders
WHERE customer_id = r.customer_id
)
) AS rec
FROM reminders AS r
id | rec
----+----------------------
1 | (hogehoge, fugafuga)
... which works, but column 'rec' is compound.
How to split this 'rec' to reminder_header and reminder_footer, like Query-2.
There is some procedure or tequnique? or other solution?
Thanks.