I am trying to use Case statement or IF ELSE statement within IFNULL function to specify in more details of options.
If I uncomment the IF statement where it starts with IF(t
.Type_id
IN (7777 , 9999), it would not cause error, but when I try to use CASE statement, it gives that error ("Select" is not valid at this position for this server version, expecting '(', with).
I am trying to use CASE statement to be more specific, but if there is any way to use IF ELSE statement to specify t
.Type_id
in (4444, 2222) then 'VIDEOVISITS - RETURN' and not causing this error, please let me know. Thanks in advance.
SELECT
IFNULL(IF(`t`.`NRP_classification` NOT IN (
'NEW',
'TELEPHONE'),
'OTHER NC',
IF(
`t`.`NRP_classification` = 'VIDEOVISITS',
//Trying to replace from here:
IF(`t`.`Type_id` IN (7777 , 9999),
'VIDEOVISITS - NEW',
'VIDEOVISITS - RETURN'
),
`t`.`NRP_classification`
//Trying to replace until here
/*
case `t`.`NRP_classification` = 'VIDEOVISITS'
when `t`.`Type_id` IN (7777 , 9999) then 'VIDEOVISITS - NEW'
when `t`.`Type_id` IN (4444, 2222) then 'VIDEOVISITS - RETURN'
end as `t`.`NRP_classification`
*/
)
),
'OTHER NC') AS `Type`
FROM
((((((((((((((SELECT
`DONI_db`.`appointment`.`Appt_id` AS `Appt_id`,
`DONI_db`.`appointment`.`CSN` AS `CSN`,
`DONI_db`.`appointment`.`N` AS `N`,
`DONI_db`.`appointment`.`Date` AS `Date`,
`DONI_db`.`appointment`.`Time` AS `Time`,
`DONI_db`.`appointment`.`Len` AS `Len`,
`DONI_db`.`appointment`.`Status` AS `Status`,
`DONI_db`.`appointment`.`Type` AS `Type`,
`DONI_db`.`appointment`.`LOS` AS `LOS`,
`DONI_db`.`appointment`.`Made_date` AS `Made_date`,
`DONI_db`.`appointment`.`Rescheduled` AS `Rescheduled`,
`DONI_db`.`appointment`.`SD_canceled` AS `SD_canceled`,
`DONI_db`.`appointment`.`Canc_date` AS `Canc_date`,
`DONI_db`.`appointment`.`Canc_reason` AS `Canc_reason`,
`DONI_db`.`appointment`.`CI_startTime` AS `CI_startTime`,
`DONI_db`.`appointment`.`CI_endTime` AS `CI_endTime`,
`DONI_db`.`appointment`.`VS_startTime` AS `VS_startTime`,
`DONI_db`.`appointment`.`AVS_time` AS `AVS_time`,
`DONI_db`.`appointment`.`Encounter_status` AS `Encounter_status`,
`DONI_db`.`appointment`.`EncClosure_provID` AS `EncClosure_provID`,
`DONI_db`.`appointment`.`Cosign_provID` AS `Cosign_provID`,
`DONI_db`.`appointment`.`PrimaryDx_ICD10` AS `PrimaryDx_ICD10`
FROM
`DONI_db`.`appointment`
WHERE
`DONI_db`.`appointment`.`Type` NOT IN ('2100' , '4493', '5044', '5045', '5183', '5225')
AND `DONI_db`.`appointment`.`Date` >= '2016-07-01')) `appt`
JOIN `DONI_db`.`type` `t`)
JOIN `DONI_db`.`appt_status` `as`)
LEFT JOIN `DONI_db`.`user` `ulos` ON (`appt`.`LOS_authProviderID` = `ulos`.`User_id`))
JOIN (SELECT
`DONI_db`.`appt_pds`.`APDS_apptID` AS `APDS_apptID`,
`DONI_db`.`appt_pds`.`APDS_provID` AS `APDS_provID`,
`DONI_db`.`appt_pds`.`APDS_deptID` AS `APDS_deptID`,
`DONI_db`.`appt_pds`.`APDS_specID` AS `APDS_specID`,
`DONI_db`.`appt_pds`.`APDS_posID` AS `APDS_posID`
FROM
`DONI_db`.`appt_pds`
WHERE
`DONI_db`.`appt_pds`.`APDS_type` = 'New') `apds_np`)
JOIN `DONI_db`.`department` `d`)
JOIN `DONI_db`.`specialty` `s`)
JOIN `DONI_db`.`user` `unp`)
LEFT JOIN (SELECT
`apds_op`.`APDS_apptID` AS `APDS_apptID`,
`apds_op`.`APDS_provID` AS `APDS_provID`,
`apds_op`.`APDS_newProv` AS `APDS_newProv`,
`uop`.`Lastname` AS `LastName`,
`uop`.`Name` AS `Name`
FROM
(((SELECT
`DONI_db`.`appt_pds`.`APDS_apptID` AS `APDS_apptID`,
`DONI_db`.`appt_pds`.`APDS_provID` AS `APDS_provID`,
`DONI_db`.`appt_pds`.`APDS_posID` AS `APDS_posID`,
`DONI_db`.`appt_pds`.`APDS_newProv` AS `APDS_newProv`
FROM
`DONI_db`.`appt_pds`
WHERE
`DONI_db`.`appt_pds`.`APDS_type` = 'Old')) `apds_op`
LEFT JOIN `DONI_db`.`user` `uop` ON (`apds_op`.`APDS_provID` = `uop`.`User_id`))
WHERE
`uop`.`Type` = 'R'
OR `uop`.`User_id` = 12
GROUP BY `apds_op`.`APDS_newProv` , `apds_op`.`APDS_apptID`
HAVING MAX(`apds_op`.`APDS_posID`)) `apds_op` ON (`apds_np`.`APDS_apptID` = `apds_op`.`APDS_apptID`
AND `apds_np`.`APDS_posID` = `apds_op`.`APDS_newProv`))
LEFT JOIN `DONI_db`.`clinic_dpp` `cdpp` ON ((`cdpp`.`CDPP_provID` IS NULL
OR IF(`apds_op`.`APDS_provID` IS NULL, `apds_np`.`APDS_provID`, `apds_op`.`APDS_provID`) = `cdpp`.`CDPP_provID`)
AND `d`.`Department_id` = `cdpp`.`CDPP_deptID`))
LEFT JOIN `DONI_db`.`clinic` `c` ON (`cdpp`.`CDPP_clinicID` = `c`.`Clinic_id`))
LEFT JOIN `DONI_db`.`program` `p` ON (`cdpp`.`CDPP_progID` = `p`.`Program_id`))
JOIN `DONI_db`.`enc_status` `es`)
WHERE
`appt`.`Type` = `t`.`Type_id`
AND `appt`.`Status` = `as`.`ApptStatus_id`
AND `appt`.`Appt_id` = `apds_np`.`APDS_apptID`
AND `apds_np`.`APDS_deptID` = `d`.`Department_id`
AND `apds_np`.`APDS_specID` = `s`.`Specialty_id`
AND `apds_np`.`APDS_provID` = `unp`.`User_id`
AND `appt`.`Encounter_status` = `es`.`ES_id`
AND `d`.`toExclude` = 0