SQL Rows into columns

this question may have been asked many times but I cant get my data work with the sample queries I found on different sites,

I have a challenge which I need to pivot my table /date,

my data looks like below;

 ID	Business 	                        Date	                                      Value
 1     	    GPS	                             Nov-18	                                        3
 2     	    GPS	                             Dec-18	                                        2
 3     	    GPS	                             Nov-18	                                        2
 4     	    GPS	                             Dec-18	                                        3

and my aim is to have a out put like below;

   ID	       Business 	                               Nov-18	      Dec-18
  1 	        GPS	                                          3	           2
  2	            GPS	                                          2	           3

I appreciates any help, if you let me know the syntax and way I should apply to query.


What type of database?

MySQL, SQL Server, other?

Microsoft SQL server management studio

I would normally argue that this is the responsibility of whatever you’re using to extract the data rather than of the database query itself; however, SQL Server has the PIVOT operator for this purpose…

Thanks, I only have above table ( only one table )
I have tried to mimic you steps which I believe is very good but I get stuck when the joins come to play and I don’t think I need those options, so a little lost how to do it

I am struggling to use it, based on my scenario

Sometimes it’s all about databases and how web app has been built. Some developers make web apps not from scratch, but from template, and that’s why it can be a cause of huge database issues.

Can anyone help please
below is DDL
,Bussiness VARCHAR(3) NOT NULL

INSERT INTO mytable( ID,Bussiness,Date, Value) VALUES
(1,‘GPS’,‘Nov-18’,3 )
,(2,‘GPS’,‘Dec-18’,2 )
,(3,‘GPS’,‘Nov-18’,2 )
,(4,‘GPS’,‘Dec-18’,3 );

i have tried your solution with a few changes

  1. I didnt need to join tables all information are in the same table
  2. i used max
    but I get this error
    Msg 488, Level 16, State 1, Line 35
    Pivot grouping columns must be comparable. The type of column “description” is “text”, which is not comparable.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.