Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 23rd, 2004, 08:15 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sub-Select?

Hello Everyone and thanks for your help in advance. I am working on a query that selects all records from a patient release database, i.e.

Select Distinct MRNumber from tblReleases Where LastName Like 'R%'

Which returns a subset of records that are in the tblRelease table. I then want to see how many visits these patients have had, something like

Select * from tblPatientVisits Where MRNumber = (Select Distinct MRNumber from tblReleases Where LastName Like 'R%')

However this syntax doesn't work. I have also tried Where MRNumber In but that doesn't return the coreect records. Any help on this issue would be greatly appreciated.

Reply With Quote
  #2 (permalink)  
Old October 24th, 2004, 01:19 AM
Authorized User
 
Join Date: Apr 2004
Location: Boise, ID, USA.
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I believe that you would want

Code:
SELECT *
  FROM tblPatientVisits
 WHERE MRNumber = (SELECT DISTINCT MRNumber
                     FROM tblReleases 
                    WHERE LastName LIKE 'R%')

Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
Reply With Quote
  #3 (permalink)  
Old October 24th, 2004, 03:15 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can either use IN:
Code:
SELECT * FROM tblPatientVisits 
WHERE MRNumber IN 
(SELECT DISTINCT MRNumber 
FROM tblReleases
WHERE LastName LIKE '%R')
If you've tried that and it doewsn't work what sort of record is brought back that fails the criterion?
Or I think a better way is to use a join:
Code:
SELECT PV.*
FROM
tblReleases R INNER JOIN tblPatientVisits PV
ON R.MRNumber = PV.MRNumber
WHERE R.LastName LIKE 'R%'
--

Joe
Reply With Quote
  #4 (permalink)  
Old October 24th, 2004, 07:45 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help. The IN clasue worked fine.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select row in GridView WITHOUT using Select button rao965 ASP.NET 2.0 Professional 1 February 15th, 2008 10:44 AM
Select from another select statement to a repeater simsen ASP.NET 2.0 Professional 0 May 2nd, 2007 04:34 PM
Fill select box and select recordset value markd Classic ASP Databases 1 February 20th, 2006 06:41 PM
select="node1", select="node2"... Baldo XSLT 7 March 12th, 2004 10:38 AM
Select Within A Select Problem vinyl-junkie Classic ASP Databases 6 June 7th, 2003 04:31 PM



All times are GMT -4. The time now is 02:47 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.