SELECT
count(*) AS total from (
SELECT
products.Validator,
policies.ProductID,
policies.PlanID,
policies.DateGen,
policies.PHName,
policies.ProductName,
policies.PolicyNumber,
policies.DateStart,
policies.CorpName,
policies.CorpID,
policies.TotalPremium,
policies.DateEnd,
policies.PolicyState,
policies.ID
FROM
policies
LEFT JOIN products ON products.ProductID = policies.ProductID
WHERE
policies.CorpID IN (
16899,
16900,
16902,
16915,
16918,
16949
)
AND PolicyNumber != ''
AND DateIssue >= '2004-05-01'
AND DateIssue <= '2019-05-21'
UNION ALL
SELECT
products.Validator,
policies.ProductID,
policies.PlanID,
policies.DateGen,
policies.PHName,
policies.ProductName,
policies.PolicyNumber,
policies.DateStart,
policies.CorpName,
policies.CorpID,
policies.TotalPremium,
policies.DateEnd,
policies.PolicyState,
policies.ID
FROM
policies
LEFT JOIN products ON products.ProductID = policies.ProductID
LEFT JOIN policy_correct ON policy_correct.PolicyID = policies.ID
WHERE
policies.CorpID IN (
16899,
16900,
16902,
16915,
16918,
16949
)
AND policies.PolicyNumber != ''
AND policy_correct.CreateTime >= '2004-05-01'
AND .policy_correct.CreateTime <= '2019-05-21'
)as temp;