我有下一个查询:
SELECT NAME_TABLE.USER,
TO_CHAR(NAME_TABLE.LAST_LOGIN, 'DD/MM/YYYY') LAST_LOGIN,
NAME_TABLE.SAILA,
MAX(NAME_TABLE.INTENTS) INTENTS,
NAME_TABLE.MAQ
FROM OWNER.MY_TABLE_NAME NAME_TABLE
WHERE NAME_TABLE.LAST_LOGIN>=TO_DATE('22/02/2017' || ' 00:00:00', 'dd/mm/yyyy hh24:mi:ss')
AND NAME_TABLE.LAST_LOGIN <= TO_DATE('22/02/2017' || ' 23:59:59', 'dd/mm/yyyy hh24:mi:ss')
GROUP BY NAME_TABLE.USER,
TO_CHAR(NAME_TABLE.LAST_LOGIN,'DD/MM/YYYY'),
NAME_TABLE.SAILA,
NAME_TABLE.INTENTS,
NAME_TABLE.MAQ
ORDER BY NAME_TABLE.SAILA,
NAME_TABLE.LAST_LOGIN DESC;
我收到下一个错误:
ORA-00979: not a GROUP BY expression
但是执行下一步:
SELECT NAME_TABLE.USER,
TO_CHAR(NAME_TABLE.LAST_LOGIN, 'DD/MM/YYYY') LAST_LOGIN,
NAME_TABLE.SAILA,
(SELECT MAX(INTENTS)
FROM OWNER.NAME_TABLE), NAME_TABLE.MAQ
FROM OWNER.MY_TABLE_NAME NAME_TABLE
WHERE NAME_TABLE.LAST_LOGIN>=TO_DATE('22/02/2017' || ' 00:00:00', 'dd/mm/yyyy hh24:mi:ss')
AND NAME_TABLE.LAST_LOGIN <= TO_DATE('22/02/2017' || ' 23:59:59', 'dd/mm/yyyy hh24:mi:ss')
GROUP BY NAME_TABLE.USER,
TO_CHAR(NAME_TABLE.LAST_LOGIN,'DD/MM/YYYY'),
NAME_TABLE.SAILA,
NAME_TABLE.INTENTS,
NAME_TABLE.MAQ
ORDER BY NAME_TABLE.SAILA;
任何解决方案?
为什么退出最新字段执行不错?
并重新
MAX(NAME_TABLE.INTENTS) INTENTS
有关
(SELECT MAX(INTENTS) FROM OWNER.NAME_TABLE)
为什么?
更新
SELECT USER, TO_CHAR(LAST_LOGIN, 'DD/MM/YYYY') LAST_LOGIN, SAILA, (SELECT MAX(INTENTS) FROM OWNER.NAME_TABLE, MAQ FROM OWNER.MY_TABLE_NAME
WHERE LAST_LOGIN>=TO_DATE('22/02/2017' || ' 00:00:00', 'dd/mm/yyyy hh24:mi:ss') AND LAST_LOGIN <= TO_DATE('22/02/2017' || ' 23:59:59', 'dd/mm/yyyy hh24:mi:ss')
GROUP BY USER, TO_CHAR(LAST_LOGIN,'DD/MM/YYYY'), SAILA, INTENTS, MAQ ORDER BY SAILA;
为什么退出NAME_TABLE.FIELD TO FIELD没有显示错误?
Iam noob。
谢谢!
答案 0 :(得分:0)
您的问题是ORDER BY
。实际上,GROUP BY
:
SELECT NAME_TABLE.USER,
TO_CHAR(NAME_TABLE.LAST_LOGIN, 'DD/MM/YYYY') as LAST_LOGIN,
NAME_TABLE.SAILA,
MAX(NAME_TABLE.INTENTS) as INTENTS,
NAME_TABLE.MAQ
FROM OWNER.MY_TABLE_NAME NAME_TABLE
WHERE NAME_TABLE.LAST_LOGIN>=TO_DATE('22/02/2017' || ' 00:00:00', 'dd/mm/yyyy hh24:mi:ss')
AND NAME_TABLE.LAST_LOGIN <= TO_DATE('22/02/2017' || ' 23:59:59', 'dd/mm/yyyy hh24:mi:ss')
GROUP BY NAME_TABLE.USER,
TO_CHAR(NAME_TABLE.LAST_LOGIN,'DD/MM/YYYY'),
NAME_TABLE.SAILA,
NAME_TABLE.MAQ
ORDER BY NAME_TABLE.SAILA,
MAX(NAME_TABLE.LAST_LOGIN) DESC;
所以:
NAME_TABLE.INTENTS
不应该在GROUP BY
中,因为它是MAX()
的参数。NAME_TABLE.LAST_LOGIN
不能在ORDER BY
中,因为它不在GROUP BY
中。您可以放入LAST_LOGIN
,但列的定义方式使得排序很尴尬。所以,MAX()
解决了这个问题。WHERE
中的条件更改为:NAME_TABLE.LAST_LOGIN >= DATE '2017-02-22' AND NAME_TABLE.LAST_LOGIN < DATE '2017-02-23
。更容易阅读。