Solved

Join syntax showing unusual results

Posted on 2008-06-20
7
181 Views
Last Modified: 2011-10-03
Following on from a question I asked yesterday - (http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_23499412.html) I now have a successful query as follows;


SELECT m.ID, m.shortname, m.retailer_name, o.merchantid, o.id AS oid, o.offertitle, o.offerbody, o.a_status, o.offerauthor, o.offershortname, o.clickcount, u.id, u.username
      FROM merchants m
    LEFT JOIN hurry_offers o
      ON m.id=o.merchantid
    LEFT JOIN hurry_users u
      ON o.offerauthor=u.ID
      ORDER BY oid DESC


I would now like to add a further join in order to get the number of comments against each 'offer'.  The query I currently have is;

SELECT m.ID, m.shortname, m.retailer_name, o.merchantid, o.id AS oid, o.offertitle, o.offerbody, o.a_status, o.offerauthor, o.offershortname, o.clickcount, u.id, u.username, count(c.offer_id) AS c_cnt, c.offer_id
      FROM merchants m
    LEFT JOIN hurry_offers o
      ON m.id=o.merchantid
    LEFT JOIN hurry_users u
      ON o.offerauthor=u.ID
    LEFT JOIN hurry_comments c
      ON o.id=c.offer_id
    GROUP BY c.offer_id
      ORDER BY o.id DESC

This works in as much as it is returning the correct figures for each element (i.e the correct username, comment count etc) however I'm only getting 2 results returned at the moment.  Unusually, there doesn't seem to be any reason that I can see for the 2nd of those results being returned - for example, it's not the 2nd row, it doesn't have any comments etc.

Having been working on this for some time, I'm hoping it's just something simple I've missed!

Any help would be much appreciated
0
Comment
Question by:Maxafi
  • 3
  • 2
7 Comments
 
LVL 9

Expert Comment

by:Tone' Shelby
ID: 21852060
I know you said it's not the second row etc, but is any of the data in any of the fields for ether result even slightly different morespecifically in the c.offerid field. Even one difference in a character  in the data could pull back 2 results icluding a blank space ..You may try Trimmiming the c.offerid just to be sure. I've had trailing invisible spaces cause me grief like this before..I'd check data differences and leading / trailing blank spaces in the data first.
Hope it helps..
0
 

Author Comment

by:Maxafi
ID: 21868891
Thanks for the input, tshel.

Unfortunately, trimming hasn't helped - same result.
0
 
LVL 9

Expert Comment

by:Tone' Shelby
ID: 21879999
Can you provide a Create Table statement with some sample data with the two records of data and I'll be happy to dig in and see what I can come up with...
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Expert Comment

by:Tone' Shelby
ID: 22196263
Hello...checking in on you. How are you coming along. Do you need further assistance / information  on this...thanks!
0
 

Author Comment

by:Maxafi
ID: 22348445
Hi tshel,

Apologies for not replying sooner - I must have missed the email notifications.

I did eventually solve this by doing a subquery to get the comment count, which did the trick.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 22793294
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now