I have a table that is set up as:
ID | NameID | Star (1 or 0)
1 | 1 | 0
2 | 1 | 0
3 | 1 | 1
4 | 2 | 1
5 | 2 | 1
6 | 3 | 0
I need to produce a recordset that will give me the NameID, the total occurances of the NameID and the number of these occurances where Star=1. (Unfortunately because the Star field is of a Bit type, I cannot just use SUM which would have made things a lot easier!) Basically I need to produce the following result:
NameID | CountOfID | CountOfStar
1 | 3 | 1
2 | 2 | 2
3 | 1 | 0
I can produce a SELECT statement that gives me CountOfID and I can produce a SELECT statement that gives me CountOfStar but I am having trouble trying to combine these statements to give me one select statement. What I have so far is:
To get the CountOfID:
SELECT tblX.NameID, Count(tblX.ID) AS CountOfID
FROM tblX
GROUP BY tblX.NameID
To get the CountOfStar:
SELECT tblX.NameID, Count(tblX.Star) AS CountOfStar
FROM tblX
WHERE tblX.Star='1'
GROUP BY tblX.NameID
So now I need to combine these so that I can pull out NameID, CountOfID and CountOfStar.
Can anyone help?
ID | NameID | Star (1 or 0)
1 | 1 | 0
2 | 1 | 0
3 | 1 | 1
4 | 2 | 1
5 | 2 | 1
6 | 3 | 0
I need to produce a recordset that will give me the NameID, the total occurances of the NameID and the number of these occurances where Star=1. (Unfortunately because the Star field is of a Bit type, I cannot just use SUM which would have made things a lot easier!) Basically I need to produce the following result:
NameID | CountOfID | CountOfStar
1 | 3 | 1
2 | 2 | 2
3 | 1 | 0
I can produce a SELECT statement that gives me CountOfID and I can produce a SELECT statement that gives me CountOfStar but I am having trouble trying to combine these statements to give me one select statement. What I have so far is:
To get the CountOfID:
SELECT tblX.NameID, Count(tblX.ID) AS CountOfID
FROM tblX
GROUP BY tblX.NameID
To get the CountOfStar:
SELECT tblX.NameID, Count(tblX.Star) AS CountOfStar
FROM tblX
WHERE tblX.Star='1'
GROUP BY tblX.NameID
So now I need to combine these so that I can pull out NameID, CountOfID and CountOfStar.
Can anyone help?