Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

i write this Select statement to select data from table to view this data as a table

SQL
select Number,convert(varchar,datein, 111) as [Date],Operator as [Operator],
	(case id when '6' then substring (SMS,2,len(SMS)) else N'' end) [6],
	(case id when '7' then substring (SMS,2,len(SMS)) else N'' end) [7],
	(case id when '8' then substring (SMS,2,len(SMS)) else N'' end) [8],
	(case id when '9' then substring (SMS,2,len(SMS)) else N'' end) [9]
	from [Table-Name] where  Number= 95239 and id in ('6','7','8','9') 
  and datein between @StartDate AND @EndDate
  order by Number,datein


but its give me this :
Number    Date	        Operator	6	7	8	9
2345	5/8/2013	X               ABC 			
2345	5/8/2013	X                       CSA		
2345	5/8/2013	X                             SSSS 	
2345	5/9/2013	X                                      SAQ 


I want to show all this rows in one row to be like this :
Number    Date	        Operator	6	7	8	9
2345	5/8/2013	X               ABC    CSA     SSSS 	SAQ 


Thanks in advance :)
Posted
Updated 21-May-13 10:08am
v2

1 solution

Did you hear about pivots[^]?

In your case i would suggest you to use MAX() as aggregate function, for example:
SQL
SELECT Number,convert(varchar,datein, 111) as [Date],Operator as [Operator], [6],[7],[8],[9]
FROM (
     SELECT * 
     FROM [Table-Name]
     WHERE Number= 95239 and id in ('6','7','8','9') AND datein between @StartDate AND @EndDate) AS DT
PIVOT(MAX([SMS]) FOR [id] IN ( [6],[7],[8],[9])) AS PT
 
Share this answer
 
v2
Comments
eman88 21-May-13 16:41pm    
Thank You for your Answer :)
Maciej Los 21-May-13 16:44pm    
You're welcome ;)

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