Solved

SQL 2005 Stored Procedure COUNT

Posted on 2011-09-22
2
206 Views
Last Modified: 2012-06-21
I have the attached query that produces a result set similar to the example below. The issue I am having is with the Cases Count. Cases come from the Claim Field which look like this (1-06-0043B, 1-06-0043E, 1-05-0022A, 1-06-0029) What I am trying to accomplish is to Drop the tenth Character like so (1-06-0043, 1-06-0043, 1-05-0022, 1-06-0029) and when a claim number is the same only count it once. So for the example below the Cases Count for Involuntary Dismissal would be 3 since (1-06-0043) displays twice and the insured Count would remain the same. The reason for this is because I have 4 records for Involuntary Dismissal but two of them are attached to the same claim.

Type                                      Indemnity Paid      Expense Paid      Cases      Insureds
Involuntary Dismissal      0.00                      131306.23      4      4
Judgment for Defendant      0.00                      390134.69      5      5

USE [EXPORTpahpixPROD]
GO
/****** Object:  StoredProcedure [dbo].[PXQ7070A]    Script Date: 09/22/2011 07:53:59 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
/* ***************************************************************************************************/
/*  		                       Closed Claims Report    	                                     */
/* ***************************************************************************************************/
 
ALTER PROCEDURE [dbo].[PXQ7070A]
    (
     @FromDate DATETIME
    ,@ToDate DATETIME
    ,@GblOptState VARCHAR(4)
    )
AS 
    SET NOCOUNT ON
    DECLARE @GetMonth VARCHAR(2)
    DECLARE @GetYear VARCHAR(4)

    SET @GetMonth = MONTH(GETDATE())
    SET @GetYear = YEAR(GETDATE())

    SELECT [Type]
           ,SUM([Indem Paid]) AS [Indemnity Paid]
           ,SUM([Exp Paid]) AS [Expense Paid]
           ,COUNT(Claim) AS [Cases]
           ,COUNT(Insured) AS Insureds
    FROM    ( SELECT DISTINCT
                        CASE WHEN CSC.DispositionCode = '5'
                             THEN 'Judgment for Plaintiff'
                             WHEN CSC.DispositionCode = '6'
                             THEN 'Judgment for Defendant'
                             WHEN CSC.DispositionCode = ' '
                                  AND AppealFlag = 'Y' THEN 'Pending Appeal'
                             WHEN CSC.DispositionCode = '11'
                             THEN 'For Plaintiff After Appeal'
                             WHEN CSC.DispositionCode = '12'
                             THEN 'For Defendant After Appeal'
                             WHEN CSC.DispositionCode = '13'
                             THEN 'Award for Plaintiff'
                             WHEN CSC.DispositionCode = '14'
                             THEN 'Award for Defense'
                             WHEN CSC.DispositionCode = '15' THEN 'Mediation'
                             WHEN CSC.DispositionCode = '18'
                             THEN 'Early Offer Claims'
                             WHEN CSC.DispositionCode = '4'
                             THEN 'Involuntary Dismissal'
                             WHEN CSC.DispositionCode = '3'
                                  AND [Indem Paid] = '0.00'
                             THEN 'Voluntary Dismissal'
                             WHEN CSC.DispositionCode = '3'
                                  AND [Indem Paid] > '0.00' THEN 'Settlements'
                             WHEN CSC.DispositionCode = 'E'
                                  AND [Indem Paid] > '0.00'
                             THEN 'Incident Only Payment'
                        END AS [Type]
                       ,[Indem Paid]
                       ,[Exp Paid]
                       ,LEFT(Claim, 9) AS Claim
                       ,Insured
              FROM      EXPORTpahpixPROD.dbo.PXQ7000_UDF(@GetMonth, @GetYear,
                                                         NULL, 0, 0, 0, 1, 0,
                                                         @GblOptState)
                        INNER JOIN wellandp.ClaimSubclaim CSC ON dbo.PXQ7000_UDF.Claim = CSC.ClaimNo
              WHERE     [Disposition Date] BETWEEN @FromDate AND @ToDate
                        AND ( CSC.DispositionCode IN ( '4', '5', '6', '11',
                                                       '12', '13', '14', '15',
                                                       '18' )
                              OR ( CSC.DispositionCode = '3'
                                   AND [Indem Paid] >= '0.00'
                                 )
                              OR ( CSC.DispositionCode = 'E'
                                   AND AppealFlag = 'Y'
                                 )
                            )
                        AND ( CurrentFlag = 'Y' )
            ) tmp
    GROUP BY [Type]
    ORDER BY [Type]

Open in new window

0
Comment
Question by:mburk1968
2 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 36582093
Try this:

,COUNT( DISTINCT Claim) AS [Cases]
0
 

Author Closing Comment

by:mburk1968
ID: 36582118
That's it! Thank You!
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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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