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


Join Values from 2 columns


Join Values from 2 columns

Author
Message
davdam8
davdam8
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1244 Visits: 668
Hi geniuses,

I have a table which contains groups inside Receivers and Payers (2 columns).
Some groups can be Receivers and Payers, for instance , A and B.
B, D and E are only payers.

For the receivers we got A, B and C.
For the payers we have A, B, D and E.

Receivers \\ Payers
A \\ A
B \\ A
A \\ B
A \\ D
B \\ D
C \\ A
B \\ E
A \\ E

How do I create a column 'Group' that contains both Receivers and Payers, not repeated?
What i need:

Group
A
B
C
D
E

Thanks, Regards
Mark Cowne
Mark Cowne
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10160 Visits: 26054
SELECT Receivers FROM myTable
UNION
SELECT Payers FROM myTable



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Want a cool Sig
Want a cool Sig
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1594 Visits: 705
I think this is what you're looking for...
Providing easily consumable sample data makes it easier for people to help and more likely...


--Setup Sample Data
declare @transactions as table (Receivers char(1), Payers char(1))
insert into @transactions(Receivers, Payers)
values('A' , 'A')
,('B' , 'A')
,('A' , 'B')
,('A' , 'D')
,('B' , 'D')
,('C' , 'A')
,('B' , 'E')
,('A' , 'E')

--Actual work statement
select distinct [Group]
from (
select Receivers [Group]
from @transactions
union all
select Payers [Group]
from @transactions
) c



---------------------------------------------------------------
Mike Hahn - MCSomething someday:-)
Right way to ask for help!!
http://www.sqlservercentral.com/articles/Best+Practices/61537/
I post so I can see my avatar Hehe
I want a personal webpage Cool
I want to win the lotto :-D
I want a gf like Tiffa w00t Oh wait I'm married!:-D
davdam8
davdam8
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1244 Visits: 668
Thanks Very Much! Helped a lot.
Could you geniuses take a look at this topic:

http://www.sqlservercentral.com/Forums/Topic1409896-391-1.aspx

Thanks again.
Best regards
davdam8
davdam8
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1244 Visits: 668
It is possible to add a Group By inside each Select?
If yes how do you do it?
Thanks.
Orlando Colamatteo
Orlando Colamatteo
SSC Guru
SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)

Group: General Forum Members
Points: 69618 Visits: 14494
I suspect your definition of 'group by' might be different than mine. Using the sample data in your first post how would the resultset look if it did a "Group By inside each Select"?

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
dwain.c
dwain.c
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28511 Visits: 6431
Hers's another way.


declare @transactions as table (Receivers char(1), Payers char(1))
insert into @transactions(Receivers, Payers)
values('A' , 'A')
,('B' , 'A')
,('A' , 'B')
,('A' , 'D')
,('B' , 'D')
,('C' , 'A')
,('B' , 'E')
,('A' , 'E')

SELECT RecPyr
FROM @transactions
CROSS APPLY (VALUES (Receivers),(Payers)) a(RecPyr)
GROUP BY RecPyr




Now what did you want to do with a GROUP BY inside of the SELECT?


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
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