我有一张看起来像这样的表:
+-----------+--------------+---------------------+
| ENTRY_ID | EMPLOYEE_ID | ACCESS_TIME |
+-----------+--------------+---------------------+
| 001 | 1001 | 6/3/2014 8:08:00 AM |
+-----------+--------------+---------------------+
| 777 | 1201 | 8/3/2015 7:07:00 AM |
+-----------+--------------+---------------------+
| 888 | 1901 | 9/9/2015 4:04:00 PM |
+-----------+--------------+---------------------+
为了获得特定日期范围的唯一身份用户数,我有以下代码:
SELECT COUNT(UNIQUE Employee_Id) Unique_Monthly_Users FROM
(SELECT * FROM CORE.DATE_TEST
WHERE ACCESS_TIME >= TO_DATE('8/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
AND ACCESS_TIME < TO_DATE('9/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
ORDER BY ACCESS_TIME ASC)
为了获得该范围的当前月份,我正在使用它:
SELECT UNIQUE TO_CHAR(ACCESS_TIME, 'Month') Month_Name FROM (
SELECT * FROM CORE.DATE_TEST
WHERE ACCESS_TIME >= TO_DATE('8/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
AND ACCESS_TIME < TO_DATE('9/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
ORDER BY ACCESS_TIME ASC)
这是我最后的期望输出:
+--------------+-----------+
| UNIQUE_USERS | MONTH |
+--------------+-----------+
| 702931 | JANUARY |
+--------------+-----------+
| 900000 | FEBRUARY |
+--------------+-----------+
| 2139041022 | MARCH |
+--------------+-----------+
现在我尝试加入这些陈述:
SELECT COUNT(UNIQUE EMPLOYEE_ID) Unique_Emp FROM
((SELECT * FROM CORE.DATE_TEST
WHERE ACCESS_TIME >= TO_DATE('8/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
AND ACCESS_TIME < TO_DATE('9/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
ORDER BY ACCESS_TIME ASC) col1)
INNER JOIN (SELECT UNIQUE TO_CHAR(ACCESS_TIME, 'Month') Month_Name FROM (
(SELECT * FROM CORE.DATE_TEST
WHERE ACCESS_TIME >= TO_DATE('8/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
AND ACCESS_TIME < TO_DATE('9/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
ORDER BY ACCESS_TIME ASC
) col2)
) ON COL1.ENTRY_ID = COL2.ENTRY_ID
......但显然这不起作用。我无法获得“id”,因为任何一个结果集都不应该有ID。两者都是存在于外部的集合。我收到以下错误:ORA-00904: "COL2"."ENTRY_ID": invalid identifier
如何正确连接这些结果集,以便获得所需的输出?
答案 0 :(得分:1)
您可以使用两个子句执行此操作,第一个选择每个月的不同用户,第二个用于计算:
SELECT
COUNT(*) AS UNIQUE_USERS,
T1.MONTH
FROM
(
SELECT DISTINCT
USER_ID,
TO_CHAR(ACCESS_TIME) AS MONTH
FROM DATE_TEST
) AS T1
GROUP BY T1.MONTH
答案 1 :(得分:1)
查找GROUP BY
子句,然后尝试:
select to_char(access_time, 'Month'), count(distinct employee_id)
from core.date_test
where usage_time >= TO_DATE('8/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
and usage_time < TO_DATE('9/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
group by to_char(access_time, 'Month');