-1

Hello I have a database that contains the following setup

id  |  message
1      5
2      5
3      6

in the message columns I have certain integers that can be duplicate. The thing I want to get now is to have all the message values AND how many time they appear in the column.

So the output should be

5(2)
6(1)

Can someone help me out how the query should be?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Reshad
  • 2,570
  • 8
  • 45
  • 86

2 Answers2

0
select count(*) as frequency, message from table_name group by message;

For your sample data, this should return:

frequency  message
    2         5
    1         6
0
DECLARE @T TABLE (id INT, message INT)
INSERT INTO @T
VALUES
(1,5),
(2,5),
(3,6)

SELECT Message , COUNT(*) NumOfMsgs
FROM @T
GROUP BY Message

Result Set

Message NumOfMsgs
 5        2
 6        1
M.Ali
  • 67,945
  • 13
  • 101
  • 127