Oracle SQL selecting the MAX of a returned Value
Current Query:
SELECT DISTINCT "HSP"."HSP_ACCOUNT_ID", "HSP"."HSP_ACCOUNT_NAME", "HSP"."ADM_DATE_TIME", "HSP"."DISCH_DATE_TIME",
"HSP"."TOT_CHGS", "HSP_ACCT_MULT_DRGS"."DRG_ID_TYPE_ID", "HSP_ACCT_MULT_DRGS"."DRG_MPI_CODE"
FROM "C"."HSP" "HSP" LEFT OUTER JOIN "C"."HSP_ACCT_MULT_DRGS" "HSP_ACCT_MULT_DRGS"
ON "HSP"."HSP_ACCOUNT_ID"="HSP_ACCT_MULT_DRGS"."HSP_ACCOUNT_ID"
WHERE ("HSP"."DISCH_DATE_TIME">=TO_DATE ('01-10-2009 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
AND "HSP"."DISCH_DATE_TIME"<TO_DATE ('01-10-2010 00:00:00', 'DD-MM-YYYY HH24:MI:SS'))
AND ( "HSP_ACCT_MULT_DRGS"."DRG_ID_TYPE_ID"=34 OR "HSP_ACCT_MULT_DRGS"."DRG_ID_TYPE_I开发者_JAVA技巧D"=10825
OR "HSP_ACCT_MULT_DRGS"."DRG_ID_TYPE_ID"=10826 OR "HSP_ACCT_MULT_DRGS"."DRG_ID_TYPE_ID"=10827)
ORDER BY "HSP"."HSP_ACCOUNT_ID"
Results:
HSP_ACCOUNT_ID HSP_ACCOUNT_NAME ADM_DATE_TIME DISCH_DATE_TIME TOT_CHGS DRG_ID_TYPE_ID DRG_MPI_CODE
4444 Betty Lou 06/01/09 10/01/09 $53,356 10827 444
4444 Betty Lou 06/02/09 10/01/09 $53,356 10826 444
5555 Big Bird 09/08/09 10/01/09 $50,203 10827 555
5555 Big Bird 09/09/09 10/01/09 $50,203 10826 555
6666 Hoots Owl 11/03/09 10/05/09 $28,693 10826 666
6666 Hoots Owl 11/04/09 10/05/09 $28,693 10827 666
7777 Rodeo Rosie 09/29/09 10/01/09 $32,201 10827 777
7777 Rodeo Rosie 09/30/09 10/01/09 $32,201 10826 777
8888 Oscar Gro 09/27/09 10/01/09 $33,600 10826 888
8888 Oscar Gro 09/28/09 10/01/09 $33,600 10827 888
I would consider all off these duplicates errors in my output. I really only want say the Max(DRG_ID_TYPE_ID) account to be displayed, but unsure how to change my SQL query. So a good output would look something like:
HSP_ACCOUNT_ID HSP_ACCOUNT_NAME ADM_DATE_TIME DISCH_DATE_TIME TOT_CHGS DRG_ID_TYPE_ID DRG_MPI_CODE
4444 Betty Lou 39965 40087.58611 53355.58 10827 444
5555 Big Bird 40064 40087.53125 50203.28 10827 555
6666 Hoots Owl 40121 40091.60694 28692.9 10827 666
7777 Rodeo Rosie 40085 40087.54236 32201.43 10827 777
8888 Oscar Gro 40084 40087.76319 33600.14 10827 888
I cannot specifically say where DRG_ID_TYPE_ID = 10827 because some accounts may have only 10826 which I would want to return.
add a MAX(DRG_ID_TYPE_ID) OVER (PARTITION BY HSP_ACCOUNT_ID)
SELECT DISTINCT
"HSP"."HSP_ACCOUNT_ID",
"HSP"."HSP_ACCOUNT_NAME",
"HSP"."ADM_DATE_TIME",
"HSP"."DISCH_DATE_TIME",
"HSP"."TOT_CHGS",
MAX("HSP_ACCT_MULT_DRGS"."DRG_ID_TYPE_ID") OVER (PARTITION BY "HSP"."HSP_ACCOUNT_ID") ,
"HSP_ACCT_MULT_DRGS"."DRG_MPI_CODE"
FROM "C"."HSP" "HSP" LEFT OUTER JOIN "C"."HSP_ACCT_MULT_DRGS" "HSP_ACCT_MULT_DRGS"
ON "HSP"."HSP_ACCOUNT_ID"="HSP_ACCT_MULT_DRGS"."HSP_ACCOUNT_ID"
WHERE ("HSP"."DISCH_DATE_TIME">=TO_DATE ('01-10-2009 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
AND "HSP"."DISCH_DATE_TIME"<TO_DATE ('01-10-2010 00:00:00', 'DD-MM-YYYY HH24:MI:SS'))
AND ( "HSP_ACCT_MULT_DRGS"."DRG_ID_TYPE_ID"=34 OR "HSP_ACCT_MULT_DRGS"."DRG_ID_TYPE_ID"=10825
OR "HSP_ACCT_MULT_DRGS"."DRG_ID_TYPE_ID"=10826 OR "HSP_ACCT_MULT_DRGS"."DRG_ID_TYPE_ID"=10827)
ORDER BY "HSP"."HSP_ACCOUNT_ID"
精彩评论