I haven't done too many nested statements in MySQL and I was hoping to have the below SQL looked at and let me know if there is a better/more efficient way of doing what I am trying to accomplish.
Through all of my tests it appears that everything is working properly, I just want to make sure I am meeting best practices, etc.
SELECT DISTINCT
-- Fab A with EMC
-- Conditions: Anything with an EMC
(SELECT
count(`wo_num`)
FROM
`work_orders`
WHERE
`emc` = '1'
AND `auth_status` = 'ACTIVE') as `a_count_e`,
-- Fab A Without EMC
-- Conditions: NO EMC, Contract price greater than 10,000, NOT "Time and Material", NOT Service Order, NOT Contract Install
(SELECT
count(`wo_num`)
FROM
`work_orders`
WHERE
`emc` = '0'
AND replace(replace(`contract_price`, ',', ''), '$', '') >= 10000
AND `auth_status` = 'ACTIVE'
AND `contract_install` = '0'
AND `terms` <> 'TIME AND MATERIAL'
AND `wo_type` <> 'SERVICE ORDER') as `a_count`,
-- Fab B
-- Conditions: NO EMC, Contract price between 1500 and 9999, NOT "Time and Material", NOT Service Order, NOT Contract Install
(SELECT
count(`wo_num`)
FROM
`work_orders`
WHERE
`emc` = '0'
AND replace(replace(`contract_price`, ',', ''), '$', '') >= 1500
AND replace(replace(`contract_price`, ',', ''), '$', '') <= 9999
AND `auth_status` = 'ACTIVE'
AND `contract_install` = '0'
AND `terms` <> 'TIME AND MATERIAL'
AND `wo_type` <> 'SERVICE ORDER') as `b_count`,
-- Small
-- Conditions: NO EMC, Contract price between 600 and 1499, NOT "Time and Material", NOT Service Order, NOT Contract Install
(SELECT
count(`wo_num`)
FROM
`work_orders`
WHERE
`emc` = '0' AND `auth_status` = 'ACTIVE'
AND `wo_type` <> 'SERVICE ORDER'
AND `contract_install` = '0'
AND `terms` <> 'TIME AND MATERIAL'
AND replace(replace(`contract_price`, ',', ''), '$', '') >= 600
AND replace(replace(`contract_price`, ',', ''), '$', '') <= 1499) as `sm`,
-- XX
-- Conditions: NO EMC, Contract price less than 600, NOT "Time and Material", NOT Service Order, NOT Contract Install
(SELECT
count(`wo_num`)
FROM
`work_orders`
WHERE
`emc` = '0' AND `auth_status` = 'ACTIVE'
AND `wo_type` <> 'SERVICE ORDER'
AND `contract_install` = '0'
AND `terms` <> 'TIME AND MATERIAL'
AND replace(replace(`contract_price`, ',', ''), '$', '') <= 599) as `xx`,
-- TM
-- Conditions: NO EMC, NOT Service Order, NOT Contract Install
(SELECT
count(`wo_num`)
FROM
`work_orders`
WHERE
`emc` = '0'
AND `auth_status` = 'ACTIVE'
AND `contract_install` = '0'
AND `wo_type` <> 'SERVICE ORDER'
AND `terms` = 'TIME AND MATERIAL') as `tm`,
-- Contract Install
-- Conditions: NO EMC, IS Contract Install
(SELECT
count(`wo_num`)
FROM
`work_orders`
WHERE
`emc` = '0'
AND `auth_status` = 'ACTIVE'
AND `contract_install` = '1') as `ci`,
-- Service
-- All NO EMC, IS Service Order, NOT Contract Install
(SELECT
count(`wo_num`)
FROM
`work_orders`
WHERE
`emc` = '0'
AND `wo_type` = 'SERVICE ORDER'
AND `auth_status` = 'ACTIVE'
AND `contract_install` = '0') as `service`,
-- Total count
-- All Active orders
(SELECT
count(`wo_num`)
FROM
`work_orders`
WHERE
`auth_status` = 'ACTIVE') as `total`
FROM
`work_orders`
WHERE
`auth_status` = 'ACTIVE'
Results:
Side Note:
Forgive me for the
`replace(replace(`contract_price`, ',', ''), '$', '')`
When the application was originally written the contract prices were stored as dollar amounts with $
and ,
, so I have to remove them for numerical comparisons.