Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using COUNT(*) within a INNER JOIN 1

Status
Not open for further replies.
Jun 9, 2006
159
US
Why does this query not work?

select A.*, c.count(*) views, B.Username from video a
inner join user b on a.userid = b.userid
inner join video_view c on a.videoid = c.videoid

It complains:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '*'.

I also tried c.count(videoid) and count(c.videoid)... neither worked.

Thanks,



Shawn Molloy
Seattle, WA
 
You can't use c.count(*), the correct syntax is count(c.SomeField). Also when you have COUNT(), SUM() etc. you must have GROUP BY. But when you have GROUP BY you must include ALL fields which are not involved in agregate functions. So beeter try:
Code:
select A.*,
       c.views,
       B.Username
from video a
inner join user b on a.userid = b.userid
inner join (SELECT videoid, COUNT(*) AS Views
                   FROM video_view
                   GROUP BY videoid)c
      on a.videoid = c.videoid


Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top