尝试从连接表中计算每个唯一ID的行数 – MySQL

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

Trying to count rows per unique ID from JOINED table - MySQL

问题

我已经将几个表格使用 JOIN 进行了合并(全部使用 LEFT JOIN 更具体)。
现在我正在尝试计算其中一个表格(表 - measures)中每个唯一参考号(主表 - referral 上的)的记录数量的条件。

一个推荐可以有多个度量,并且每个度量可以具有不同的状态(来自度量表的状态计算列)。
我正在尝试创建一个列(Test),它将计算每个推荐的所有未取消度量。

使用我目前的代码,我得到了不带有“取消”状态的度量的记录的总数,但我想按推荐拆分这个数量。

谢谢,Maria

请参阅我到目前为止的代码:

  1. SELECT r.reference, r.status, pcla.la_name, r.project_reference,
  2. (SELECT COUNT(DISTINCT(id))
  3. FROM measure
  4. WHERE status_calculation <> "Cancelled") AS "Test",
  5. p.EPCR_GasProperty, DATE(r.created)
  6. FROM referral r
  7. LEFT JOIN postcode_la pcla ON r.postcode = pcla.postcode
  8. LEFT JOIN measure m ON r.reference = m.reference
  9. LEFT JOIN customer c ON r.reference = c.reference
  10. LEFT JOIN survey s ON s.customer_id = c.id
  11. LEFT JOIN property p ON p.id = s.property_id
  12. AND DATE(r.created) > "2022-03-31"
  13. AND m.completed IS NOT NULL
  14. AND pcla.la_name = "Ealing"
  15. GROUP BY r.reference
  16. ORDER BY r.reference
  17. LIMIT 20;

这是我得到的结果:

result table

希望有人能指导我缺少了什么。

英文:

I have combined few tables with JOIN (all LEFT JOIN to be more specific).
Now I am trying to count the number of records from one of the tables (table - measures) per
unique Reference number from main table - referral on a condition.

One referral can have more than one measure, and each measure can have different Status (col status_calculation from measure table).
I am trying to create a column (Test) which will count all non-cancelled measures per referral.

With the code I currently have I get the total number of records where a measure is not with status "Cancelled", but I want to split this per referral.

Thanks,
Maria

Please see below the code I have so far:

  1. SELECT r.reference, r.status, pcla.la_name, r.project_reference,
  2. (SELECT COUNT(DISTINCT(id))
  3. FROM measure
  4. WHERE status_calculation <> "Cancelled") AS "Test",
  5. p.EPCR_GasProperty, DATE(r.created)
  6. FROM referral r
  7. LEFT JOIN postcode_la pcla ON r.postcode = pcla.postcode
  8. LEFT JOIN measure m ON r.reference = m.reference
  9. LEFT JOIN customer c ON r.reference = c.reference
  10. LEFT JOIN survey s ON s.customer_id = c.id
  11. LEFT JOIN property p ON p.id = s.property_id
  12. AND DATE(r.created) > "2022-03-31"
  13. AND m.completed IS NOT NULL
  14. AND pcla.la_name = "Ealing"
  15. GROUP BY r.reference
  16. ORDER BY r.reference
  17. LIMIT 20;

this is the result I am getting:

result table

Hopefully someone can advise what is it that I am missing..

答案1

得分: 0

你需要包括所有相关的引用,如下所示:

  1. SELECT r.reference, r.status, pcla.la_name, r.project_reference,
  2. (SELECT COUNT(DISTINCT(m1.id))
  3. FROM measure m1
  4. WHERE m1.status_calculation <> "Cancelled"
  5. and m1.reference = r.reference) AS "Test",
  6. p.EPCR_GasProperty, DATE(r.created)
  7. FROM referral r
  8. LEFT JOIN postcode_la pcla ON r.postcode = pcla.postcode
  9. LEFT JOIN measure m ON r.reference = m.reference
  10. LEFT JOIN customer c ON r.reference = c.reference
  11. LEFT JOIN survey s ON s.customer_id = c.id
  12. LEFT JOIN property p ON p.id = s.property_id
  13. AND DATE(r.created) > "2022-03-31"
  14. AND m.completed IS NOT NULL
  15. AND pcla.la_name = "Ealing"
  16. GROUP BY r.reference
  17. ORDER BY r.reference
  18. LIMIT 20;

如果您需要进一步的帮助,请随时告诉我。

英文:

You need to include only all references that belong

like

  1. SELECT r.reference, r.status, pcla.la_name, r.project_reference,
  2. (SELECT COUNT(DISTINCT(m1.id))
  3. FROM measure m1
  4. WHERE m1.status_calculation &lt;&gt; &quot;Cancelled&quot;
  5. and m1.reference = r.reference) AS &quot;Test&quot;,
  6. p.EPCR_GasProperty, DATE(r.created)
  7. FROM referral r
  8. LEFT JOIN postcode_la pcla ON r.postcode = pcla.postcode
  9. LEFT JOIN measure m ON r.reference = m.reference
  10. LEFT JOIN customer c ON r.reference = c.reference
  11. LEFT JOIN survey s ON s.customer_id = c.id
  12. LEFT JOIN property p ON p.id = s.property_id
  13. AND DATE(r.created) &gt; &quot;2022-03-31&quot;
  14. AND m.completed IS NOT NULL
  15. AND pcla.la_name = &quot;Ealing&quot;
  16. GROUP BY r.reference
  17. ORDER BY r.reference
  18. LIMIT 20;

huangapple
  • 本文由 发表于 2023年6月18日 21:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500725.html
匿名

发表评论

匿名网友

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

确定