Solved

Help with CrossTab Store Procedure in SQL2000

Posted on 2011-02-23
6
364 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Any benefit to adding a Clustered index here? 4 35
SQL Job Failed 6 27
TDE for SQL Web Edition 1 33
Sql server query 8 18
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

739 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