简体   繁体   中英

how do I count 1st column values against the second value(in which 1 st column values are repeated)

[how do I count the number of times p's value is repeated in column n ]

1

If I Understand what you want, you can use this code:

This Code is to create your sample data:

SELECT  1  AS N, 2 AS P INTO #t
UNION ALL
SELECT  3  AS N, 2 AS P
UNION ALL
SELECT  6  AS N, 8 AS P
UNION ALL
SELECT  9  AS N, 8 AS P
UNION ALL
SELECT  2  AS N, 5 AS P
UNION ALL
SELECT  8  AS N, 5 AS P
UNION ALL
SELECT  8  AS N, NULL AS P

Here is the code to count column

SELECT *, (SELECT COUNT(1) FROM #t t WHERE t.N = #t.p) AS c  FROM #t

DROP TABLE #t

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