Solved

Arrange column in SQL

Posted on 2015-01-25
5
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40569320
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
ID: 40569798
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
ID: 40571099
ste5an,
Is there a way to remove null from mistmatch and possible?
0
 

Author Comment

by:VBdotnet2005
ID: 40571100
How can I remove null?

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

Open in new window

0
 
LVL 33

Expert Comment

by:ste5an
ID: 40571333
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

730 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