如何查看行之间的更改?

时间:2016-08-18 21:09:54

标签: sql hiveql

我有一张类似

的表格
object_id   attribute_1    attribute_2    attribute_3
1           a              a              a
1           a              a              a
1           a              b              a
1           a              a              a
2           z              z              z
2           w              z              z

我想了解用户对这些对象所做的更改。一个潜在的最终结果可能是对象表和属性更改的次数,以及对象记录的总次数,如下所示:

object_id   attribute_1    attribute_2    attribute_3   count
1           0              2              0             3
2           1              0              0             2

我将如何做到这一点?还有哪些其他可能的方法来量化和理解正在做出哪些改变?

1 个答案:

答案 0 :(得分:1)

如果在将属性更改回先前值时不需要将其视为更改,则可以按object_id和attribute_N进行分组,以获取此属性的不同值的数量。

我建议您编写应用程序或程序以计算您想要的内容。但是如果你只想用一个查询解决问题,那就是这样的:

SELECT object_id,
       sum(changes_attribute_1) AS changes_attribute_1,
       sum(changes_attribute_2) AS changes_attribute_2
FROM
  ( SELECT object_id,
           sum(IF(atribute = 2,changes,0)) AS changes_attribute_2,
           sum(IF(atribute = 1,changes,0)) AS changes_attribute_1
   FROM
     (SELECT count(*)-1 AS changes,
             object_id,
             1 AS atribute
      FROM
        (SELECT count(*),
                object_id
         FROM TABLE AS inner_table
         GROUP BY object_id,
                  attribute_1) AS tudo
      GROUP BY object_id
      UNION ALL SELECT count(*)-1 AS changes,
                       object_id,
                       2 AS atribute
      FROM
        (SELECT count(*),
                object_id
         FROM TABLE AS inner_table
         GROUP BY object_id,
                  attribute_2) AS tudo
      GROUP BY object_id) AS tudo
   GROUP BY object_id,
            atribute) AS tudo2
GROUP BY object_id

我已尝试使用此表进行查询:

object_id, attribute_1, attribute_2
1, 1, 1
1, 2, 1
1, 3, 2
2, 1, 1
2, 2, 2
2, 3, 4

我有这个结果

# object_id, changes_attribute_1, changes_attribute_2
1, 2, 1
2, 2, 2