我需要一个查询,它将从书表中返回书籍列表
但它也会显示可用的书籍和书籍总数。
对于书籍数量,它将计算具有类似ISBN n数量的书籍数量,这将计算具有可用状态的类似ISBN的书籍数量
表格结构
Bookid booktitle author ISBN edition publisher price supplier volume language status
status contains available,issued
我希望结果为
BookTitle Author Publisher Location Total available
请解释该查询,因为我需要在许多表上实现它
我试过这个查询
select *,count(1) as Quantity from Book group by Book.BookTitle
它成功地为我提供了总数
的图书清单和
select *,count(1) as Quantity from Book where status='Available' group by Book.BookTitle
它成功地为我提供了有多少可用的书籍。
但我需要总计和可用的组合..
答案 0 :(得分:0)
您必须按SELECT查询中的所有项目进行分组。您可以计算(*)或总和(1)。我将这样做:
SELECT BookId, Name, Available, count(*) as Quantity
FROM Book GROUP BY Bookid,Name,Available
答案 1 :(得分:0)
简介:
select isbn, booktitle, sum(case when status='available' then 1 else 0 end) as avail, count(*) as total
from Book
group by isbn, booktitle
http://sqlfiddle.com/#!2/80a41/5
我绝对建议你阅读第三范式
答案 2 :(得分:0)
select d.* from (select BookTitle,Author,Publisher,Location,count(BookTitle) as Total, Status, Count(Status) as StatusQuantity from Book group by BookTitle,Author,Publisher,Location,Status)d where d.Status='Available'
答案 3 :(得分:0)
试试这个:
SELECT
BookTitle,
Author,
Publisher,
Location,
SUM(CASE WHEN status='available' THEN 1 ELSE 0 END) Available,
COUNT(*) AS Total
FROM Book
GROUP BY BookTitle,Author,Publisher,Location
答案 4 :(得分:0)
你有任何关键限制吗?
你必须拆分表..
使用 bookid或ISBN 作为主键,将状态存储在单独的表中。
这将很容易进一步处理,否则记录应该是多余的..