Question

This seems to be a problem I have quite frequently, but I have yet figure out (I'm not a dba). I'm running a query for a report I'm creating, however the user does not want any duplicate rows displayed for the same 'case number'.

I have tried both "select distinct" and "group by" but neither are showing the results how I would like them shown.

The problem is that the report queries for a column that can have 2 different possible values, and we want to show only cases that match either of those values, but we don't want to display a specific case more than 1 time.

Here is the query:

SELECT cases.casenum, 
       sp_first_party(cases.casenum), 
       cases.matcode, 
       cases.open_status, 
       cases.date_opened, 
       cases.close_date 
FROM cases, 
     insurance 
WHERE (cases.casenum = insurance.case_num) 
  AND  cases.date_opened >= :start 
  AND cases.date_opened <= :end 
  AND (    insurance.policy_type = 'Liability' 
        OR insurance.policy_type = 'SUM') 
  AND insurance.date_settled is Null  
  AND (    cases.matcode like 'GPI' 
        OR cases.matcode like 'MVA' 
        OR cases.matcode like 'S&F') 
  AND cases.close_date is not NULL  

This is what I believe to be the problem area:

(insurance.policy_type = 'Liability' OR insurance.policy_type = 'SUM')

A single "case"/"case number" can have multiple insurance policy types... meaning they can have either liability or SUM, or both. And the output of the report is showing a new row for each instance of either of these insurance types showing up.

We would like to show only a single row per case, regardless of how many/which insurance policy types are assigned to the case... but still only show cases that match that criteria.

I've tried select distinct, as well as group by cases.casenum, what am I doing wrong here?

Thank you for reading!

PS- this is for a cms platform/application, but it runs on t-sql (same syntax as mssql).

Here's an example of the output- as you can see on the leftmost column, there are multiple of the same values for "Case Number" column. This is because there are multiple different insurance.policy_type's for that case number that meet the criteria. While we still need to filter by those policy types, we don't want to see an extra row for each.

output

Était-ce utile?

La solution

You don't have to use MIN, just the ROW_NUMBER line will let you select one row per case number.

I couldn't test following query without the data model:

WITH Src AS 
(
SELECT cases.casenum, 
       sp_first_party(cases.casenum) as first_party, 
       cases.matcode, 
       cases.open_status, 
       cases.date_opened, 
       cases.close_date,
       ROW_NUMBER() OVER(PARTITION BY cases.casenum ORDER BY insurance.policy_type) as rank
FROM cases, 
     insurance 
WHERE (cases.casenum = insurance.case_num) 
  AND  cases.date_opened >= :start 
  AND cases.date_opened <= :end 
  AND (    insurance.policy_type = 'Liability' 
        OR insurance.policy_type = 'SUM') 
  AND insurance.date_settled is Null  
  AND (    cases.matcode like 'GPI' 
        OR cases.matcode like 'MVA' 
        OR cases.matcode like 'S&F') 
  AND cases.close_date is not NULL 
)
SELECT * FROM Src WHERE rank = 1
Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange
scroll top