Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.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
Mysql 排除在第n次未指明的细菌感染后6个月内死亡的患者_Mysql_Sql - Fatal编程技术网

Mysql 排除在第n次未指明的细菌感染后6个月内死亡的患者

Mysql 排除在第n次未指明的细菌感染后6个月内死亡的患者,mysql,sql,Mysql,Sql,我有一个数据集,需要排除在上一次报告感染的6个月内死亡的患者。 以下是样本数据: id icd9 AgeAtDx AgeAtFirstDM AgeAtDeath disbetes infection 479174 I288.2 68 NULL 68.166666 0.0 0.0 479174 IV45.82 68.16 NULL 68.166666 0.0 0.0

我有一个数据集,需要排除在上一次报告感染的6个月内死亡的患者。 以下是样本数据:

id      icd9    AgeAtDx AgeAtFirstDM    AgeAtDeath  disbetes    infection
479174  I288.2  68      NULL            68.166666   0.0         0.0
479174  IV45.82 68.16   NULL            68.166666   0.0         0.0
479174  IV45.82 68      NULL            68.166666   0.0         0.0
479174  I272.4  68      NULL            68.166666   0.0         0.0
479174  I276.8  68      NULL            68.166666   0.0         0.0
479174  I338.3  68.16   NULL            68.166666   0.0         0.0
479174  I197.7  68      NULL            68.166666   0.0         0.0
479174  I600.00 68      NULL            68.166666   0.0         0.0
479174  I790.5  67.6    NULL            68.166666   0.0         0.0
479174  I573.8  67.75   NULL            68.166666   0.0         0.0
479174  IV66.7  68.16   NULL            68.166666   0.0         0.0
479174  I154.1  68.16   NULL            68.166666   0.0         0.0
479174  I401.9  68      NULL            68.166666   0.0         0.0
479174  I578.1  67.66   NULL            68.166666   0.0         0.0
479174  I414.01 68.16   NULL            68.166666   0.0         0.0
479174  IV45.82 68      NULL            68.166666   0.0         1.0
479174  I715.98 67.66   NULL            68.166666   0.0         0.0
479174  I607.84 67.66   NULL            68.166666   0.0         0.0
479174  I154.1  68      NULL            68.166666   0.0         0.0
479174  I300.00 68.16   NULL            68.166666   0.0         0.0
0.0=无感染,1.0=感染 到目前为止,我有以下代码

select * 
from #Data1
except
select * 
from #Data1
where infection = 1.0
and AgeAtDeath < AgeAtDx  + interval '6 month'

它不起作用了。任何帮助都将不胜感激。

在我看来,最简单的方法是使用天数而不是月份,因为您可以避免每月计算天数等。如果您的解决方案允许这种回旋余地,则可以这样做

select * 
from #Data1
except
select * 
from #Data1
where infection = 1.0
and DATEDIFF(DAY,AgeAtDeath, AgeAtDx) <= 180  --6 months

如果你的解决方案不允许这样做,那么这个家伙:写了一个UDF,可以让你更精确。但是使用函数可能会稍微影响性能。

您的示例缺少一列表示日期,如果没有它,获取请求的记录将是一件非常麻烦的事情。我认为间隔“6个月”应该是间隔“6个月”,不应该吗?不工作对任何人都没有帮助-当您发布的代码在语法上无效时,这一点尤其正确。所以把你的问题分解成碎片。你说在N之后。。。传染这意味着你在找一个号码-什么号码?或者你是说感染了什么?因为n不匹配。另外,如何解释年龄列?68.16是否意味着68年+0.16*365天?也许是0.16*364天?还是小数部分是指16天?如何计算6个月-6*30天?0.5年?你真正使用的是什么数据库?请贴上相应的标签。您的代码实际上与任何数据库都不一致——包含MySQL、SQL Server和Postgres元素。