如何计算在相同类别中不存在TRUE的情况?

huangapple go评论98阅读模式
英文:

How to count if there does not exist TRUE in the same category?

问题

  1. |roadNum|length|
  2. |-|-|
  3. |3|140|
  4. |9|209|

请注意,你提供的SQL查询代码中,表的别名不一致。你在查询中使用了 "c" 和 "r" 作为表的别名,但在 WHERE 子句中使用了 "h.highwayNum",这会导致查询出现错误。我已经根据你的需求进行了修正。

英文:

Assume I have two tables:

cameraNum roadNum isWorking
100 1 TRUE
101 1 FALSE
102 1 TRUE
103 3 FALSE
104 3 FALSE
105 7 TRUE
106 7 TRUE
107 7 TRUE
108 9 FALSE
109 9 FALSE
110 9 FALSE
roadNum length
1 90
3 140
7 110
9 209

I want to select a table like this:
If there is no camera working, I put it in the table.

roadNum length
3 140
9 209

I tried this below:

  1. SELECT r.roadNum, r.length
  2. FROM Cameras c, Road r
  3. WHERE c.isWorking = FALSE
  4. AND h.highwayNum = c.highwayNum

But these code only fliter there exists FALSE in isWorking.

roadNum length
1 90
3 140
9 209

答案1

得分: 1

以下是代码的中文翻译:

  1. 您想要的是所有摄像头都不工作的道路。以下是一种使用聚合和`having`的方法:
  2. select r.*
  3. from road r
  4. inner join camera c on c.roadNum = r.roadNum
  5. group by r.roadNum
  6. having not bool_or(isWorking)

在DB Fiddle上查看演示

roadnum length
3 140
9 209
英文:

You want roads whose all cameras are not working. Here is one way to do it with aggregation and having:

  1. select r.*
  2. from road r
  3. inner join camera c on c.roadNum = r.roadNum
  4. group by r.roadNum
  5. having not bool_or(isWorking)

Demo on DB Fiddle

roadnum length
3 140
9 209

答案2

得分: 0

关于使用 not exists,是的,你可以使用它。以下代码使用公共表达式(CTE)仅获取满足摄像头要求roadnum,然后将其与road表连接起来(请参见演示)。

  1. with no_working_camera (roadnum) as
  2. ( select distinct on (c1.roadNum)
  3. c1.roadnum
  4. from cameras c1
  5. where not c1.isworking
  6. and not exists (select null
  7. from cameras c2
  8. where c2.roadNum = c1.roadNum
  9. and c2.isworking
  10. )
  11. order by c1.roadnum
  12. )
  13. select r.*
  14. from no_working_camera nwc
  15. join road r
  16. on nwc.roadnum = r.roadnum;
英文:

Regarding using not exists, yes, you can use it. The following uses a CTE to get only the roadnum of those satisfying the camera requirement then joins that to road: (see demo)

  1. with no_working_caMera (roadnum) as
  2. ( select distinct on (c1.roadNum)
  3. c1.roadnum
  4. from cameras c1
  5. where not c1.isworking
  6. and not exists (select null
  7. from cameras c2
  8. where c2.roadNum = c1.roadNum
  9. and c2.isworking
  10. )
  11. order by c1.roadnum
  12. )
  13. select r.*
  14. from no_working_camera nwc
  15. join road r
  16. on nwc.roadnum = r.roadnum;

答案3

得分: 0

只使用NOT INNOT EXISTS来查找没有工作摄像头的道路:

  1. select *
  2. from road
  3. where roadnum not in (select roadnum from cameras where isworking);
英文:

Don't join, don't aggregate, just use NOT IN or NOT EXISTS in order to find roads that don't have a working camera:

  1. select *
  2. from road
  3. where roadnum not in (select roadnum from cameras where isworking);

huangapple
  • 本文由 发表于 2023年2月6日 02:49:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定