Help getting correct SQL syntax for a "crosstab query" report in SQL Server

britpopfan74
britpopfan74 used Ask the Experts™
on
Hello,

The purpose of the report I'm trying to create is to find out if a member is being brought
back to a clinic several times (separate DOS) to have multiple procedures performed.

Each of the 6 procedures (LINE_CODE) listed in my basic code below HAS THE POSSIBILITY OF BEING DONE ON BOTH THE RT AND LT SIDES OF THE BODY, so essentially, I'd like to show every possible combination (12 procedures) for any given member, even if he isn't having all performed.

I think to show this data most effectively to the provider will be to show a format similar to as follows:


MEMBER      DOS      PROC1_RT DONE  PROC1_LT NOT DONE (checks) ...PROC12_LT... PAID AMT/DOS
 A      1/5/11       1               0                            0            $1000
 A      1/12/11      0               1                            0            $500
 A      1/12/11           1               0                            0                          $200


Then later I can do summary statistics on the counts per member (make sure procedures aren't being duplicated unnecessarily) and average costs per procedure...

Anyhow, any advice is much appreciated; thank you


Current coding - gets all members having these procedures:

SELECT DISTINCT MC.MEMBER
, MC.FIRST_DOS
, CASE WHEN MCD.LINE_CODE IN('36478','36479') THEN 'LASER'
         WHEN MCD.LINE_CODE IN('36470','36471') THEN 'UGS'
       WHEN MCD.LINE_CODE IN('36011','37204') THEN 'TCC'
      END AS TX
, MCD.MOD1
, MCD.MOD2
, MC.TOTAL_PAY_AMT
INTO #MC_STATUS
FROM #temp_CLAIMS_MC MC
INNER JOIN MASTER_CLAIM#DETAIL mcd ON mc.[DOCUMENT] = mcd.[DOCUMENT]
WHERE (MCD.LINE_CODE IN('36478','36479','36470','36471','36011','37204') )
AND
((MCD.MOD1 IN('RT','LT'))
OR (MCD.MOD2 IN('RT','LT')))
ORDER BY MC.MEMBER, MCD.LINE_CODE, MCD.MOD1, MCD.MOD2, FIRST_DOS

--counting the distinct procedures:

SELECT MEMBER
, FIRST_DOS
, COUNT(DISTINCT PROCD) AS COUNT_DISTINCT_PROCS
FROM #MC_STATUS
GROUP BY MEMBER
ORDER BY COUNT(DISTINCT PROCD) DESC
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
In SQL Server 2005 and above you can use PIVOT. Here is an example how to do this dynamically:
http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx

Author

Commented:
Thank you for this link...I'm trying to figure out how to structure what I have into the PIVOT structure and if it could be dynamic?

In the meantime, I created a temp table to concatenate the LINE_CODE with the modifiers to at least get all the columns together.

At least it helps figure that using a source of real data below, member A had the same procedure billed 3x in the clinic on one date of service, 11/8/2011.

For ex.:

MEMBER      PROC_CAT      PROC_COUNT      FIRST_DOS
A         36478-RT      3                 11/8/2011
A         36470-RT      2                 12/27/2011
A         36478-RT      1                  7/13/2011
Top Expert 2011
Commented:
In order to have pivoted data like this:
MEMBER      DOS      PROC1_RT DONE  PROC1_LT NOT DONE (checks) ...PROC12_LT... PAID AMT/DOS
 A      1/5/11       1               0                            0            $1000
 A      1/12/11      0               1                            0            $500
 A      1/12/11           1               0                            0                          $200


you need to prepare flat data like this:
 MEMBER      DOS        Procedure            Value
 A                 1/5/11       PROC1_RT DONE          1
 A                 1/12/11      PROC1_LT NOT DONE      1
 A                 1/12/11      PROC12_LT... PAID      1
 A                 1/12/11      PAID AMT/DOS           $1000

You may need also to join Member and Dos columns

Author

Commented:
Thanks...helped me get to what I need with:

SELECT MEMBER
, FIRST_DOS
,   SUM(CASE WHEN PROC_CAT = '36478-RT' THEN 1 ELSE 0 END) As LASER_36478_RT --2 KINDS OF LASER
,   SUM(CASE WHEN PROC_CAT = '36478-LT' THEN 1 ELSE 0 END) As LASER_36478_LT
,   SUM(CASE WHEN PROC_CAT = '36479-RT' THEN 1 ELSE 0 END) As LASER_36479_RT
,      SUM(CASE WHEN PROC_CAT = '36479-LT' THEN 1 ELSE 0 END) As LASER_36479_LT
,      SUM(CASE WHEN PROC_CAT = '36470-RT' THEN 1 ELSE 0 END) As UGS_36470_RT  --2 KINDS OF UGS
,      SUM(CASE WHEN PROC_CAT = '36470-LT' THEN 1 ELSE 0 END) As UGS_36470_LT  
,      SUM(CASE WHEN PROC_CAT = '36471-RT' THEN 1 ELSE 0 END) As UGS_36471_RT  
,      SUM(CASE WHEN PROC_CAT = '36471-LT' THEN 1 ELSE 0 END) As UGS_36471_LT  
,      SUM(CASE WHEN PROC_CAT = '36011-RT' THEN 1 ELSE 0 END) As TCC_36011_RT  --2 KINDS OF TCC
,      SUM(CASE WHEN PROC_CAT = '36011-LT' THEN 1 ELSE 0 END) As TCC_36011_LT  
,      SUM(CASE WHEN PROC_CAT = '37204-RT' THEN 1 ELSE 0 END) As TCC_37204_RT  
,      SUM(CASE WHEN PROC_CAT = '37204-LT' THEN 1 ELSE 0 END) As TCC_37204_LT  
FROM #MC_STATUS
GROUP BY MEMBER, FIRST_DOS
ORDER BY MEMBER, FIRST_DOS DESC

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial