Link to home
Start Free TrialLog in
Avatar of c0ry98
c0ry98

asked on

Dynamic Crosstab Query - Replicate Transform

Hi all,

I have a query that I need to convert from Access97 to TSQL.  

I've written a simple count/group by select to get the data to this state but I can't figure out the next step.

Here's where I am:
Location        cover           hits
140 ML           A                  21
140 ML           B                  14
140 ML           C                  19
181 ML           A                  45
181 ML           C                  10
181 ML           D                  45
....

And where I need to end up
cover           Total            140 ML            181 ML     ....(needs to grow dynamically)
A                  66                 21                   45
B                  14                 14
C                  29                 19                    10
D                  45                                         45

I've looked at the Pivot function however I was unable to figure out how to support n columns with custom names. This code will be in a stored procedure so dynamic tsql will work too.


ASKER CERTIFIED SOLUTION
Avatar of brejk
brejk
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of c0ry98
c0ry98

ASKER

Thanks for the response.. I working through the code as we speak.. So far it looks like what I need. If it works I'll accept the solution.
Avatar of c0ry98

ASKER

Hi breajk,

Excellent code.  Thanks
Avatar of c0ry98

ASKER

The code was right on.. I only found one type in the left(error_code) lines.  It should trim the last 2 characters instead of 1.
There is no type :-) LEN trims trailing blanks by default so you have to trim the comma only :-)