Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 "I'll Just Have One of Each, Please...."

Author  Topic 

Xerxes
Aged Yak Warrior

666 Posts

Posted - 2005-08-12 : 16:26:58
Hello again.....gotta situation where I have multiple entries and I only want one of each, but if one is of a certain group---that's the one I want in particular. Sound confusing? Here's my input:


MEMNUM PAYOR MEMNAME Doctor DIAG1 DIAG2
444 SSS ZZYGOTE, HAZEL K FRANKENSTEIN 332.0
444 XPLAN ZZYGOTE, HAZEL K JEKYLL 493.90
999 XPLAN ZZYJILLIC, IGUANA T JEKYLL 493.90
555 SSS ZZYLCHO, ZIPPY JEKYLL 493.90
555 XPLAN ZZYLCHO, ZIPPY JEKYLL 493.90
555 XPLAN ZZYLCHO, ZIPPY JEKYLL 493.90
555 XPLAN ZZYLCHO, ZIPPY FRANKENSTEIN 493.90
22 XPLAN ZZYPPURR, FUMBLER DETROIT 493.90
888 XPLAN ZZYTHLEFART, HORKCHOP DETROIT 493.90
999 XPLAN ZZYUZOON, FUJIYAMA FRANKENSTEIN 493.90
999 SSS ZZYUZOON, FUJIYAMA FRANKENSTEIN 414.00
999 SSS ZZYUZOON, YOMAMA HEMATOMA 424.1
33 SSS ZZYZZ, SPLEEN W. MCCOY 424.1
33 SSS ZZYZZ, SPLEEN W. MCCOY 428.0
33 SSC ZZYZZ, SPLEEN W. MCCOY 428.0 250.00
33 SSC ZZYZZ, SPLEEN W. MCCOY 424.1
33 SSC ZZYZZ, SPLEEN W. MCCOY 424.1


Now, I want to extract from this list one of each patient. But if a patient has a PAYOR of SSS---I prefer that one to any other payor for that patient. Also, if the patient had multiple maladies, I'd rather have that one.

Even multiple doctors means nothing.....now how do I get only one of each patient? I thought a GROUP BY clause would do it, but that's a dead end....

Semper fi,
XERXES, USMC(Ret.)
------------------------------------------------------
The Marine Corps taught me everything but SQL!

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2005-08-12 : 17:34:34
So in the case of the patient 'ZZYZZ, SPLEEN W. MCCOY', what output do you want? Do you want:

33 SSC ZZYZZ, SPLEEN W. MCCOY 428.0 250.00

because of the multiple maladies, or do you want:

33 SSS ZZYZZ, SPLEEN W. MCCOY 428.0

because of the 'SSS' payor, or do you want:

33 SSS ZZYZZ, SPLEEN W. MCCOY 428.0 250.00

because you want both the 'SSS' payor and the multiple maladies?

Go to Top of Page

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2005-08-12 : 17:37:09
BTW, I think you should at least win some sort of prize for having the most entertaining sample data in recent memory (I especially liked Doctor "Detroit", Doctor "Hematoma", and 'Fumbler Zzyppurr'!).
Go to Top of Page

Xerxes
Aged Yak Warrior

666 Posts

Posted - 2005-08-12 : 17:39:26
quote:
Originally posted by nosepicker

So in the case of the patient 'ZZYZZ, SPLEEN W. MCCOY', what output do you want? Do you want:

33 SSC ZZYZZ, SPLEEN W. MCCOY 428.0 250.00

because of the multiple maladies, or do you want:

33 SSS ZZYZZ, SPLEEN W. MCCOY 428.0

because of the 'SSS' payor, or do you want:

33 SSS ZZYZZ, SPLEEN W. MCCOY 428.0 250.00

because you want both the 'SSS' payor and the multiple maladies?





Yep===> 33 SSS ZZYZZ, SPLEEN W. MCCOY 428.0 250.00

because you want both the 'SSS' payor and the multiple maladies?

Semper fi,
XERXES, USMC(Ret.)
------------------------------------------------------
The Marine Corps taught me everything but SQL!
Go to Top of Page

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2005-08-12 : 18:23:22
There may be a simpler way to do this, but this seems to work:


SELECT C.memnum, C.payor, C.memname, B.MaxDoctor, B.MaxDiag1, B.MaxDiag2
FROM
(SELECT A.memnum, CASE A.payor WHEN 1 THEN 'SSS' WHEN 2 THEN 'SSC' WHEN 3 THEN 'XPLAN' END as payor, A.memname
FROM (SELECT
memnum,
MIN(CASE payor WHEN 'SSS' THEN 1 WHEN 'SSC' THEN 2 WHEN 'XPLAN' THEN 3 END) as payor,
memname
FROM #patient
GROUP BY memnum, memname) AS A
) AS C

JOIN

(SELECT
memname,
MAX(doctor) as MaxDoctor,
MAX(diag1) as MaxDiag1,
MAX(CASE WHEN diag2 IS NULL THEN 0 ELSE diag2 END) as MaxDiag2
FROM #patient
GROUP BY memname) AS B

ON C.memname = B.memname
Go to Top of Page

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2005-08-12 : 18:45:44
I was thinking that it might be better to join on the memnum instead of the memname:


SELECT C.memnum, C.payor, C.memname, B.MaxDoctor, B.MaxDiag1, B.MaxDiag2
FROM
(SELECT A.memnum, CASE A.payor WHEN 1 THEN 'SSS' WHEN 2 THEN 'SSC' WHEN 3 THEN 'XPLAN' END as payor, A.memname
FROM (SELECT
memnum,
MIN(CASE payor WHEN 'SSS' THEN 1 WHEN 'SSC' THEN 2 WHEN 'XPLAN' THEN 3 END) as payor,
memname
FROM #patient
GROUP BY memnum, memname) AS A
) AS C

JOIN

(SELECT
memnum,
MAX(doctor) as MaxDoctor,
MAX(diag1) as MaxDiag1,
MAX(CASE WHEN diag2 IS NULL THEN 0 ELSE diag2 END) as MaxDiag2
FROM #patient
GROUP BY memnum) AS B

ON C.memnum= B.memnum
Go to Top of Page

Xerxes
Aged Yak Warrior

666 Posts

Posted - 2005-08-12 : 18:48:33
Thanks........and you're welcome to change your monicker to any of those fictitious patients I listed....those names might be more "palatable"---get it?

Thanks again!

Semper fi,
XERXES, USMC(Ret.)
------------------------------------------------------
The Marine Corps taught me everything but SQL!
Go to Top of Page

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2005-08-12 : 19:23:37
LOL - nice one!
Go to Top of Page
   

- Advertisement -