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选择记录,但类型为某种类型且日期早于或与id相同时除外_Sql_Teradata - Fatal编程技术网

SQL选择记录,但类型为某种类型且日期早于或与id相同时除外

SQL选择记录,但类型为某种类型且日期早于或与id相同时除外,sql,teradata,Sql,Teradata,尝试选择所有记录,但类型较小且之前或与相同id的日期相同时除外 id date type 1 02/09/2012 BIG 2 05/16/2012 BIG 2 06/18/2012 BIG 3 08/08/2011 BIG 3 09/13/2011 BIG 4 06/08/2016 BIG 4 05/27/2016 SMALL 5 08/16/2012 BIG 5 08/15/2012 SMALL 6 09/05/2012 B

尝试选择所有记录,但类型较小且之前或与相同id的日期相同时除外

id  date    type
1   02/09/2012  BIG
2   05/16/2012  BIG
2   06/18/2012  BIG
3   08/08/2011  BIG
3   09/13/2011  BIG
4   06/08/2016  BIG
4   05/27/2016  SMALL
5   08/16/2012  BIG
5   08/15/2012  SMALL
6   09/05/2012  BIG
7   09/05/2012  BIG
7   02/13/2013  BIG
7   08/03/2011  BIG
7   05/09/2012  BIG
7   04/24/2013  SMALL
8   03/31/2017  BIG
8   03/06/2017  SMALL
9   02/17/2016  SMALL
我尝试的代码

select id, date, type
from (select id, date,type,   min(case when type = 'SMALL' then date end) 
over (partition by id) as min_dt
from table1 as t ) t
where date>min_dt
group by 1,2,3
这个只给了我这些记录

id  date    type    
4   06/08/2016  BIG
5   08/16/2012  BIG
8   3/31/2017   BIG
但这是我期待的一次

id  date    type    
1   2/9/2012    BIG 
2   5/16/2012   BIG 
2   6/18/2012   BIG 
3   8/8/2011    BIG 
3   9/13/2011   BIG 
4   6/8/2016    BIG 
4   5/27/2016   SMALL   X
5   8/16/2012   BIG 
5   8/15/2012   SMALL   X
6   9/5/2012    BIG 
7   9/5/2012    BIG 
7   2/13/2013   BIG 
7   8/3/2011    BIG 
7   5/9/2012    BIG 
7   4/24/2013   SMALL   
8   3/31/2017   BIG 
8   3/6/2017    SMALL   X
9   2/17/2016   SMALL   
除了错误的一次之外,所有的都应该是错误的。这里有一种方法:

select t1.*
from table1 t1
where t1.type <> 'SMALL' or
      exists (select 1
              from table1 tt1
              where tt1.id = t1.id and tt1.date < t1.date
             );
编辑:

根据OP的声明,我们得到了所有的记录,除了

一,。类型很小

二,。与同一id在同一日期之前 我将此视为日期小于或等于至少1条具有相同id的其他记录

都是真的,

这意味着我们将获得至少一个条件为false的记录

因此,类型“SMALL”获取第一个条件为false的记录

or not exists(select date from table1 
    where id = t.id and date >= t.date and type <> 'SMALL')

获取第二个条件为false的记录,例如,如果没有具有相同id且数据小于或等于的记录

这将应用与@AnthonyMcGrath solution相同的逻辑,但使用OLAP函数:如果存在另一个日期更晚或相等的小行,则使用running total检查小行并丢弃它

SELECT *
FROM table1
QUALIFY 
   type <> 'SMALL' -- all other rows
OR -- it's SMALL
   Min(CASE WHEN type <> 'SMALL' THEN date END) -- returns NULL if there's no later row
   Over (PARTITION BY id
         ORDER BY date DESC
           ,CASE WHEN type <> 'SMALL' THEN 0 ELSE 1 END -- same date-> ensure SMALL is sorted last
         ROWS Unbounded Preceding) IS NULL
重新表述您的逻辑,仅当最新的一行似乎也适用于行号时才返回一小行:

QUALIFY type_ <> 'SMALL'
  OR Row_Number() -- get the latest row if it's SMALL
     Over (PARTITION BY id
           ORDER BY date DESC
             ,CASE WHEN type <> 'SMALL' THEN 0 ELSE 1 end) = 1

您需要将EXISTS条件修改为id=t.id和date>=t.date,然后键入'SMALL'以捕获之前或相同的日期条件。请提供一些信息,说明上面的代码如何回答问题以改进此答案。不返回最后一行9 2/17/2016SMALL@Gordon是此查询不提供最后一行这是意料之中的。
QUALIFY type_ <> 'SMALL'
  OR Row_Number() -- get the latest row if it's SMALL
     Over (PARTITION BY id
           ORDER BY date DESC
             ,CASE WHEN type <> 'SMALL' THEN 0 ELSE 1 end) = 1