设计过程:
(1)对应的程序如下:
USE Library
DECLARE @num int
SELECT @num=(SELECT COUNT(*) FROM book) --图书总数
SELECT a.出版社 AS '出版社',
CASE
WHEN a.rate>50.0 THEN '很高'
WHEN a.rate>30.0 THEN '较高'
WHEN a.rate>10.0 THEN '一般'
ELSE '较低'
END AS '图书比例情况'
FROM (SELECT 出版社 AS '出版社',
CAST(ROUND(COUNT(*)*100.0/@num,1) AS decimal(5,1)) AS 'rate'
FROM book
GROUP BY 出版社) a
ORDER BY a.rate
(2)对应的程序如下:
USE Library
DECLARE @num int
SELECT @num=(SELECT COUNT(*) FROM borrow) --借书总数
SELECT a.系名 AS '系名',
CASE
WHEN a.rate>50.0 THEN '很高'
WHEN a.rate>30.0 THEN '较高'
WHEN a.rate>10.0 THEN '一般'
ELSE '较低'
END AS '借书情况'
FROM (SELECT depart.系名,CAST(ROUND(COUNT(*)*100.0/@num,1) AS decimal(5,1)) AS 'rate'
FROM borrow,student,depart
WHERE borrow.学号=student.学号 AND student.班号=depart.班号
GROUP BY depart.系名) a
ORDER BY a.rate DESC
(3)对应的程序如下:
USE Library
GO
DECLARE b_cur CURSOR
FOR SELECT DISTINCT(图书名),定价 FROM book
DECLARE @bn char(20),@dj decimal(4,1),@pr char(10)
OPEN b_cur
FETCH NEXT FROM b_cur INTO @bn,@dj
WHILE @@FETCH_STATUS = 0
BEGIN
SET @pr=CASE
WHEN @dj>=50 THEN '价格太高'
WHEN @dj>=30 THEN '价格偏高'
WHEN @dj>=20 THEN '价格适中'
WHEN @dj>=10 THEN '价格偏低'
ELSE '价格太低了'
END
PRINT @bn+CAST(@dj AS char(5))+@pr
FETCH NEXT FROM b_cur INTO @bn,@dj
END
CLOSE b_cur
DEALLOCATE b_cur
GO
(4)对应的程序如下:
USE Library
GO
DECLARE b_cur CURSOR
FOR
SELECT a.出版社,CAST(bsum*100.0/psum AS decimal(4,1)) As 'rate'
FROM (SELECT 出版社,COUNT(*) AS 'psum'
FROM book
GROUP BY 出版社) a,
(SELECT b.出版社,COUNT(bor.图书编号) AS 'bsum'
FROM book b,borrow bor
WHERE b.图书编号=bor.图书编号
GROUP BY b.出版社) b
WHERE a.出版社=b.出版社
DECLARE @pn char(16),@rt decimal(4,1)
OPEN b_cur
FETCH NEXT FROM b_cur INTO @pn,@rt
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT '"'+RTRIM(@pn)+'"图书借出率为'+RTRIM(CAST(@rt AS char(5)))+'%'
FETCH NEXT FROM b_cur INTO @pn,@rt
END
CLOSE b_cur
DEALLOCATE b_cur
GO