Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Column Name                     Type                               Copies
        
ELECTRICAL MACHINES -II		           Issue	                               48

ELECTRICAL MACHINES -II		         Reference	                                2  


how to convert it in to single row like


              Name                               Reference         Issue  

ELECTRICAL MACHINES -II                            2                       48
Posted
Updated 27-Feb-14 23:41pm
v2

1 solution

That what you need is called PIVOT[^] ;)

SQL
SELECT [Name], [Reference], [Issue]
FROM (
    SELECT [Name], [Type], [Copies] 
    FROM MyTable
) AS DT
PIVOT(MAX(Copies) FOR [Type] IN ([Reference], [Issue])) AS PT
 
Share this answer
 
Comments
Member 10517120 28-Feb-14 5:47am    
tnk u . let me check
Maciej Los 28-Feb-14 5:52am    
You're very welcome. If it was helpful, please mark this answer as a solution (green button).
Member 10517120 28-Feb-14 6:15am    
select BookTitle,Library_Department_ID,SectorID,sum(NoofCopies) from tbl_bookmst where Status= 1 group by BookTitle,Library_Department_ID,SectorID



this is wt my query is .



I'm having one more column(bit) named IsReference which is 1 for Referencebook

and 0 for Issuebook

now i need to Convert this row to column With the name reference and Issue

How i need to Write the query
Maciej Los 28-Feb-14 6:24am    
SELECT BookTitle, Library_Department_ID, SectorID, [Reference], [Issue]
FROM (
SELECT BookTitle, Library_Department_ID, SectorID, NoofCopies, CASE WHEN [Status] = 1 THEN 'Reference' ELSE 'Issue' END
FROM tbl_bookmst
WHERE Status= 1
) AS DT
PIVOT(SUM(NoofCopies) FOR [Status] IN([Reference], [Issue])) AS PT

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900