Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Help with CrossTab Store Procedure in SQL2000

Posted on 2011-02-23
6
360 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

808 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