我需要检查具有订阅小时数的代理机构消耗的分钟数和总百分比。表格保留了每个代理所消耗的分钟数,但如果“nocomp_ta”列值为“1”,则为总值,如果为“0”,则为净值(收费小时)。如果已启用的列是“1”,则它不会进入度量标准。我的问题是我似乎无法让它工作,没有抛出错误,但结果不正确。我正在使用 Heidi sql 进行测试。
SELECT
( SUM( times.times_minutes ) / 60 ) AS Gross,
( SUM( times.times_minutes ) / 60 * 100 ) / agencies.subscription AS GrossPercent,
( _tt.net / 60 ) AS Net,
( ( _tt.net / 60 )*100 ) / agencies.subscription AS NetPercent,
times.time_minutes,
times.time_task,
times.time_agencie,
tasks_tiemes.code_t,
tasks_tiemes.nocomp_ta,
agencias.EMP_CODE,
agencias.EMP_NAME,
agencies.subscription,
agencies.habilitated
FROM times
INNER JOIN agencies
ON agencies.EMP_CODE = times.time_agencie
INNER JOIN tasks_times
ON tasks_times.nocomp_ta = 0 AND times.time_task = tasks_times.code_t
LEFT JOIN (SELECT
( SUM( times.time_minutes ) / 60 ) AS net,
times.time_date,
times.time_minutes,
agencies.EMP_CODE,
agencies.habilitaed
FROM times
INNER JOIN agencies
ON agencies.EMP_CODE = times.time_agencie
INNER JOIN tasks_times
ON tasks_times.nocomp_ta = 1 AND times.time_task = tasks_times.code_t
WHERE
agencies.habilitated = 0 AND
AND YEAR( times.time_date ) BETWEEN '2013' AND '2013'
AND MONTH( times.time_date ) BETWEEN '5' AND '5'
GROUP BY times.time_agencie) _tt
ON _tt.EMP_CODE = times.time_agencie
WHERE
agencies.habilitated = 0
AND YEAR( times.time_date ) BETWEEN '2013' AND '2013'
AND MONTH( times.time_date ) BETWEEN '5' AND '5'
GROUP BY times.time_agencie