Solved

Arrange column in SQL

Posted on 2015-01-25
5
124 Views
Last Modified: 2015-01-26
How can I return result as my sample below in sql?  There will always be only on mismatch, but one or multiple possible(s).

Sample data - my current result
clientid  poss_mismatch customername
1            mismatch            bank of a
1            possible               bofa
1            possible               b of a
3            mismatch            Wells
3            possible               Wells Fargo
3            possible               WellsFargo
etc


New Result
Clientid   Col_Mismatch         Col_Possible     Customername
1                bank of a
1                                                possible              bofa    
1                                                possible              b of a  
3               Wells
3                                                possible               Wells Fargo
3                                                possible               WellsFargo

etc
0
Comment
Question by:VBdotnet2005
  • 2
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
Use the PIVOT clause, e.g.

SELECT * FROM yourTable
PIVOT ( MIN(CustomerName) FOR poss_mismatch IN (possible, mismatch)) P;

Open in new window

0
 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
like this ?

although  your ordiginal order (of possibles) can't be guaranteed to be maintained...

select  clientid
   ,case when poss_mismatch='Mismatch' then customername
         else ' ' end as col_mismatch
   ,case when pos_mismatch='mismatch' then ' '
         else pos_mismatch end as col_possible
   ,case when pos_mismatch='mismatch' then ' '
         else customername end as Customername
from yourtable
order by 1,2 Desc,3,4

Open in new window

0
 

Author Comment

by:VBdotnet2005
Comment Utility
ste5an,
Is there a way to remove null from mistmatch and possible?
0
 

Author Comment

by:VBdotnet2005
Comment Utility
How can I remove null?

SELECT * FROM yourTable
PIVOT ( MIN(CustomerName) FOR poss_mismatch IN (possible, mismatch)) P;

Open in new window

0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Use COALESCE():

SELECT Clientid,
  Customername ,
  COALESCE(possible, ''),
  COALESCE(mismatch, '')
FROM yourTable
PIVOT ( MIN(CustomerName) FOR poss_mismatch IN (possible, mismatch)) P;

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now