我有一个查询,从2个子查询和左连接中选择。问题是左连接没有正确识别子查询,我不知道为什么。它无法识别dt.timestamp
SELECT dt.TIMESTAMP
,count(*)
,tc.total_cells - count(*)
FROM (
SELECT DISTINCT TIMESTAMP
FROM (
SELECT to_char(trunc(TIMESTAMP, 'hh24'), 'dd/mm/yyyy hh24:mi') TIMESTAMP
FROM t_date_time
WHERE trunc(TIMESTAMP, 'dd') = to_date('16-mar-17', 'dd-mon-yy')
ORDER BY TIMESTAMP DESC
)
ORDER BY to_date(TIMESTAMP, 'dd/mm/yyyy hh24:mi')
) dt,
(
SELECT count(DISTINCT cell) total_cells
FROM t_ca_processed
WHERE trunc(TIMESTAMP, 'dd') = trunc(to_date('16-mar-17', 'dd-mon-yy'), 'dd')
) tc
LEFT JOIN t_ca_processed p ON dt.timestamp = to_char(p.TIMESTAMP, 'dd/mm/yyyy hh24:mi')
GROUP BY dt.TIMESTAMP
ORDER BY to_date(dt.TIMESTAMP, 'dd/mm/yyyy hh24:mi')
答案 0 :(得分:0)
**
SELECT dt.TIMESTAMP
,count(*)
,tc.total_cells - count(*)
FROM (
SELECT DISTINCT TIMESTAMP
FROM (
SELECT to_char(trunc(TIMESTAMP, 'hh24'), 'dd/mm/yyyy hh24:mi') TIMESTAMP
FROM t_date_time
WHERE trunc(TIMESTAMP, 'dd') = to_date('16-mar-17', 'dd-mon-yy')
ORDER BY TIMESTAMP DESC
)
ORDER BY to_date(TIMESTAMP, 'dd/mm/yyyy hh24:mi')
) dt
left join
(
SELECT count(DISTINCT cell) total_cells
FROM t_ca_processed
WHERE trunc(TIMESTAMP, 'dd') = trunc(to_date('16-mar-17', 'dd-mon-yy'), 'dd')
) tc
ON dt.timestamp = --(tc.something)--to_char(p.TIMESTAMP, 'dd/mm/yyyy hh24:mi')
GROUP BY dt.TIMESTAMP
ORDER BY to_date(dt.TIMESTAMP, 'dd/mm/yyyy hh24:mi')
**
并且您需要在TC查询中添加TIMESTAMP列,然后我们才能匹配DT和TC
答案 1 :(得分:0)
编译器不喜欢混合连接符号(vs vs left join)
一种方法是使用公用表表达式(CTE)来实现交叉连接数据,然后在左连接中使用它。
WITH TC AS (
SELECT DISTINCT TIMESTAMP
FROM (
SELECT to_char(trunc(TIMESTAMP, 'hh24'), 'dd/mm/yyyy hh24:mi') TIMESTAMP
FROM t_date_time
WHERE trunc(TIMESTAMP, 'dd') = to_date('16-mar-17', 'dd-mon-yy')
ORDER BY TIMESTAMP DESC
)
ORDER BY to_date(TIMESTAMP, 'dd/mm/yyyy hh24:mi')
) dt,
(
SELECT count(DISTINCT cell) total_cells
FROM t_ca_processed
WHERE trunc(TIMESTAMP, 'dd') = trunc(to_date('16-mar-17', 'dd-mon-yy'), 'dd')
)
SELECT dt.TIMESTAMP
,count(*)
,tc.total_cells - count(*)
FROM tc
LEFT JOIN t_ca_processed p ON dt.timestamp = to_char(p.TIMESTAMP, 'dd/mm/yyyy hh24:mi')
GROUP BY dt.TIMESTAMP
ORDER BY to_date(dt.TIMESTAMP, 'dd/mm/yyyy hh24:mi')