Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于子查询减少结果_Sql_Ms Access - Fatal编程技术网

Sql 基于子查询减少结果

Sql 基于子查询减少结果,sql,ms-access,Sql,Ms Access,我正在尝试运行该查询,但我只想在以前的英语MOI月份至少有3个月的情况下减少结果 例如,我在MS Access中使用以下字段导出了OTIFDATATOTOExport表: ╔════════════╦═══════════════════════╦═══════════════════════════════════════════════════════════╗ ║ t1.Mois ║ t1.[Code fournisseur] ║ t2.ra

我正在尝试运行该查询,但我只想在以前的英语MOI月份至少有3个月的情况下减少结果

例如,我在MS Access中使用以下字段导出了OTIFDATATOTOExport表:

╔════════════╦═══════════════════════╦═══════════════════════════════════════════════════════════╗
║  t1.Mois   ║ t1.[Code fournisseur] ║                      t2.ratioOnTime                       ║
╠════════════╬═══════════════════════╬═══════════════════════════════════════════════════════════╣
║ 12/01/2016 ║                     1 ║ 0,5 = Outcomes AVG ratioOnTime 0 ( no 3 previous months)  ║
║ 01/01/2017 ║                     1 ║ 0,5 = Outcomes AVG ratioOnTime 0 ( no 3 previous months)  ║
║ 02/01/2017 ║                     1 ║ 0,5 = Outcomes AVG ratioOnTime 0 ( no 3 previous months)  ║
║ 03/01/2017 ║                     1 ║ 0,5 = Outcomes AVG ratioOnTime 0,5 (  3 previous months)  ║
║ 12/01/2016 ║                     2 ║ 1 = Outcomes AVG ratioOnTime 0 ( no 3 previous months)    ║
║ 01/01/2017 ║                     2 ║ 0,5 = Outcomes AVG ratioOnTime 0 ( no 3 previous months)  ║
║ 02/01/2017 ║                     2 ║ 0,5 = Outcomes AVG ratioOnTime 0 ( no  3 previous months) ║
║ 03/01/2017 ║                     2 ║ 0,5 = Outcomes AVG ratioOnTime 0,66 ( 3 previous months)  ║
║ 03/12/2016 ║                     3 ║ 0,5 = Outcomes AVG ratioOnTime 0 ( no 3 previous months)  ║
╚════════════╩═══════════════════════╩═══════════════════════════════════════════════════════════╝
选择t1.Mois,t1.[Code fourniseur],avgt2.rationtime作为Moyenne 3 dernier Mois 从…起 otifdata导出为t1 左连接 选择t3.mois,t3.[代码Fourniseur],t3.time 从[OtifDataToExport]导出为t3 as t2 在t1.Mois>t2.Mois和t1上。[代码fourniseur]=t2。[代码fourniseur] 按t1分组。Mois,t1。[代码Fourniseur] 我想我已经接近了,但我无法在MS Access中找到解决方案

非常感谢您的帮助。

我想您只需要一个having子句:


除非我误解了您的要求,否则我可能会建议以下方法之一,具体取决于您是否希望显示前三个月内的记录:

select
    t1.mois,
    t1.[code fournisseur],
    avg(t2.ratioontime) as avgratio
from
    otifdatatoexport t1 left join otifdatatoexport t2 on
    t1.mois >= t2.mois and t1.[code fournisseur] = t2.[code fournisseur]
group by
    t1.mois,
    t1.[code fournisseur]
having
    count(*) > 3
order by
    t1.[code fournisseur],
    t1.mois
选择 t1.mois, t1.[代码Fourniseur], iifcount*>3,平均2.比率时间,0作为平均值 从…起 otifdatatoexport t1左连接otifdatatoexport t2 t1.mois>=t2.mois和t1.[code fourniseur]=t2.[code fourniseur] 分组 t1.mois, t1.[代码Fourniseur] 订购人 t1.[代码Fourniseur], t1.mois 要仅使用最近3个月的数据计算平均值,您可以更改联接条件,使其仅包括当前记录3个月内的记录,例如:

select
    t1.mois,
    t1.[code fournisseur],
    iif(count(*)>3,avg(t2.ratioontime),0) as avgratio
from
    otifdatatoexport t1 left join otifdatatoexport t2 on 
    (t2.mois between dateadd("m",-3,t1.mois) and t1.mois) and t1.[code fournisseur] = t2.[code fournisseur]
group by
    t1.mois,
    t1.[code fournisseur]
order by
    t1.[code fournisseur],
    t1.mois
或者,使用前三个月,您可以使用以下内容:

select
    t1.mois,
    t1.[code fournisseur],
    iif(count(*)=4,avg(t2.ratioontime),0) as avgratio
from
    otifdatatoexport t1 left join otifdatatoexport t2 on 
    t1.mois >= t2.mois and t1.[code fournisseur] = t2.[code fournisseur]
group by
    t1.mois,
    t1.[code fournisseur]
order by
    t1.[code fournisseur],
    t1.mois

我已经检查过了,结果显示该查询占用了前一个月的所有时间。也许我错过了什么?嗨,李麦克,谢谢你的帮助,如果只针对特定的t1.MOI和t1,是否可以定制查询以仅执行avgt2.ratioontime。[代码Fourniseur]它确实存在前3个月特定的月份,不包括在平均数中。实际上,我只想在T1之前的前三个月表现平均值。平均值不包括MOI。我希望我很清楚谢谢你李麦克,我很接近,我刚刚注意到这种情况t2.mois介于dateaddm,-3,t1.mois和t1.mois之间,假设连续3个月。但我有一个案子,我有三个月的时间,但还没有结束。1月20日,3月20日,4月20日,5月20日。2月20日不见了,但如果可能的话,我想计算3个月的平均数。再次感谢您抽出宝贵的时间!!!ArteI已将结果添加为图像,它仅适用于计数=4,如图像cpt=4所示。谢谢
select
    t1.mois,
    t1.[code fournisseur],
    iif(count(*)=4,avg(t2.ratioontime),0) as avgratio
from
    otifdatatoexport t1 left join otifdatatoexport t2 on 
    t1.mois >= t2.mois and t1.[code fournisseur] = t2.[code fournisseur]
group by
    t1.mois,
    t1.[code fournisseur]
order by
    t1.[code fournisseur],
    t1.mois