SQL分组报表一例(摘自book online)

USE pubs
GO
SELECT   Category = 
      CASE type
         WHEN 'popular_comp' THEN 'Popular Computing'
         WHEN 'mod_cook' THEN 'Modern Cooking'
         WHEN 'business' THEN 'Business'
         WHEN 'psychology' THEN 'Psychology'
         WHEN 'trad_cook' THEN 'Traditional Cooking'
         ELSE 'Not yet categorized'
      END,
   CAST(title AS varchar(25)) AS 'Shortened Title',
   price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO

Here is the result set:

Category            Shortened Title           Price                      
------------------- ------------------------- -------------------------- 
Business            You Can Combat Computer S 2.99                       
Business            Cooking with Computers: S 11.95                      
Business            The Busy Executive's Data 19.99                      
Business            Straight Talk About Compu 19.99                      

                                              avg
                                              ==========================
                                              13.73                      

Category            Shortened Title           Price                      
------------------- ------------------------- -------------------------- 
Modern Cooking      The Gourmet Microwave     2.99                       
Modern Cooking      Silicon Valley Gastronomi 19.99                      

                                              avg
                                              ==========================
                                              11.49                      

Category            Shortened Title           Price                      
------------------- ------------------------- -------------------------- 
Popular Computing   Secrets of Silicon Valley 20.00                      
Popular Computing   But Is It User Friendly?  22.95                      

                                              avg
                                              ==========================
                                              21.48                      

Category            Shortened Title           Price                      
------------------- ------------------------- -------------------------- 
Psychology          Life Without Fear         7.00                       
Psychology          Emotional Security: A New 7.99                       
Psychology          Is Anger the Enemy?       10.95                      
Psychology          Prolonged Data Deprivatio 19.99                      
Psychology          Computer Phobic AND Non-P 21.59                      

                                              avg
                                              ==========================
                                              13.50                      

Category            Shortened Title           Price                      
------------------- ------------------------- -------------------------- 
Traditional Cooking Fifty Years in Buckingham 11.95                      
Traditional Cooking Sushi, Anyone?            14.99                      
Traditional Cooking Onions, Leeks, and Garlic 20.95                      

                                              avg
                                              ==========================
                                              15.96                      

(21 row(s) affected)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值