* Oops, required field missing *
SELECT DISTINCT `l`.*, IF(dold.ts_added IS NULL, l.ts_added, dold.ts_added) AS `time`, `l`.`id` AS `leadID`, livebuyers.fn_leads_transactions_totalused(l.id, 1) AS `numTimesSold`, `loc`.`locationid`, `dist`.`distance`, `dist`.`DistSort`, `crh`.`credithistory`, `la`.`loanamount`, `dlam`.`minval` AS `loanamount_manual_min`, `dlam`.`maxval` AS `loanamount_manual_max`, `own`.`ownership`, IFNULL(dmxs.maxshares,4) AS `maxshares`, IF(xlf9.leadflagid IS NULL, 0, 1) AS `isBogusFlag`, `lstattype`.`vis_allusers_searchresults`, `lstattype`.`vis_shareowner_searchresults`, `drprem`.`household_income` AS `rapleaf_household_income`, `drprem`.`home_owner_status` AS `rapleaf_home_owner_status`, `dold`.`ts_added` AS `ts_added_et`, `sc`.`qualityscore`, `s`.`latitude`, `s`.`longitude`, `z`.`Latitude` AS `ziplat`, `z`.`Longitude` AS `ziplon` FROM `leads`.`leads_recent` AS `l`
LEFT JOIN `leads`.`data_location` AS `loc` ON l.id = loc.leadid
LEFT JOIN `ushud`.`states_distances` AS `dist` ON dist.locationid1 = loc.locationid AND dist.locationid2 = 203
LEFT JOIN `leads`.`data_credithistory` AS `dcrh` ON l.id = dcrh.leadid
LEFT JOIN `leads`.`_credithistory` AS `crh` ON dcrh.credithistory_id = crh.id
LEFT JOIN `leads`.`data_loanamount` AS `dla` ON l.id = dla.leadid
LEFT JOIN `leads`.`_loanamount` AS `la` ON dla.loanamount_id = la.id
LEFT JOIN `leads`.`data_loanamount_manual` AS `dlam` ON l.id = dlam.leadid
LEFT JOIN `leads`.`data_ownership` AS `down` ON l.id = down.leadid
LEFT JOIN `leads`.`_ownership` AS `own` ON down.ownership_id = own.id
LEFT JOIN `leads`.`data_maxshares` AS `dmxs` ON l.id = dmxs.leadid
LEFT JOIN `livebuyers`.`lead_transactions` AS `owner` ON l.id = owner.leadid and owner.userid = 0
LEFT JOIN `livebuyers`.`xref_users_leadflags` AS `xlf9` ON l.id = xlf9.leadid and xlf9.userid = 0 and xlf9.leadflagid = 9
LEFT JOIN `livebuyers`.`lead_status` AS `lstat` ON l.id = lstat.leadid
LEFT JOIN `livebuyers`.`_lead_status_types` AS `lstattype` ON lstattype.status_type_id = lstat.statusid
LEFT JOIN `leads`.`data_rapleaf_premium` AS `drprem` ON l.id = drprem.leadid
LEFT JOIN `leads`.`data_onlinedate` AS `dold` ON l.id = dold.leadid and dold.usertypeid = 1
LEFT JOIN `leads`.`data_score` AS `sc` ON l.id = sc.leadid
LEFT JOIN `ushud`.`states` AS `s` ON loc.locationid = s.locationid
LEFT JOIN `ushud`.`zips` AS `z` ON l.zip = z.zip WHERE (dist.locationid1 IS NOT NULL) AND (IF(loc.locationid = 106 and l.source_id = 3, 1, 0) <> 1) AND (IF(xlf9.leadflagid IS NULL, 0, 1) = 0) AND (s.state = 'CA') AND (crh.credithistory IN ('excellent')) AND (300000 BETWEEN IFNULL(la.minval, 0) AND IF(la.minval IS NULL, IFNULL(la.maxval, 0), IFNULL(la.maxval, 4294967295)) OR 300000 BETWEEN IFNULL(dlam.minval, 0) AND IF(dlam.minval IS NULL, IFNULL(dlam.maxval, 0), IFNULL(dlam.maxval, 4294967295))) AND (IF(IFNULL(vis_shareowner_searchresults, 1) = 0 AND owner.leadid IS NOT NULL, 0, 1) = 1) AND (IFNULL(vis_allusers_searchresults, 1) = 1) AND (loc.locationID != 0) AND (sc.isvalid = 1) AND (sc.underreview = 0) AND (IF(dold.ts_added IS NULL, l.ts_added, dold.ts_added) > DATE_SUB(CURDATE(), INTERVAL 120 DAY)) HAVING (numTimesSold < maxshares) ORDER BY `DistSort` ASC, `l`.`ts_added` DESC