Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
njguy
Starting Member
16 Posts |
Posted - 2012-05-10 : 14:52:36
|
| SELECT Users.Name, COUNT(Ratings.UserId) as RankedTotalFROM Users Users LEFT OUTER JOIN Ratings Ratings ON Users.UserId = Ratings.UserIdWHERE Users.Inactive = 0GROUP BY Users.NameORDER BY Users.NameThe SQL above works correctly but I wanted to add another column to give me a count from another table. The Query for the count be.SELECT COUNT(*)FROM IdeasWHERE Refrence is not nullHow do I incorporate these two together. |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-05-10 : 15:03:41
|
Since the tables don't seem to be related in anyway, not sure what the data tells you. But, if you need to you can do it like this:SELECT Users.Name, COUNT(Ratings.UserId) AS RankedTotal, c.IdeasCountFROM Users Users LEFT OUTER JOIN Ratings Ratings ON Users.UserId = Ratings.UserId CROSS JOIN (SELECT COUNT(*) AS IdeasCount FROM Ideas WHERE Refrence IS NOT NULL) cWHERE Users.Inactive = 0GROUP BY Users.NameORDER BY Users.Name |
 |
|
|
njguy
Starting Member
16 Posts |
Posted - 2012-05-10 : 16:26:43
|
| Thanks....but I am getting the following error based on the code you sent8120 - '[Microsoft][SQL Server Native Client 10.0][SQL Server]Column 'c.IdeasCount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.SQL State is: 42000' |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-10 : 17:10:01
|
quote: Originally posted by njguy Thanks....but I am getting the following error based on the code you sent8120 - '[Microsoft][SQL Server Native Client 10.0][SQL Server]Column 'c.IdeasCount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.SQL State is: 42000'
Add column c.IdeasCount in group by clause.SELECT Users.Name, COUNT(Ratings.UserId) AS RankedTotal, c.IdeasCountFROM Users Users LEFT OUTER JOIN Ratings Ratings ON Users.UserId = Ratings.UserId CROSS JOIN (SELECT COUNT(*) AS IdeasCount FROM Ideas WHERE Refrence IS NOT NULL) cWHERE Users.Inactive = 0GROUP BY Users.Name,c.IdeasCountORDER BY Users.Name |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-05-10 : 19:32:59
|
Thanks Vijay! |
 |
|
|
|
|
|