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


Select Distinct record based on a field (I am using CTE)


Select Distinct record based on a field (I am using CTE)

Author
Message
skb 44459
skb 44459
SSC Veteran
SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)

Group: General Forum Members
Points: 297 Visits: 297
I would like to select unique records using childkey field. the resultset should not include any record with repeated childkey.

IF My Data looks like
ParentKey ChildKey
---------- ---------
PARENT1 CHILD1
PARENT1 CHILD2
PARENT2 CHILD1

Select Statement Result should be
ParentKey ChildKey
---------- ---------
PARENT1 CHILD1
PARENT1 CHILD2

HERE is the SP.
WITH KIDD (ParentKey, ChildKey)
AS
(
select KI_SLDrillDown.ParentKey, KI_SLDrillDown.ChildKey
from KI_SLDrillDown
where KI_SLDrillDown.ChildKey = ('O_123456' )
and KI_SLDrillDown.ParentKey is NULL
union ALL
select KI_SLDrillDown.ParentKey, KI_SLDrillDown.ChildKey from KI_SLDrillDown
inner join KIDD as d
on KI_SLDrillDown.ParentKey = d.ChildKey
)
SELECT ParentKey, ChildKey
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