Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Join Values from 2 columns Expand / Collapse
Author
Message
Posted Tuesday, January 22, 2013 6:16 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 24, 2013 4:56 AM
Points: 178, 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
Post #1409987
Posted Tuesday, January 22, 2013 6:31 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 08, 2014 6:13 AM
Points: 1,694, Visits: 19,550

SELECT Receivers FROM myTable
UNION
SELECT Payers FROM myTable



____________________________________________________

How to get the best help on a forum

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

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1409995
Posted Tuesday, January 22, 2013 6:55 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, April 14, 2014 2:35 PM
Points: 285, Visits: 503
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 - Future MCM 2025
Right way to ask for help!!
http://www.sqlservercentral.com/articles/Best+Practices/61537/
I post so I can see my avatar
I want a personal webpage
I want to win the lotto
I want a gf like Tiffa
Post #1410009
Posted Tuesday, January 22, 2013 7:35 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 24, 2013 4:56 AM
Points: 178, 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
Post #1410035
Posted Tuesday, January 22, 2013 8:26 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 24, 2013 4:56 AM
Points: 178, Visits: 668

It is possible to add a Group By inside each Select?
If yes how do you do it?
Thanks.
Post #1410063
Posted Saturday, January 26, 2013 7:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 9:25 AM
Points: 7,070, Visits: 12,523
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

Believe you can and you're halfway there. --Theodore Roosevelt

Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein

The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein

1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
Post #1412027
Posted Sunday, January 27, 2013 8:59 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:35 PM
Points: 3,593, Visits: 5,102
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!
Post #1412171
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse