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


Most efficient way to get a single unique combined record for distributed information corresponding...


Most efficient way to get a single unique combined record for distributed information corresponding to same ID

Author
Message
ScottPletcher
ScottPletcher
SSCrazy Eights
SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)

Group: General Forum Members
Points: 8020 Visits: 7163
MAX(age) seems safe enough -- no one gets any younger :-).
Combining MIN(name) and MAX(name) would allow one name change ... that should handle everyone, right!?



/*
insert into #temp(id,name)
select 2,'Jane Single'
insert into #temp(id,age)
select 2,28
insert into #temp(id,contact_number)
select 2,'123-456-7890'
insert into #temp(id,contact_number)
select 2,'987-654-3210'
insert into #temp(id,name)
select 2,'Jane Married'
*/

SELECT
tGroup.id, ca1.name, tGroup.age, t.contact_number
FROM (
SELECT id, MIN(name) AS name_min, MAX(name) AS name_max, MAX(age) AS age
FROM #temp
GROUP BY id
) AS tGroup
LEFT OUTER JOIN #temp t ON
t.id = tGroup.id
CROSS APPLY (
SELECT DISTINCT name
FROM (
SELECT name_min AS name UNION ALL
SELECT name_max
) AS derived
) AS ca1
WHERE
t.contact_number IS NOT NULL




SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
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