0

I have a table with 3 columns and looks like

branchid  semesterid  courseid
 4          25           2        
 4          36           5
 4          23           3
 4          12           10 
 4          34           15
 4          2            7
 4          23           42

 7          23           9
 7          10           6
 7          34           3
 7          20           17

I need the count of semesterid and courseid of that branch

SELECT branchid, count(semesterid), count(courseid) WHERE branchid = 4

When I execute this query, the count(semesterid) shows wrong count and courseid shows correct count of 7

1
  • what is the value semesterid shows? Commented Sep 13, 2012 at 13:40

3 Answers 3

1

With my crystal ball I predict you need this instead:

SELECT COUNT(DISTINCT semesterid), 
       COUNT(DISTINCT courseid) 
  FROM Table1 
 WHERE branchid = 4;

... as the query shown should work just fine for counting the number of records (but not unique records).

Sign up to request clarification or add additional context in comments.

Comments

0

try:

SELECT branchid, 
       count(distinct semesterid) as semesterid, 
       count(distinct courseid) as courseid
FROM   <table>
WHERE  branchid = 4
group by branchid

Comments

0

If you want the number of unique results you have to replace count(id) with count(distinct id)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.