Oracle sql count() 函数?

发布于 2024-12-13 03:02:40 字数 1412 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

昇り龍 2024-12-20 03:02:41

使用having 子句:

having count(*) > 0

Use the having clause:

having count(*) > 0
三月梨花 2024-12-20 03:02:41

我的快速回答是,您必须执行 LEFT JOIN 而不是您所做的 INNER JOIN。试试这个:

SELECT ag.nom_agence as NOM_AGENCE, ag.responsable_agence, count(*) as RESPONSABLE
FROM agences ag
left join locations l on l.id_agence = ag.id_agence
WHERE 
  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

My quick answer is that you have to do LEFT JOIN and not INNER JOIN that you do. Try this:

SELECT ag.nom_agence as NOM_AGENCE, ag.responsable_agence, count(*) as RESPONSABLE
FROM agences ag
left join locations l on l.id_agence = ag.id_agence
WHERE 
  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
小鸟爱天空丶 2024-12-20 03:02:41
SELECT ag.nom_agence as NOM_AGENCE, ag.responsable_agence, count(*) as RESPONSABLE
FROM agences ag LEFT OUTER JOIN locations l ON ag.id_agence = l.id_agence 
WHERE 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 

关键是您需要执行 LEFT OUTER JOIN ,它将检索代理表中的所有记录,无论位置中是否有匹配的记录

SELECT ag.nom_agence as NOM_AGENCE, ag.responsable_agence, count(*) as RESPONSABLE
FROM agences ag LEFT OUTER JOIN locations l ON ag.id_agence = l.id_agence 
WHERE 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 

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

金兰素衣 2024-12-20 03:02:41

我找到了查询。它给了我正确的桌子

SELECT ag.nom_agence as NOM_AGENCE, ag.responsable_agence as RESPONSABLE, count(case when to_char(l.date_location, 'YYYY') = '2010' and l.duree>3 then 1 end) as NOMBRE
FROM agences ag, locations l
WHERE ag.id_agence = l.id_agence
      group by nom_agence, responsable_agence

谢谢大家的帮助。

I found the query. It gives me the right table

SELECT ag.nom_agence as NOM_AGENCE, ag.responsable_agence as RESPONSABLE, count(case when to_char(l.date_location, 'YYYY') = '2010' and l.duree>3 then 1 end) as NOMBRE
FROM agences ag, locations l
WHERE ag.id_agence = l.id_agence
      group by nom_agence, responsable_agence

Thanks for your help, everyone.

白况 2024-12-20 03:02:40

您需要对位置表使用外部联接,以确保始终提取该期间内的所有租金,无论其长度如何。

然后统计出租次数> 3天。

试试这个:

SELECT 
  NOM_AGENCE, 
  RESP_AGENCE,
  SUM(RESPONSABLE)
FROM 
  (
    SELECT 
      ag.nom_agence as NOM_AGENCE, 
      ag.responsable_agence RESP_AGENCE, 
      CASE  
        WHEN l.duree > 3 THEN 1
        ELSE 0
      END RESPONSABLE
    FROM 
      agences ag LEFT OUTER JOIN locations l ON 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') 
  ) 
GROUP BY 
  NOM_AGENCE,
  RESP_AGENCE

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:

SELECT 
  NOM_AGENCE, 
  RESP_AGENCE,
  SUM(RESPONSABLE)
FROM 
  (
    SELECT 
      ag.nom_agence as NOM_AGENCE, 
      ag.responsable_agence RESP_AGENCE, 
      CASE  
        WHEN l.duree > 3 THEN 1
        ELSE 0
      END RESPONSABLE
    FROM 
      agences ag LEFT OUTER JOIN locations l ON 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') 
  ) 
GROUP BY 
  NOM_AGENCE,
  RESP_AGENCE
江湖正好 2024-12-20 03:02:40

尝试以下方法(我没有尝试)。

它基本上是机构表和按照您的搜索过滤器按机构分组的租金之间的外部联接。

NVL 将在正确查询中没有匹配的机构的计数转换为 0。

select left.id_agence, left.nom_agence, left.responsable_agence, NVL(right.count, 0)
from
(select id_agence, nom_agence, responsable_agence from agences) left
left outer join 
(
  SELECT id_agence, count(*) as count
  FROM locations
  WHERE date_location BETWEEN to_date('01/01/2010','DD.MM.YYYY') AND     to_date('31/12/2010','DD.MM.YYYY') AND
      duree > 3
  group by id_agence
) right
on left.id_agence = right.id_agence

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.

select left.id_agence, left.nom_agence, left.responsable_agence, NVL(right.count, 0)
from
(select id_agence, nom_agence, responsable_agence from agences) left
left outer join 
(
  SELECT id_agence, count(*) as count
  FROM locations
  WHERE date_location BETWEEN to_date('01/01/2010','DD.MM.YYYY') AND     to_date('31/12/2010','DD.MM.YYYY') AND
      duree > 3
  group by id_agence
) right
on left.id_agence = right.id_agence
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文