Arrange column in SQL

Posted on 2015-01-25
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

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

Question by:VBdotnet2005
  • 2
  • 2
LVL 33

Accepted Solution

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

LVL 50

Expert Comment

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


Author Comment

ID: 40571099
Is there a way to remove null from mistmatch and possible?

Author Comment

ID: 40571100
How can I remove null?

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

Open in new window

LVL 33

Expert Comment

ID: 40571333

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

Open in new window


Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i use WITH CTE for checking exist value? 3 30
SQL: launch actions one before the other 10 22
SQL Server Designer 19 39
Create snapshot on MSSQL 2012 3 17
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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

813 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

12 Experts available now in Live!

Get 1:1 Help Now