尝试填充别名列,其中X = 1,多个连接,子查询

时间:2016-11-07 19:07:57

标签: mysql

我在下面的查询中返回了我期望的所有列。但是,date_met列似乎返回一个。)所有结果的相同日期和b。)任何结果的错误日期。看起来它真的只是在date_start列中选择最新的日期,考虑到它是2016-11-06,这是一个有效的date_start,但它超出了主要的查询BETWEEN ...

我对子查询非常可怕,所以我不确定我到底错在哪里:

SELECT 
    the_thresholds.client_id,
    the_thresholds.zipcode,
    ziplatlongs.county,
    ziplatlongs.city,
    ziplatlongs.stateabbr,
    the_thresholds.email,
    the_thresholds.threshold_snow AS snow_threshold,
    SUM(certifications.total_snow) AS rollingTotal,
    SUM(certifications.total_snow) >= the_thresholds.threshold_snow AS targetMet,
    IF(SUM(certifications.total_snow) >= the_thresholds.threshold_snow,
        MIN(certifications.date_start),
    0) AS dateMet
FROM
    `customer_settings`.`snowfall_thresholds` AS the_thresholds
        INNER JOIN
    `snowfall_certification`.`the_certifications` AS certifications ON `the_thresholds`.`zipcode` = `certifications`.`zipcode`
        INNER JOIN
    `zipcodes`.`ziplatlongs` AS ziplatlongs ON `the_thresholds`.`zipcode` = `ziplatlongs`.`zipcode`
WHERE
    DATE(`certifications`.`date_start`) BETWEEN '2015-10-01' AND '2016-05-31'
GROUP BY `the_thresholds`.`zipcode`

正如我所说的,它正在返回我期望的所有列,我只是无法将它放到达到阈值的正确dateMet。

0 个答案:

没有答案