Question : How Do I  create pivot query in t-sql for I-Anywhere

I would liek to create a result set like the one below from Access but need to do this in SQL for I -anywhere sybase.
Given table ( Top row column headers)
aType(varchar)      aDate(Short datetime)      Cat ( Varchar)
A      1/1/2008      IND
B      2/1/2008      ARI
c      3/1/2008      ATL
A      4/1/2008      BAL
B      5/1/2008      BUF
c      6/1/2008      CAR
A      7/1/2008      CHI
B      8/1/2008      CIN
c      9/1/2008      CLE
A      10/1/2008      DAL
B      11/1/2008      DEN
c      12/1/2008      DET
A      1/1/2009      GB
B      2/1/2009      HOU
c      3/1/2009      IND
A      4/1/2009      JAX
B      5/1/2009      KC
c      6/1/2009      MIA
A      7/1/2009      MIN
B      8/1/2009      NE
c      9/1/2009      NO
A      10/1/2009      NYG
B      11/1/2009      NYJ
c      12/1/2009      OAK
A      1/1/2010      PHI
B      2/1/2010      PIT
c      3/1/2010      SD
A      4/1/2010      SEA
B      5/1/2010      SF
c      6/1/2010      STL
A      7/1/2010      TB
B      8/1/2010      TEN
c      9/1/2010      WAS

I would like to roll up the data as a pivot query with the results  below( From MS Access  crosstab query)
aType      TotalOfcat      2008      2009      2010
A      11      4      4      3
B      11      4      4      3
c      11      4      4      3
sql from Access

TRANSFORM Count(Table1.cat) AS CountOfcat
SELECT Table1.aType, Count(Table1.cat) AS [Total Of cat]
FROM Table1
GROUP BY Table1.aType
PIVOT Format([aDate],"yyyy");

Can this be accomplished in Sybase?

Answer : How Do I  create pivot query in t-sql for I-Anywhere

The "pivot" operation is not relational.  It is available with Access by way of the Jet Engine and some code essentially lifted from Excel.  Real relational databases do not support Pivot.

Your problem is that you cannot specify the column aliases dynamically even though you have managed to generalize the rest of the query quite nicely.  Bravo!!

You pretty much have to use Dynamic SQL to get that to work.  The good news is that it is not that tough to do.  You declare a string, e.g. @CMDSTR, and start assigning the text of the SQL statement to it, concatenating each piece as you go.  In this instance, the only variable portion of the query are the column aliases, which you can get with your datepart(dateadd()) sequence.  After you build up the @CMDSTR, you use the Execute Immediate function, execute (@CMDSTR), and out will pop the results.

BTW, variables, e.g. @bdate, have no scope in dynamic SQL so your best bet is to put the entire BEGIN/END SQL block in the @CMDSTR.  That way you only need to substitute for @bdate in one place.

Also, you might want to stuff this whole thing into a stored procedure.  This is not the kind of code you want to try and develop, test, tune, and maintain in the context of an application.  If your application can only issue a query against a table, I will show you a trick for getting around that too.

It usually takes a few tries to get the SQL built correctly so make sure you do a SELECT @CMDSTR immediately before the execute so you have half a chance of debugging it.  When it works, you can comment that line out.

BTW, make sure your datepart specifications are consistent.  You have 'Year' in some and 'YYYY' in others.

Regards,
Bill
Random Solutions  
 
programming4us programming4us