2

I have table with two columns, both of them contains integer values, that looks like this:

DocumentUpId    RelatedDocId
31608768        31624333
31608768        31624334
31618133        31618117

And I am trying to merge them into a single column like so:

DocumentUpId    
31608768        
31608768        
31618133    
31624333
31624334
31624334

I have tried this:

select rel_CTE.DocumentUpId + rel_CTE.DocumentDownId as 'RelatedDocId' 
into #temprelations 
from RelationsCTE rel_CTE

But it gives me concatenate of values (well sum in this case), so is it possible?

0

3 Answers 3

9

UNION ALL

select rel_CTE.DocumentUpId  as 'RelatedDocId'  from RelationsCTE
union all
select rel_CTE.DocumentDownId from RelationsCTE
Sign up to request clarification or add additional context in comments.

Comments

4

Try like this,

SELECT DocumentUpId as DocumentUpId 
FROM RelationsCTE

UNION ALL

SELECT DocumentDownId as DocumentUpId 
FROM RelationsCTE

4 Comments

union will not give the correct results as it filters duplicate values.
don't really understand the down vote for something that's easily corrected within a reasonable time frame, what's the point? have a +1 from me
@Tanner: And what I want to know is why these easy questions/answers get so many votes (both up and down), and then those that answer difficult questions barely get any upvotes. I think I know the answer to that, but still...
@sstan that's the way of the world. the suggested duplicate was too late and not really a suitable candidate, but that would have been the way to go. it's not too late to close.
0

Try some thing different using CROSS -APPLY :).

SELECT [RelatedDocId]  
FROM RelationsCTE 
  CROSS APPLY (VALUES(DocumentUpId),
                     (DocumentDownId)) V( [RelatedDocId]) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.