[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Sql Syntax

Posted on 2008-10-06
8
Medium Priority
?
238 Views
Last Modified: 2012-05-05
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


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

Open in new window

0
Comment
Question by:grogo21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22655245
What is the actual data and expected results.  My current understanding is you want the sum for the whole entire list and likewise for count.  If that is true something like this would work.
WITH linkCTE AS (
	SELECT l.LinkID, l.NavigateURL, l.ImageFile, l.Status
	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 
)
SELECT DISTINCT *
,  (SELECT Sum(u.rating) FROM linkCTE) As Rating
,  (SELECT Count(o.LinkID) FROM linkCTE) As Clicks
FROM linkCTE

Open in new window

0
 

Author Comment

by:grogo21
ID: 22655460
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!
0
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22655486
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
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
							

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 5

Expert Comment

by:Cvijo123
ID: 22655500
i miss alias after first inner join

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
							

Open in new window

0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 400 total points
ID: 22655520
In that case, simply use something like what was just suggested.
0
 

Author Comment

by:grogo21
ID: 22655642
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
0
 
LVL 5

Accepted Solution

by:
Cvijo123 earned 1600 total points
ID: 22655651
this should do it
SELECT
        l.LinkID,
        l.NavigateURL,
        l.ImageFile,
        l.Status,
        isnull(u.rating,0) AS Rating,
        isnull(o.Clicks,0) AS Clicks
FROM LinkIndex l
left join ( 
                Select sum(rating) as Rating, LinkID
                from UserRatings 
                group by LinkID
        ) u
on l.LinkID = u.LinkID
left join ( 
                Select count(LinkID) as Clicks, LinkID
                from OutboundTraffic
                group by LinkID
        ) o
on l.LinkId = o.LinkId

Open in new window

0
 

Author Comment

by:grogo21
ID: 22655951
YESS!!

Thanks so much!
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question