grogo21
asked on
Sql Syntax
Hello, Im having trouble writing a sql query. I need to select l.LinkID, l.NavigateURL, l.ImageFile, l.Status and then also get a sum of rating field and a count of LinkID field where LinkID is equal to the one in the select statment. My query below returns incorrect results for the count and sum fields.
How can i fix this?
Thanks
How can i fix this?
Thanks
SELECT l.LinkID, l.NavigateURL, l.ImageFile, l.Status, Sum(u.rating) As Rating, Count(o.LinkID) As Clicks
FROM LinkIndex l, UserRatings u, OutboundTraffic o
Where l.LinkID = u.LinkID and l.LinkID = o.LinkID
and l.Status In(0,1,2) and l.UserID = 1001
Group By l.LinkID, l.NavigateURL, l.ImageFile, l.Status
ASKER
Hello,
I need to select LinkIndex.LinkID, LinkIndex.NavigateURL, LinkIndex.ImageFile, LinkIndex.Status.
I also need to select sum(UserRatings.Rating) of all records from UserRatings Table where UserRatings.LinkID = LinkIndex.LinkID(The Link id in the select statment above)
I also need to select count(*) of all records from OutboundTraffic Table where OutboundTraffic .LinkID = LinkIndex.LinkID(The Link id in the select statment above)
I tried your query and i got error:
The multi-part identifier "u.rating" could not be bound.
Thanks!
I need to select LinkIndex.LinkID, LinkIndex.NavigateURL, LinkIndex.ImageFile, LinkIndex.Status.
I also need to select sum(UserRatings.Rating) of all records from UserRatings Table where UserRatings.LinkID = LinkIndex.LinkID(The Link id in the select statment above)
I also need to select count(*) of all records from OutboundTraffic Table where OutboundTraffic .LinkID = LinkIndex.LinkID(The Link id in the select statment above)
I tried your query and i got error:
The multi-part identifier "u.rating" could not be bound.
Thanks!
can u try this one will it work:
It can be done better my code, but i dont know wich table multiple your records so this way should work i think
It can be done better my code, but i dont know wich table multiple your records so this way should work i think
SELECT
l.LinkID,
l.NavigateURL,
l.ImageFile,
l.Status,
u.rating AS Rating,
o.Clicks AS Clicks
FROM LinkIndex l
inner join (
Select sum(rating) as Rating, LinkID
from UserRatings
group by LinkID
)
on l.LinkID = u.LinkID
inner join (
Select count(LinkID) as Clicks, LinkID
from OutboundTraffic
group by LinkID
) o
on l.LinkId = o.LinkId
i miss alias after first inner join
so correct syntax should be
so correct syntax should be
SELECT
l.LinkID,
l.NavigateURL,
l.ImageFile,
l.Status,
u.rating AS Rating,
o.Clicks AS Clicks
FROM LinkIndex l
inner join (
Select sum(rating) as Rating, LinkID
from UserRatings
group by LinkID
) u
on l.LinkID = u.LinkID
inner join (
Select count(LinkID) as Clicks, LinkID
from OutboundTraffic
group by LinkID
) o
on l.LinkId = o.LinkId
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cvijo123 your query looks good but there is one thing I still need. When LinkId is not in OutboundLinks or UserRatings Table it does not return anything. How can I select 0 for u.rating AS Rating or o.Clicks AS Clicks if they are not contained in the tables?
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
YESS!!
Thanks so much!
Thanks so much!
Open in new window