complex query

Поиск
Список
Период
Сортировка
От Mark Fenbers
Тема complex query
Дата
Msg-id 508C75D1.2070906@noaa.gov
обсуждение исходный текст
Ответы Re: complex query  (Scott Marlowe <scott.marlowe@gmail.com>)
Список pgsql-sql
I have a query:<br /> SELECT id, SUM(col1), SUM(col2) FROM mytable WHERE condition1 = true GROUP BY id;<br /><br />
Thisgives me 3 columns, but what I want is 5 columns where the next two columns -- SUM(col3), SUM(col4) -- have a
slightlydifferent WHERE clause, i.e., WHERE condition2 = true.<br /><br /> I know that I can do this in the following
way:<br/> SELECT id, SUM(col1), SUM(col2), (SELECT SUM(col3) FROM mytable WHERE condition2 = true), (SELECT SUM(col4)
FROMmytable WHERE condition2 = true) FROM mytable WHERE condition1 = true GROUP BY id;<br /><br /> Now this doesn't
seemto bad, but the truth is that condition1 and condition2 are both rather lengthy and complicated and my table is
ratherlarge, and since embedded SELECTs can only return 1 column, I have to repeat the exact query in the next SELECT
(exceptfor using "col4" instead of "col3").  I could use UNION to simplify, except that UNION will return 2 rows, and
thecode that receives my resultset is only expecting 1 row.<br /><br /> Is there a better way to go about this?<br
/><br/> Thanks for any help you provide.<br /> Mark<br /><br /> 

В списке pgsql-sql по дате отправления:

Предыдущее
От: Jasen Betts
Дата:
Сообщение: Re: Trigger triggered from a foreign key
Следующее
От: Scott Marlowe
Дата:
Сообщение: Re: complex query