Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


SQL search / results from 2 tables

Posted on 2010-01-12
Medium Priority
Last Modified: 2012-05-08
Using SQL Server 2008 Management Studio

Table 1: MasterAccounts
PK   MasterAccount_ID     int
LeadSourceProvider_ID    int

Table 2: LeadSourceProviders
PK    LeadSourceProvider_ID     int
LeadSourceProviderName       nvarchar(50)

the result I am looking for is as follows (when executing the sql script) :

LeadSourceProviderName,  [count of Master Accounts using that LeadSourceProviderName)
LeadSourceProviders has a 1-to-Many relationship with MasterAccounts table.

How would you construct a SQL query to do this please?
Question by:amillyard
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
LVL 75

Accepted Solution

Aneesh Retnakaran earned 2000 total points
ID: 26294133
SELECT a.LeadSourceProviderName, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID ) as cnt
from LeadSourceProviders a

Author Closing Comment

ID: 31676146
spot on -- thank you aneeshattingal  :-)

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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