Oracle 甲骨文集团

Oracle 甲骨文集团,oracle,group-by,Oracle,Group By,我对甲骨文很陌生(几个小时)。我的经验就是MySQL 在我看来,我的查询是非常基本的,但是GROUPBY似乎没有返回预期的结果。经过几个小时的搜索,我希望这里有人能帮忙 以下是我的代码: SELECT trunc(start_time), display_name, count(*) FROM TblName WHERE (DISPLAY_NAME like '%Advise the customer how they can change their marketing preferences

我对甲骨文很陌生(几个小时)。我的经验就是MySQL

在我看来,我的查询是非常基本的,但是GROUPBY似乎没有返回预期的结果。经过几个小时的搜索,我希望这里有人能帮忙

以下是我的代码:

SELECT trunc(start_time), display_name, count(*)
FROM TblName
WHERE (DISPLAY_NAME like '%Advise the customer how they can change their marketing preferences%')
      OR (DISPLAY_NAME like '%Inform customer on relevant ads%')
      OR (DISPLAY_NAME like '%Is the customer requesting to block%')
GROUP BY start_time, display_name
HAVING start_time >= '2013-10-24';
我得到的结果将有多行,具有相同的开始时间、显示名称和计数1。。查询中返回的行超过400行,并且所有行的计数都为1


编辑:以下是结果。因此,我现在明白了为什么by小组工作不正常。在MySQL中,我会使用date(start_time)来纠正这个错误。甲骨文也有类似的情况吗

11-SEP-13 02.16.42.809000000 PM Advise the customer how they can change their marketing preferences.    1
25-SEP-13 11.56.05.814000000 AM Advise the customer how they can change their marketing preferences.    1
26-SEP-13 11.17.29.737000000 AM Advise the customer how they can change their marketing preferences.    1
24-OCT-13 10.53.17.941000000 AM Advise the customer how they can change their marketing preferences.    1
26-OCT-13 10.24.25.099000000 AM Is the customer requesting to block?    1
23-OCT-13 08.28.58.234000000 PM Advise the customer how they can change their marketing preferences.    1
25-OCT-13 02.50.49.329000000 PM Inform customer on relevant ads 1
26-OCT-13 11.46.45.686000000 AM Advise the customer how they can change their marketing preferences.    1
28-OCT-13 10.12.31.613000000 AM Inform customer on relevant ads 1
25-OCT-13 11.38.24.570000000 AM Inform customer on relevant ads 1

提前谢谢

您需要将您的TRUC(开始时间)应用于组,如下所示:

SELECT trunc(start_time), display_name, count(*)
FROM TblName
WHERE (DISPLAY_NAME like '%Advise the customer how they can change their marketing preferences%')
      OR (DISPLAY_NAME like '%Inform customer on relevant ads%')
      OR (DISPLAY_NAME like '%Is the customer requesting to block%')
GROUP BY trunc(start_time), display_name
HAVING trunc(start_time) >= '2013-10-24';

您需要通过以下方式将您的trunc(开始时间)应用于组:

SELECT trunc(start_time), display_name, count(*)
FROM TblName
WHERE (DISPLAY_NAME like '%Advise the customer how they can change their marketing preferences%')
      OR (DISPLAY_NAME like '%Inform customer on relevant ads%')
      OR (DISPLAY_NAME like '%Is the customer requesting to block%')
GROUP BY trunc(start_time), display_name
HAVING trunc(start_time) >= '2013-10-24';

请尝试
选择开始时间,显示\u名称
(注意不再显示
TRUNC()
),并查看
开始时间
是否不同。“我的经验都是MySQL。”---在MySQL中,这将是完全相同的问题。您的HAVING子句应该在WHERE子句中。另外,发布查询结果。开始时间值都是唯一的吗?“Oracle有类似的吗?”---您已经使用了
TRUNC()
了,不是吗?是的,但它没有预期的结果,因为我可以有多行具有相同的开始时间、显示名称和计数1@user2930089:对,因为您没有意识到,
SELECT
是在
groupby
之后执行的。如果将
TRUNC()
应用于
分组依据中的列,该怎么办?这样它就可以按日期分组尝试
选择开始时间,显示\u name
(注意不再
TRUNC()
),并查看
开始时间
是不同的。“我的经验都是MySQL。”---在MySQL中,这将是完全相同的问题。您的HAVING子句应该在WHERE子句中。另外,发布查询结果。开始时间值都是唯一的吗?“Oracle有类似的吗?”---您已经使用了
TRUNC()
了,不是吗?是的,但它没有预期的结果,因为我可以有多行具有相同的开始时间、显示名称和计数1@user2930089:对,因为您没有意识到,
SELECT
是在
groupby
之后执行的。如果将
TRUNC()
应用于
分组依据中的列,该怎么办?所以它按日期分组