Solved

Help with CrossTab Store Procedure in SQL2000

Posted on 2011-02-23
6
356 Views
Last Modified: 2012-05-11
I am trying to create a crosstab using the following reference: http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspx but I don't quite understand how to call the store procedure...

This is my query.....
SELECT TOP 100 PERCENT dbo.VW_HPPATMFL.ccGrpName, dbo.VW_HPPATMFL.ccYRMO, dbo.VW_MasterDoctorPt.PDSPCL,
               dbo.VW_MasterDoctorPt.PDNAME AS DoctorName, COUNT(*) AS TotalCount
FROM  dbo.VW_HPPATMFL INNER JOIN
               dbo.VW_MasterDoctorPt ON dbo.VW_HPPATMFL.PPGRPno = dbo.VW_MasterDoctorPt.PJGRP# AND
               dbo.VW_HPPATMFL.PPPATno = dbo.VW_MasterDoctorPt.PJPAT#
WHERE (dbo.VW_HPPATMFL.PPTYPE = 'IP') AND (dbo.VW_HPPATMFL.PPMTCD <> 'D') AND (dbo.VW_MasterDoctorPt.PJAFTP = '2') AND
               (dbo.VW_HPPATMFL.ccAdmitDT BETWEEN CONVERT(DATETIME, '2009-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2009-12-31 00:00:00', 102))
GROUP BY dbo.VW_HPPATMFL.ccGrpName, dbo.VW_HPPATMFL.ccYRMO, dbo.VW_MasterDoctorPt.PDSPCL, dbo.VW_MasterDoctorPt.PDNAME
HAVING (dbo.VW_MasterDoctorPt.PDSPCL <> 'nur') AND (dbo.VW_HPPATMFL.ccGrpName = 'ELA')

This is my output... OUTPUT DATA
I want to be able to group by PDSPCL and DoctorName and crosstab total by ccYRMO....
0
Comment
Question by:epicazo
  • 3
  • 3
6 Comments
 

Author Comment

by:epicazo
ID: 34964306
This is my StoreProcedure....

CREATE procedure [dbo].[USP_CrossTab] (@Select varchar(1000),
    @PivotCol varchar(100),
    @Summaries varchar(100),
    @GroupBy varchar(100),
    @OtherCols varchar(100) = Null)
AS
set nocount on
set ansi_warnings off

declare @sql varchar(8000)
declare @Vals varchar(8000);
set @Vals = '';
set @OtherCols= isNull(', ' + @OtherCols,'')
create table #temp  (PV varchar(100))
set @sql = 'select distinct convert(varchar(100),' + @PivotCol + ') as PV FROM (' + @Select + ') A'

insert into #temp
exec (@sql)
select @Vals = @Vals + ', ' +
    replace(replace(@Summaries,'(','(CASE WHEN ' + @PivotCol + '=''' +
            PV +  ''' THEN '),')[', ' END) as [' + PV + '')
from #Temp
order by PV
drop table #Temp

set @sql = 'select ' + @GroupBy + @OtherCols + @Vals +
       ' from (' + @Select + ') A GROUP BY ' + @GroupBy
exec ( @sql )

set nocount off
set ansi_warnings on
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 34968770
Well considering I have been writing these for longer than Jeff, then I'll give it a go. Gimme an hour or two and I will be back.

In the meantime, what is happening - what results (or errors) are you getting ?
0
 

Author Comment

by:epicazo
ID: 34971593
I may have the columns in wrong place, but I try....  

exec USP_CrossTab
'SELECT ccYRMO, PDSPCL, PDNAME FROM dbo.VW_HPPATMFL INNER JOIN
dbo.VW_MasterDoctorPt ON dbo.VW_HPPATMFL.PPGRPno = dbo.VW_MasterDoctorPt.PJGRP# AND
dbo.VW_HPPATMFL.PPPATno = dbo.VW_MasterDoctorPt.PJPAT#',
'ccYRMO',
'COUNT(1 ELSE 0)[]',
'PDNAME',
'Count(*) as TotalCount'

Error:
Msg 207, Level 16, State 3, Line 1
Invalid column name 'PDNAME'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'ccYRMO'.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
ID: 34973170
OK, just a couple of bits to play with...

First use your original query - to use single quotes inside a set of single quotes, then any preexisting single quote you want inside has to become two single quotes.

Then, have to get the columns correct. While you dont need to group by in the SQL (because the pivot will do that) you do need some kind of counter to aggregate.

So, for you original query we removed the bits you dont need, added in a cntr and use table alias (so you dont have to repeat the entire table (view) name everytime, just use the alias instead). We also changed the "having" into part of the where clause.

And also get the arguments / parameters around the correct way / sequence.

exec USP_CrossTab '
SELECT HPP.ccGrpName, HPP.ccYRMO, MDP.PDSPCL, MDP.PDNAME AS DoctorName, 1 as cntr
FROM  dbo.VW_HPPATMFL HPP
INNER JOIN dbo.VW_MasterDoctorPt MDP ON HPP.PPGRPno = MDP.PJGRP# AND HPP.PPPATno = MDP.PJPAT#
WHERE (HPP.PPTYPE = ''IP'') 
AND (HPP.PPMTCD <> ''D'') 
AND (HPP.PJAFTP = ''2'') 
AND (HPP.ccAdmitDT BETWEEN CONVERT(DATETIME, ''2009-01-01 00:00:00'', 102) AND CONVERT(DATETIME, ''2009-12-31 00:00:00'', 102))
AND (MDP.PDSPCL <> ''nur'') 
AND (HPP.ccGrpName = ''ELA'')
',
'ccYRmo',
'count(cntr)[]',
'PDSPCL,DoctorName'

Open in new window


For another variation on the same Pivot theme you can look at : http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/A_4256-Dynamic-Pivot-Procedure-without-the-Pivot-function.html  it has a fw more explanations in there that might help - same basic routine - parameters in a different sequence, but might help.
0
 

Author Closing Comment

by:epicazo
ID: 34975120
You are awesome...
how do I sort by Jan..Dec?


exec USP_CrossTab '
SELECT HPP.ccGrpName, HPP.ccYRMM, MDP.PDSPCL, MDP.PDNAME AS DoctorName, 1 as cntr
FROM  dbo.VW_HPPATMFL HPP
INNER JOIN dbo.VW_MasterDoctorPt MDP ON HPP.PPGRPno = MDP.PJGRP# AND HPP.PPPATno = MDP.PJPAT#
WHERE (HPP.PPTYPE = ''IP'')
AND (HPP.PPMTCD <> ''D'')
AND (mdp.PJAFTP = ''2'')
AND (HPP.ccAdmitDT BETWEEN CONVERT(DATETIME, ''2009-01-01 00:00:00'', 102) AND CONVERT(DATETIME, ''2010-12-31 00:00:00'', 102))
AND (MDP.PDSPCL <> ''nur'')
AND (HPP.ccGrpName = ''ELA'')
',
'ccYRMM',
'count(cntr)[]',
'PDSPCL,DoctorName'
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 34975215
Sorting... Thats where you might want to check my routine - it can handle date sorting, but, you will need to cast ccYRMM as a date (simply append '-01') and then use the formatting options.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now