テーブルの構造とデータは次のとおりです。
col a col c count
107 0
107 0
107 0
107 0
108 0
この結果を得るためにどのselectステートメントを書くことができますか?'cola'値の数を数えたい。
col a col c count
107 4
107 4
107 4
107 4
108 1
テーブルの構造とデータは次のとおりです。
col a col c count
107 0
107 0
107 0
107 0
108 0
この結果を得るためにどのselectステートメントを書くことができますか?'cola'値の数を数えたい。
col a col c count
107 4
107 4
107 4
107 4
108 1
count() over()
構文を使用できます:
select cola,
count(cola) over(partition by cola) CountColC
from yourtable
SQL FiddlewithDemoを参照してください
| COLA | COUNTC |
-----------------
| 107 | 4 |
| 107 | 4 |
| 107 | 4 |
| 107 | 4 |
| 108 | 1 |
次に、このデータを使用して更新を実行する場合は、次を使用します。
;WITH cte AS
(
SELECT cola,
colc,
count(cola) over(partition BY cola) CountC
FROM yourtable
)
UPDATE cte
SET colc = countc;
SQL FiddlewithDemoを参照してください
あなたはこれを使うことができます:
select tableName.col_a, cnt
from tableName inner join
(
select col_a, count(col_a) as cnt
from tableName
group by col_a
) tmp on tableName.col_a = tmp.col_a
更新クエリでこのメソッドを使用する場合:
update tableName set col_a = cnt
from
(
select col_a, count(col_a) as cnt
from tableName
group by col_a
) tmp
where tableName.col_a = tmp.col_a
重複するエントリを削除しても問題ない場合は、次のようにします。
SELECT cola, COUNT(1)
FROM TABLE1
GROUP BY cola
これは以下を返します:
コーラコルク 107 4 108 1