• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

SQL Sub Query

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
BananaFury
Asked:
BananaFury
  • 2
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>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
 
BananaFuryAuthor Commented:
Great, thanks!
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now