Solved

help with my sql query select in a select

Posted on 2012-03-18
3
226 Views
Last Modified: 2012-03-18
Select UserID, (select count(*) from names where userid = userid) as totalcount from users where firstname like '%john%'


The error is "where userid = userid".  I don't know how to word this.  UserID's are unique accross both tables.  I want to return results for all firstnames like "%john%" out of my Users table, and also, for each record, I want the count(*) from names for that same UserID.
0
Comment
Question by:arthurh88
3 Comments
 
LVL 7

Accepted Solution

by:
micropc1 earned 500 total points
Comment Utility
You need an alias for the outer table...

Select u.UserID, (select count(*) from names where userid = u.userid) as totalcount 
from users u
where u.firstname like '%john%'

Open in new window

0
 
LVL 39

Expert Comment

by:appari
Comment Utility
or try like this

Select users.UserID, count(names.userid) as totalcount
from users left outer join names
on users.userid = names.userid
where firstname like '%john%'
Group by users.UserID
0
 

Author Closing Comment

by:arthurh88
Comment Utility
that was it!  I knew it was something easy
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

772 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

10 Experts available now in Live!

Get 1:1 Help Now