在表A中我有日期,而在B中我有订单号。 在这两个表中,我有一个称为订单ID的公共字段。 我只有一个简单的目标来获取每个日期的订单数量[如第1,第2 ..] 这是我尝试过的,因为我不想使用连接或视图。
select
A.date_of_order,
count(B.order_number)
from A, B
where A.order_id=B.order_id;
group by A.date_of_order
我收到以下错误。可能会犯一些微不足道的错误。提前致谢 更新: 考虑到Dmitri和rafa的建议后,我得到了表格:
23-FEB-14 1
23-FEB-14 1
23-FEB-14 2
23-FEB-14 2
23-FEB-14 2
07-MAR-14 2
07-MAR-14 4
07-MAR-14 1
07-MAR-14 5
02-MAR-14 1
02-MAR-14 1
正如我所说,我的要求非常简单,只需将其作为
23-Feb-14 10[i.e. all the orders placed on this date]
07-Mar-14 13
02-mar-14 2
答案 0 :(得分:1)
在 GROUP BY:
之前应该放 select A.date_of_order,
count(B.order_number)
from A, B
where A.order_id = B.order_id -- <- possible, but join will be better here
group by A.date_of_order
如果你想在
GROUP BY之后找到条件,你应该使用HAVING select A.date_of_order,
count(B.order_number)
from A, B
where A.order_id = B.order_id
group by A.date_of_order
having count(B.order_number) < 3 -- having demo
答案 1 :(得分:1)
WHERE
子句必须在GROUP BY
子句的之前。
使用TRUNC(date)
摆脱时间,以便GROUP BY
按预期工作。
SELECT TRUNC(A.date_of_order), COUNT(B.order_number)
FROM A, B
WHERE A.order_id=B.order_id
GROUP BY TRUNC(A.date_of_order)
无论如何,建议改为使用ANSI-standard SQL JOIN
子句。
SELECT TRUNC(A.date_of_order), COUNT(B.order_number)
FROM A INNER JOIN B ON A.order_id = B.order_id
-- (WHERE conditions here)
GROUP BY TRUNC(A.date_of_order)
-- (HAVING conditions here)
答案 2 :(得分:0)
可能的原因可能是
1)日期有时间戳,在这种情况下,查询后会有所帮助:
select
trunc(A.date_of_order),
count(B.order_number)
from A, B
where A.order_id=B.order_id
group by trunc(A.date_of_order);
2)因为在您的示例数据中,您已经计算了日期,所以您需要为查询计算总和而不是计数
select
A.date_of_order,
sum(B.order_number)
from A, B
where A.order_id=B.order_id
group by A.date_of_order;
3)或者可以两者兼而有,在这种情况下你可以尝试
select
trunc(A.date_of_order),
sum(B.order_number)
from A, B
where A.order_id=B.order_id
group by trunc(A.date_of_order);
答案 3 :(得分:0)
我根本不明白为什么你需要加入另一张桌子。试着跑步:
select date_of_order, count(order_id) from tbl_a group by date_of_order
您的问题表明,表A包含每个订单ID和日期的行,您想要做的就是按日期计算订单数。