Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Thank you again! I can't tell you how much I and my company appreciate what you've done! I love this place!..."

Geography

Where in the world do Tek-Tips members come from?

Trying to include a COUNT from another table...Helpful Member! 

Amesville (Programmer)
14 May 12 13:48
Hi Folks

I'm trying to collect data from two tables where I would ordinarily use a left join on the columns that relate the tables. But I'm trying to bring back a count of items from the second table where on the column being used where the table repation would take place. Something like this:

CODE

SELECT COUNT(wb.WaybillID), Tr.TrID, Tr.TrName, CAST(Tr.trMaxCars * (Tr.TrPctThru * 0.01) AS integer) as TrCarLimit, Tr.TrFromStagingArea FROM Train Tr LEFT JOIN Waybill wb ON tr.TrID = wb.TrainAssignment WHERE Tr.TrOriginateOL = 'Y' GROUP BY Tr.TrID
Amesville (Programmer)
14 May 12 13:50
Sorry, somehow that got posted before I was done. Anyway, this statement tells me that Tr.TrID is invalid in the select because it doesn't appear in the GROUP BY Clause. I'm not sure how to fix this, I'm afraid I might have to break this down into a cursor? Can anyone suggest a fix?

Thanks

Craig
Helpful Member!  gmmastros (Programmer)
14 May 12 13:59
There are several ways you can write this query to return the results you are looking for. The following is called a derived table method. Basically, you write a query that returns the minimum data you need, and then you wrap the query in parenthesis and treat it (from the aspect of the outer query) as though it was a separate table. Like this:

CODE

SELECT wb.WaybillCount, Tr.TrID, Tr.TrName, CAST(Tr.trMaxCars * (Tr.TrPctThru * 0.01) AS integer) as TrCarLimit, Tr.TrFromStagingArea FROM Train Tr LEFT JOIN ( Select TrainAssignment, Count(WayBillId) As WayBillCount From WayBill Group By TrainAssignment ) As wb ON tr.TrID = wb.TrainAssignment WHERE Tr.TrOriginateOL = 'Y'

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

Amesville (Programmer)
14 May 12 14:06
Thank you George! That did the trick. I will take a good look at it and try to learn something from it... :)

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close