Oracle sql count() 函数?
我的 count() 函数有问题。我希望它仅在特定条件下才有效。
我想要做的是显示每个公司的名称、公司老板的名字以及2010年超过三天的租赁次数。
所以条件是:2010年超过三天的租赁次数。
因此,如果公司没有任何满足条件的租金,则不应从结果表中删除它,而应将其写入零。例如:
company 1 -------------------- BOSS 1-----------------------2
company 2---------------------- BOSS 2---------------------- 0 --doesn't satisfy the condition: 0 rentals
company 3-----------------------BOSS 3 ----------------------5
company 4---------------------- BOSS 4--------------------------1
company 4 ----------------------BOSS 5 ----------------------- 0 --doesn't satisfy the condition: 0 rentals
AND NOT
company 1----------------------BOSS 1---------------------------2
company 3--------------------- BOSS 3---------------------------5
company 4----------------------BOSS 4 --------------------------1
我的sql代码显示第二个表而不是第一个表。这是我的代码:
SELECT ag.nom_agence as NOM_AGENCE, ag.responsable_agence, count(*) as RESPONSABLE
FROM agences ag, locations l
WHERE ag.id_agence = l.id_agence AND
l.date_location BETWEEN to_date('01/01/2010','DD.MM.YYYY') AND to_date('31/12/2010','DD.MM.YYYY') AND
l.duree > 3
group by ag.nom_agence,ag.responsable_agence
我想要这种格式的东西(没有 where 子句):
count(l.date_location BETWEEN to_date('01/01/2010','DD.MM.YYYY') AND to_date('31/12/2010','DD.MM.YYYY') AND
l.duree > 3)
有什么想法吗?谢谢。
I have a problem with the function count(). I want to it to count only under specific conditions.
What i want to do is to display for each company, the company's name, the name of the company's boss and the number of rentals exceeding three days made in 2010.
So the condition is: the number of rentals exceeding three days made in 2010.
Therefore, if the company doesn't have any rentals that satisfy the condition, it shouldn't be eliminated form the resulting table but instead it should be written zero. For example:
company 1 -------------------- BOSS 1-----------------------2
company 2---------------------- BOSS 2---------------------- 0 --doesn't satisfy the condition: 0 rentals
company 3-----------------------BOSS 3 ----------------------5
company 4---------------------- BOSS 4--------------------------1
company 4 ----------------------BOSS 5 ----------------------- 0 --doesn't satisfy the condition: 0 rentals
AND NOT
company 1----------------------BOSS 1---------------------------2
company 3--------------------- BOSS 3---------------------------5
company 4----------------------BOSS 4 --------------------------1
My sql codes displays the second table and not the first table. This is my code:
SELECT ag.nom_agence as NOM_AGENCE, ag.responsable_agence, count(*) as RESPONSABLE
FROM agences ag, locations l
WHERE ag.id_agence = l.id_agence AND
l.date_location BETWEEN to_date('01/01/2010','DD.MM.YYYY') AND to_date('31/12/2010','DD.MM.YYYY') AND
l.duree > 3
group by ag.nom_agence,ag.responsable_agence
I want something of this format (without the where clause):
count(l.date_location BETWEEN to_date('01/01/2010','DD.MM.YYYY') AND to_date('31/12/2010','DD.MM.YYYY') AND
l.duree > 3)
Any ideas? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用having 子句:
Use the having clause:
我的快速回答是,您必须执行 LEFT JOIN 而不是您所做的 INNER JOIN。试试这个:
My quick answer is that you have to do LEFT JOIN and not INNER JOIN that you do. Try this:
关键是您需要执行 LEFT OUTER JOIN ,它将检索代理表中的所有记录,无论位置中是否有匹配的记录
The key is you need to do a LEFT OUTER JOIN which will retrieve all records from your agences table regardless of whether there are any matching records in locations
我找到了查询。它给了我正确的桌子
谢谢大家的帮助。
I found the query. It gives me the right table
Thanks for your help, everyone.
您需要对位置表使用外部联接,以确保始终提取该期间内的所有租金,无论其长度如何。
然后统计出租次数> 3天。
试试这个:
You need to use an outer join to the locations table to make sure you always pull in all rentals in the period irrespective of their length.
Then count the number of rentals > 3 days.
Try this:
尝试以下方法(我没有尝试)。
它基本上是机构表和按照您的搜索过滤器按机构分组的租金之间的外部联接。
NVL 将在正确查询中没有匹配的机构的计数转换为 0。
Try the following (I did not try it).
It's basically and outer join between the agencies table and the rentals grouped by agency following your search filter.
The NVL transforms the count for the agencies that don't have a match in the right query to a 0.