Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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
如何使用列作为条件。Postgresql_Sql_Postgresql - Fatal编程技术网

如何使用列作为条件。Postgresql

如何使用列作为条件。Postgresql,sql,postgresql,Sql,Postgresql,我想通过使用以前应用于同一列的条件来减少该列。我使用的条件是日期(介于当前日期-间隔(“1个月”)和当前日期之间)。我要查找的列是id,以便查看在一段时间内使用某个应用程序的用户的id,然后将此id从整个id列中删除。这将显示上个月未使用该应用程序的用户的ID with t as( select id as active_id from table1 where last_interaction between current_date

我想通过使用以前应用于同一列的条件来减少该列。我使用的条件是日期(介于当前日期-间隔(“1个月”)和当前日期之间)。我要查找的列是id,以便查看在一段时间内使用某个应用程序的用户的id,然后将此id从整个id列中删除。这将显示上个月未使用该应用程序的用户的ID

with t as(
          select id as active_id
          from table1
          where last_interaction between current_date - interval '1 month' and current_date
          group by id)

select id
from table1 
join t
on t.active_id = table1.id
where table1.id != t.active_id

我将您要查找的内容称为“查找不匹配”查询。您希望查找表中与另一个查询的结果不匹配的所有内容。为此,在主表和“筛选表”之间执行左联接。当存在匹配项时,这将在t.xy列中放置t.xy的值,当不存在匹配项时,将在t.xy列中放置NULL

从这里开始,你只想得到一个t.xy为NULL的值,这样你就只能得到没有t.xy的t.x

with t as(
          select x as xy
          from table1
          where x between current_date - interval '1 month' and current_date
          group by x)

select x
from table1 
LEFT join t
on t.xy = table1.x
where t.xy IS NULL

我还修复了一个拼写错误,您使用的是
table.x
,而不是
table1.x

,仅从一个英语句子和一个部分SQL查询很难理解您想要什么。请提供示例数据和预期结果,以澄清您想要实现的目标。示例数据和预期结果可能会让您了解您正在尝试执行的操作。添加一些示例表数据和预期结果。(请用格式化文本,不要图片)我们可以给Yiyi一个你想要的条件的例子吗?我使用的条件是日期(在当前日期-间隔(“1个月”)和当前日期之间)。我要查找的列是id,以便查看在一段时间内使用某个应用程序的用户的id,然后将此id从整个id列中删除。这将向我显示上个月没有使用该应用程序的人的ID。非常感谢。“t.xy为空”是我要找的。