Multiple counts on a single column SQL

JimmyPop13

I am currently running a query like the following:

SELECT a.ID, a.ContactID, a.Code, 
FROM tableA a
JOIN (SELECT ContactID, Code
      FROM tableA
      WHERE ContactID IS NOT NULL
      GROUP BY Code, ContactID
      HAVING COUNT(Code) > 1) b
ON (a.Code = b.Code AND a.ContactID = b.ContactID)
WHERE a.ContactID IS NOT NULL
ORDER BY a.Code

This returns data that looks like the folloing:

table : a    
    +-------+-----------+-----------+
    |   ID  | ContactID |   Code    |     
    +-------+-----------+-----------+
    |   1   |    111    |  abcd2    |   
    |   2   |    111    |  abcd2    |   
    |   3   |    222    |  abcd1    |  
    |   4   |    222    |  abcd1    |   
    |   5   |    222    |  abcd1    |  
    |   6   |    222    |  abcd1    |
    +-------+-----------+-----------+

So as you can see I get ContactID's that have more then one of the same Code.

The problem with this is, is that I don't want all this output (real table is much larger). I want a COUNT to go along side the Code column and just show one row for each iteration of Code. Like the following:

 +-------+-----------+-----------+------+
 |   ID  | ContactID |   Code    |COUNT |    
 +-------+-----------+-----------+------+
 |   1   |    111    |  abcd2    |   2  | 
 |   3   |    222    |  abcd1    |   4  |
 +-------+-----------+-----------+------+

Any help on this would be great and I hope I have explained my problem well enough. If not please ask for more information and if this has been answered before please point in that direction.

Thanks.

Tanner

Your solution and other answers are way to complicated, you don't need the self join when you're simply aggregating with HAVING Count(x) > 1:

SELECT MIN(ID), ContactID, Code, COUNT(Code) AS [COUNT]
FROM tableA
WHERE ContactID IS NOT NULL
GROUP BY Code, ContactID
HAVING COUNT(Code) > 1

Full solution:

SQL Fiddle

CREATE TABLE TableA
    ([ID] int, [ContactID] int, [Code] varchar(5))
;

INSERT INTO TableA
    ([ID], [ContactID], [Code])
VALUES
    (1, 111, 'abcd2'),
    (2, 111, 'abcd2'),
    (3, 222, 'abcd1'),
    (4, 222, 'abcd1'),
    (5, 222, 'abcd1'),
    (6, 222, 'abcd1')
;

Query 1:

SELECT min(id), ContactID, Code, count(Code) as [COUNT]
      FROM tableA
      WHERE ContactID IS NOT NULL
      GROUP BY Code, ContactID
      HAVING COUNT(Code) > 1

Results:

|   | ContactID |  Code |   |
|---|-----------|-------|---|
| 1 |       111 | abcd2 | 2 |
| 3 |       222 | abcd1 | 4 |

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Single column with value counts from multiple column dataframe

How to replace multiple values in a single column with SQL?

getting multiple counts in SQL

Multiple Counts with single query in mongodb

Convert from single column to multiple Column in sql Server

MS SQL Concatenate multiple values into a single column

How do I get counts for different values of the same column with a single totals row, using Postgres SQL?

Multiple Counts in a single query SQL

Pandas GroupBy a single column and display multiple columns as value counts

How do I combine multiple counts in a single SQL query

multiple counts in single mysql query

Multiple counts on single grouped values

MYSQL - Multiple counts in a single query

SQL creating both multiple column index and single column index on a Table

SQL Query to Transpose Column Counts to Row Counts

sql server multiple column values in single column by building query

sql convert single column to multiple based on delimiter

Sql single column to multiple on '.' delemiter

Multiple counts in a single SQL query

SQL Group By with multiple counts

Multiple counts in single query with multiple joining of tables - SQL

SQL: How to divide column by counts?

SQL Multiple constraints on a single column

SQL select multiple values in single column into multiple columns

SQL Query to get multiple resultant on single column

SQL SUM multiple Rows into a single Column

SQL mapping multiple records to single column value

SQL join on multiple columns or on single calculated column

Does there exist a 'single' SQL query for getting unique counts by column?

TOP Ranking

HotTag

Archive