SQL查询以查找所用订阅时间的总百分比和净百分比
我需要检查有订阅小时数的机构所消耗的分钟数的净百分比和总百分比。表中保留了每个代理消耗的分钟数,但如果“nocomp_ta”collumn值为“1”,则为总分钟数,如果为“0”,则为净小时数(可计费小时数)。如果collumn-habilitated为“1”,则不在度量中。我的问题是,我似乎无法使它工作,没有抛出错误,但结果不正确。我正在使用HeidiSQL进行测试SQL查询以查找所用订阅时间的总百分比和净百分比,sql,Sql,我需要检查有订阅小时数的机构所消耗的分钟数的净百分比和总百分比。表中保留了每个代理消耗的分钟数,但如果“nocomp_ta”collumn值为“1”,则为总分钟数,如果为“0”,则为净小时数(可计费小时数)。如果collumn-habilitated为“1”,则不在度量中。我的问题是,我似乎无法使它工作,没有抛出错误,但结果不正确。我正在使用HeidiSQL进行测试 SELECT ( SUM( times.times_minutes ) / 60 ) AS Gross, ( SUM( time
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
我认为当您可能需要一个联合时,您正在使用联合(以涵盖
nocomp_ta
的不同计算情况-但您可能可以在一个查询中使用以下内容:
select
(SUM(time.time_minutes * tasks_time.nocomp_ta)) as net,
(SUM(time.time_minutes * (1 - tasks_time.nocomp_ta))) as gross
...
我认为当您可能需要一个联合时,您正在使用联合(以涵盖
nocomp_ta
的不同计算情况-但您可能可以在一个查询中使用以下内容:
select
(SUM(time.time_minutes * tasks_time.nocomp_ta)) as net,
(SUM(time.time_minutes * (1 - tasks_time.nocomp_ta))) as gross
...
您得到的结果是什么?您预期的结果是什么?向我们展示一些sql FIDLE和示例数据,以便我们能够更好地帮助您。您得到的结果是什么,您预期的结果是什么?向我们展示一些sql FIDLE和示例数据,以便我们能够更好地帮助您