[thelist] counting frequency of distinct values in MSSQL

John Evolt at Phuket-Island.com
Sun Sep 14 23:55:10 CDT 2003


 >> Joel D Canfield wrote:
 >>=20
 >> > I need to count the=20
 >> > frequency of each unique numerical value in a table.
 >>=20
 >> You need to use group by:
 >>=20
 >> select column, count(column) from table group by column;
 >Bingo! Thanks, Simon
 >Josh:

How about if the numerical value being counted was a primary
index (foreign key?) from another table - how could I return the
corresponding fields from that table next to the counts?

Would I use a join statement? I keep getting syntax errors when
I try this and variations (using MySQL).

SELECT column, foreign_column,
COUNT(column)
FROM table, foreign_table
GROUP_BY column
JOIN ON foreign_table
WHERE column = foreign_column;

Anyone know of any good (simple;-) tutorials about getting data
from two tables (queries using foreign keys)?

Thanks John




More information about the thelist mailing list