我想计算有多少本书具有相同的名称,然后计算其中有多少书的状态 = 1
I wanted to count how many books have same name then how many of those books have status = 1
我的代码:
SELECT ROW_NUMBER() OVER(ORDER BY Title ASC) as row,
Title,
BookNo,
COUNT(*) as Copies,
COUNT(Status = 1)
from tbbook
GROUP BY Title
result i got
- 查询不正确,您的 GROUP BY 表达式不完整。阅读 ONLY_FULL_GROUP_BY SQL 模式,您将修复查询。
COUNT(Status = 1)
显示 Status
不为 NULL 的行数。使用 SUM(Status = 1)
.
检查这个:
SELECT ROW_NUMBER() OVER(ORDER BY Title ASC) as RowNumber,
Title,
BookNo,
COUNT(*) AS CopiesAmount,
SUM(Status = 1) CopiesWithStatus_1
from tbbook
GROUP BY Title, BookNo
ORDER BY Title ASC
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=024afcc763715d6ffb52fee97156a797
我找到了答案及其子查询+内连接
我的代码:
SELECT ROW_NUMBER() OVER(ORDER BY Title ASC), tbbook.Title, COUNT(*), COALESCE(Available, 0) as Available
FROM tbbook
LEFT JOIN (
SELECT Title, COUNT(*) as Available
from tbbook
WHERE Status = 1
GROUP by Title
) AS n on tbbook.Title = n.Title
GROUP BY Title
我的代码:
SELECT ROW_NUMBER() OVER(ORDER BY Title ASC) as row,
Title,
BookNo,
COUNT(*) as Copies,
COUNT(Status = 1)
from tbbook
GROUP BY Title
result i got
- 查询不正确,您的 GROUP BY 表达式不完整。阅读 ONLY_FULL_GROUP_BY SQL 模式,您将修复查询。
COUNT(Status = 1)
显示Status
不为 NULL 的行数。使用SUM(Status = 1)
.
检查这个:
SELECT ROW_NUMBER() OVER(ORDER BY Title ASC) as RowNumber,
Title,
BookNo,
COUNT(*) AS CopiesAmount,
SUM(Status = 1) CopiesWithStatus_1
from tbbook
GROUP BY Title, BookNo
ORDER BY Title ASC
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=024afcc763715d6ffb52fee97156a797
我找到了答案及其子查询+内连接
我的代码:
SELECT ROW_NUMBER() OVER(ORDER BY Title ASC), tbbook.Title, COUNT(*), COALESCE(Available, 0) as Available
FROM tbbook
LEFT JOIN (
SELECT Title, COUNT(*) as Available
from tbbook
WHERE Status = 1
GROUP by Title
) AS n on tbbook.Title = n.Title
GROUP BY Title