1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQL エラー 備忘録 自分用

Last updated at Posted at 2022-01-20

GROUP BY句で指定するか、集約関数内で使用しなければなりません

自分用

DB:PostgreSQL

    SELECT
        history_id,
        COUNT(*)
    FROM
        (
        SELECT
            his.*
        FROM 
        housework_history AS his
        WHERE
        works_id= 2/*i*/
        AND
        flag_nexttime_comp = FALSE
        ) AS A

欲しい結果

|history_id|count|
|----|----|----|
|2|2||
|31|2||

列"a.history_id"はGROUP BY句で指定するか、集約関数内で使用しなければなりません

GROUP BY追加

GROUP BY history_id;

結果

|history_id|count|
|----|----|----|
|2|1||
|31|1||

集約関数

集約関数は複数の入力値から単一の結果を計算

分析関数

それぞれの行で集計結果を計算

OVER()をつけることで分析関数として使うことができる。

COUNT(*)
↓
COUNT(*)OVER()

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?