比较"连续" Access中的行

时间:2012-03-27 12:56:19

标签: sql ms-access ms-access-2010

我有一个看起来像这样的表:

ID   Subid      Time    Value     Value2
1        1       3       100       2
1        1       4       100       2
2        2       3       200       1       
2        2       4       200       2
4        4       4       10        4
5        6       3       10        2       
5        6       4       12        2

我现在要做的只是显示从时间3到时间4有差异的条目。所以结果应如下所示:

ID   Subid      Time    Value     Value2
2        2       3       200       1       
2        2       4       200       2
4        4       4       10        4
5        6       3       10        2       
5        6       4       12        2

第二部分中的条目条件如下: 如果有两个条目具有相同的ID和Subid,但是月份不同,则检查value和value2是否相等。如果它们相等,则删除条目,否则显示条目。 如果在其他时间找不到匹配,则还显示该条目。 通常这个表会有很多匹配的条目,所以我希望通过对它们进行排序来使我的生活更轻松。

我必须使用MS Access(2010)来执行此操作。

感谢您的帮助。

在Remou的回答后编辑:

我现在遇到的问题是我没有上面显示的表,但我用另一个查询创建它。所以现在我只是把查询在Remou使用它的四个地方创建那个表...我确信有更好的方法来做到这一点,但我无法弄明白......

SELECT t2.*

FROM (SELECT t.ID, t.SubID, t.Time, t.Value, t.value2, t.value FROM

( HUGE SELECT HERE with 3 joins) t

   WHERE t.Time=4)  AS t2 
           LEFT JOIN 
   (SELECT t.ID, t.SubID, t.Time, t.Value, t.value2, t.value
    FROM

( HUGE SELECT HERE with 3 joins) t

    WHERE t.Time=3)  AS t1 
         ON t2.Id = t1.Id
         WHERE t2.value<>t1.value
         OR t2.value2<>t1.value2 
         OR t1.Id Is Null

UNION ALL

   SELECT t1.* FROM
   (SELECT t.ID, t.SubID, t.Time, t.Value, t.value2, t.value
    FROM

( HUGE SELECT HERE with 3 joins) t

     WHERE t.Time=3) AS t1
           LEFT JOIN
    (SELECT t.ID, t.SubID, t.Time, t.Value, t.value2, t.value
    FROM

( HUGE SELECT HERE with 3 joins) t

    WHERE t.Time=4) AS t2
          ON t1.Id=t2.Id
          WHERE t1.value<>t2.value
         OR t1.value2<>t2.value2 
          OR t2.Id Is Null

WHERE t.Time=4) AS t2 LEFT JOIN (SELECT t.ID, t.SubID, t.Time, t.Value, t.value2, t.value FROM

2 个答案:

答案 0 :(得分:1)

怎么样:

SELECT t2.*
FROM (SELECT t.ID, t.Subid, t.Time, t.Value, t.Value2
FROM Test2 t
WHERE t.Time=4)  AS t2 LEFT JOIN (SELECT t.ID, t.Subid, t.Time, t.Value, t.Value2
FROM Test2 t
WHERE t.Time=3)  AS t1 ON t2.ID = t1.ID
WHERE t2.Value<>t1.Value 
OR t2.Value2<>t1.Value2 
OR t1.ID Is Null
UNION ALL
SELECT t1.* FROM
(SELECT t.ID, t.Subid, t.Time, t.Value, t.Value2
FROM Test2 t
WHERE t.Time=3) t1
LEFT JOIN
(SELECT t.ID, t.Subid, t.Time, t.Value, t.Value2
FROM Test2 t
WHERE t.Time=4) t2
ON t1.ID=t2.ID
WHERE t1.Value <> t2.Value
OR  t1.Value2 <> t2.Value2
OR t2.ID Is Null
ORDER BY ID

答案 1 :(得分:0)

由于这是MS Access,如何使用两个t副本创建查询(Access调用第二个t_1)(只需连接每个字段,或查看此http://i49.tinypic.com/rix175.jpg。)

然后右键单击,编辑SQL,您应该看到如下内容:

    SELECT t.ID, t.Subid, t.Time, t.Value, t.Value2
    FROM t INNER JOIN t AS t_1 ON (t.Value2 = t_1.Value2) 
    AND (t.Value = t_1.Value) 
    AND (t.Subid = t_1.Subid) 
    AND (t.Time = t_1.Time-1) 
    AND (t.ID = t_1.ID);

你可以像这样重写:

    SELECT t.ID, t.Subid, t.Time, t.Value, t.Value2
    FROM t INNER JOIN t AS t_1 ON 
    (t.Time = t_1.Time-1)
    AND (t.ID = t_1.ID) 
    AND (t.Subid = t_1.Subid) 
    AND ((t.Value <> t_1.Value) OR (t.Value2 <> t_1.Value2));

注意“t.Time = t_1.Time-1”编辑和AND语句;这将为您提供Time = 3的查询结果。现在对此进行一次UNION,略微修改,这次使用“t.Time-1 = t_1.Time”代替得到Time = 4的结果。然后在末尾添加一个以捕获唯一条目:

    SELECT t.ID, t.Subid, t.Time, t.Value, t.Value2
    FROM t INNER JOIN t AS t_1 ON 
    (t.Time = t_1.Time-1) 
    AND (t.ID = t_1.ID) 
    AND (t.Subid = t_1.Subid) 
    AND ((t.Value <> t_1.Value) OR (t.Value2 <> t_1.Value2))

    UNION SELECT t.ID, t.Subid, t.Time, t.Value, t.Value2
    FROM t INNER JOIN t AS t_1 ON 
    (t.Time-1 = t_1.Time)  
    AND (t.ID = t_1.ID)
    AND (t.Subid = t_1.Subid) 
    AND ((t.Value <> t_1.Value) OR (t.Value2 <> t_1.Value2))

    UNION SELECT t.ID, t.Subid, t.Time, t.Value, t.Value2
    FROM t INNER JOIN t AS t_1 ON 
    (t.Time-1 = t_1.Time) 
    AND (t.ID <> t_1.ID) 
    AND (t.Subid <> t_1.Subid);