我與這個停止的線程有同樣的問題。如何分割複合列像ROW,ARRAY?
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的回答說:
ERROR:子查詢必須返回只有一列
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
...這工作,但運行在同一子查詢塊的兩倍。
這會導致性能下降。
(但是,這個結果表是我想要的。)
查詢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)
...它的工作原理,但列 'REC' 是複合。
如何將這個'rec'分隔爲類似於Query-2的reminder_header和reminder_footer, 。
有一些程序或tequnique? 或其他解決方案?
謝謝。
+1 @Andomar,我摸索着朝當我看到你的答案時,雖然已經完成了(看到你的版本),但我不得不承認我更喜歡子查詢版本。 – 2010-08-02 12:30:00