Solved

SQL Sub Query

Posted on 2013-05-23
3
183 Views
Last Modified: 2013-05-23
Hi,

I expect this is a nice easy one.

I have a statement

SELECT a, Sum(b), Sum(c)
FROM tbl1
Group By a

But I want to restrict the results to certain records found in tbl2

Thanks
0
Comment
Question by:BananaFury
[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
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39190801
>But I want to restrict the results to certain records found in tbl2
Then join tbl1 to tbl2, and the ON clause would spell out the relationship between the two tables such that anything that doesn't match in both tables would not be returned.

SELECT tbl1.a, Sum(tbl1.b), Sum(tbl1.c)
FROM tbl1
   JOIN tbl2 ON tbl1.ID = tbl2.tbl1_ID    -- How the tables are joined 
WHERE tbl2.some_value = "something"   -- If you want to restrict it further based on specific criteria
GROUP BY tbl1.a

Open in new window

0
 

Author Closing Comment

by:BananaFury
ID: 39190940
Great, thanks!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39190942
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

696 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