此查询将返回ETL的位置以及在ETL过程中拉出的表。我想创建一个查询,向我显示任何具有设置表>的max(date_pulled)的位置。事件表的max(date_pulled)。
SELECT
location_id, table_name, max(date_pulled)
FROM
ods.etl_log
where table_name in ('events','settings')
group by location_id, table_name
结果如下:
location_id | table_name | max(date_pulled)
20 | events | 2014-12-04
20 | settings | 2014-12-05
26 | events | 2014-12-04
26 | settings | 2014-12-03
但我的最终结果我想看起来像这样:
location_id | max(date_pulled) settings | max(date_pulled) events | settings > events?
20 | 2014-12-05 | 2014-12-04 | Yes
26 | 2014-12-03 | 2014-12-04 | No
我是否需要使用嵌套连接来执行此操作?一个用于事件,另一个用于设置,然后将它们连接在一起并创建一个if字段?
答案 0 :(得分:0)
这是pivoting
的一种形式。您可以通过几种方式在mysql中实现这些结果,其中max
和case
将结果放在子查询中:
SELECT
location_id,
max(case when table_name ='events' then maxdate_pulled end) maxdateevent,
max(case when table_name ='settings' then maxdate_pulled end) maxdatesettings,
max(case when table_name ='settings' then maxdate_pulled end) >
max(case when table_name ='events' then maxdate_pulled end) settingsgreater
FROM (
SELECT location_id, table_name, max(date_pulled) maxdate_pulled
FROM ods.etl_log
WHERE table_name in ('events','settings')
GROUP BY location_id, table_name
) T
group by location_id