powered by simpleCommunicator - 2.0.60     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Что-то не пойму c SQL в PG 8.2.1
3 сообщений из 3, страница 1 из 1
Что-то не пойму c SQL в PG 8.2.1
    #34245283
Vzhik
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Пишу

Код: plaintext
select distinct count(df1b_r_id) from data_form1b where df1b_date =  200604  and df1b_f_id =  1  group by df1b_r_id

получаю 40 (без distinct 36 записей по 40).

Пишу

Код: plaintext
select df1b_r_id from data_form1b where df1b_date =  200604  and df1b_f_id =  1  group by df1b_r_id

получаю 36 записей айдишников (правильно).

Уточняю запрос
Код: plaintext
select count(df1b_r_id) from data_form1b where df1b_date =  200604  and df1b_f_id =  1  and df1b_pf1b_id =  1  group by df1b_r_id

получаю 36 записей по 1.

а собственно COUNT должен записи считать вроде. В чем я не прав?
...
Рейтинг: 0 / 0
Что-то не пойму c SQL в PG 8.2.1
    #34245362
Алексей Ключников
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Все дело в GROUP BY


Дока

GROUP BY will condense into a single row all selected rows that share the same values for the grouped expressions. expression can be an input column name, or the name or ordinal number of an output column (SELECT list item), or an arbitrary expression formed from input-column values. In case of ambiguity, a GROUP BY name will be interpreted as an input-column name rather than an output column name.

Aggregate functions, if any are used, are computed across all rows making up each group, producing a separate value for each group (whereas without GROUP BY, an aggregate produces a single value computed across all the selected rows). When GROUP BY is present, it is not valid for the SELECT list expressions to refer to ungrouped columns except within aggregate functions, since there would be more than one possible value to return for an ungrouped column.

т.е. Агрегационные функции работают отдельно для каждой группы.
...
Рейтинг: 0 / 0
Что-то не пойму c SQL в PG 8.2.1
    #34245381
Vzhik
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Алексей Ключников
Спасибо.
Наступило просветление мозгов ;-)
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Что-то не пойму c SQL в PG 8.2.1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]