简体   繁体   中英

How to count instances in SQL

I have a table like:

在此处输入图片说明

But I want to count the instances of my duplicate id's: 在此处输入图片说明

Any help would be appreciated.

This will be the syntax in MSSQL

 CREATE TABLE #test(id int , x1 int, x2 int)

 INSERT INTO #test(id,x1,x2) VALUES (1,1,1), (2,1,1), (2,2,2);

 SELECT id
        ,ROW_NUMBER() OVER(PARTITION BY id ORDER BY id)
        ,x1 
        ,x2
 FROM #test

Use ROW_NUMBER() function

SELECT id,
       ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) AS idcnt,
       x1,
       x2
  FROM tablename

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM