SQL在两个值之间选择数据

时间:2018-08-29 15:03:15

标签: sql select between

有人知道如何选择所有数据

   between (AE = "A" AND AE = "E") 
OR between (AE = "A" AND AE = "B")  
OR  between (AE = "B" AND AE = "E")

表按IPID排序,然后按Measurement_time排序

请看所附图片。

谢谢!

table

3 个答案:

答案 0 :(得分:0)

您的第二个和第三个之间没有用,因为第一个条件覆盖了  无论如何,其他两个的结果应该是

  select * from my_table 
  where   (AE between  "A" AND  "E") 
      OR  (AE between  "A" AND  "B")  
      OR  (AE between  "B" AND  "E") 

答案 1 :(得分:0)

我不知道如何处理其他值,也不知道A / B / E是否可能重复,但是类似的逻辑应该起作用:用前一个A / B / E字符填充空行,然后进行过滤。

WITH cte AS
 (
   SELECT t.*,
      Last_Value(CASE WHEN AE IN ('A', 'B', 'E' THEN AE END IGNORE NULLS)
      Over (PARTITION BY IPID
            ORDER BY measurement_time
            ROWS BETWEEN Unbounded Preceding AND 1 Preceding) AS lv
   FROM tab t
 )
SELECT *
FROM cte 
WHERE lv IN ('A', 'B')

答案 2 :(得分:0)

来自sas.com论坛的TomKari在数据步骤中找到了解决方案。

感谢dnoeth和scaisEdge为您提供帮助!

今天过得愉快!

data SetSwitch;
    retain KeepSwitch 0;
    set Have;

    if AE = "A" then KeepSwitch = 1;
    else if AE = "E" then KeepSwitch = 0;
    else if AE = "B" & KeepSwitch = 1 then KeepSwitch = 0;
    else if AE = "B" & KeepSwitch = 0 then KeepSwitch = 1;
run;

data SetVar;
    set SetSwitch;

    if KeepSwitch = 1 then if AE = " " then AE = lag(AE);
run;

data Want;
    set SetVar;

    if AE ^= " " then output;
run;