?
Solved

Help with CrossTab Store Procedure in SQL2000

Posted on 2011-02-23
6
Medium Priority
?
367 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 51

Accepted Solution

by:
Mark Wills earned 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

741 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