Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2004, 08:15 PM
Authorized User
 
Join Date: Jul 2003
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.

 
Old October 24th, 2004, 01:19 AM
Authorized User
 
Join Date: Apr 2004
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
 
Old October 24th, 2004, 03:15 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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
 
Old October 24th, 2004, 07:45 AM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help. The IN clasue worked fine.






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





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