SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Correlated Subqueries


Correlated Subqueries

Author
Message
nmckinnon
nmckinnon
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 20
Morning,

I have a request from one of my clients to produce a report that lists all his client information and fee details. My problem is that all the fee information is contained in the one table and I need to pass the query three different paramater values for Directors fees, Takeon fees and Fee listing.

Obviously if I use the where statement to filter my recordset I will exclude the records that don't have values for all of the parameters.

I have seen how subqueries can overcome issues like this by effectively running recursive selects on recordsets but I am not sure if this is the way to go. I have produced a couple of basic subqueries but if anyone who has more experience could guide me as to the suitability of the method or suggest a better approach I would be very grateful.

Thank you kindly,

H
Michael Earl-395764
Michael Earl-395764
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12399 Visits: 23078
I would suggest you help us help you a bit:

http://www.sqlservercentral.com/articles/Best+Practices/61537/
k man
k man
SSC Veteran
SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)

Group: General Forum Members
Points: 261 Visits: 138
If I understood you right...could you join to the same table a multiple times to get the different information...something like this...
You would join to the Fees table on the client, but each time only join for the Fee type you are looking for.
Not the best performing query...but...

select * from clienttbl
left outer join feestbl fee1 on clienttbl.id = fee1.id and fee1.type = type1
left outer join feestbl fee2 on clienttbl.id = fee2.id and fee2.type = type2
left outer join feestbl fee3 on clienttbl.id = fee3.id and fee3.type = type3

hope this is helpful...

K
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search