0

Hi

I have two tables that I want to use to build a third. They have a many-to-one relationship for one of the columns. Table A has the "one" side of the many-to-one relationship. Table B has many rows that have foreign keys referencing Table A. I want to write a SQL statement that will return rows that tell how many references to each row in Table B are in Table A. For example:

Table A
1 A
2 B
3 C

Table B
1
1
1
2
2
2
2
3
3


I want the statement to return:
A 3
B 4
C 2

Does this make sense or is more information necessary?

Thany you in advance

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by trihaitran
0

How about :

SELECT COUNT (*) DISTINCT FROM TABLE_NAME;

Definetely not sure, but just give it a shot?

0

I found the solution.

SELECT name, COUNT(fk)
FROM TableA INNER JOIN TableB ON TableA.id = TableB.fk
GROUP BY TableB.fk
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.