CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

这个查询怎么写?

楼主GameLu(GameKing)2005-11-09 16:31:19 在 MS-SQL Server / 基础类 提问

如何求每个产品销量最高的5种,以及销量  
  表字段:Company,Product,sales  
  要求,求每个COmpany的销量最高的5个产品,及其销量  
   
  急  
  谢谢各位。 问题点数:50、回复次数:12Top

1 楼vivianfdlpw()回复于 2005-11-09 16:34:12 得分 0

select   *   from   表   t  
  where   sales   in   (select   distinct   top   5   sales    
                                        from   表  
                                              where   company=t.company)Top

2 楼vivianfdlpw()回复于 2005-11-09 16:34:42 得分 10

少了order   by  
   
  select   *   from   表   t  
  where   sales   in   (select   distinct   top   5   sales    
                                        from   表  
                                              where   company=t.company  
                                                    order   by   sales   desc)  
  Top

3 楼GameLu(GameKing)回复于 2005-11-09 16:37:24 得分 0

结果明显不对呀  
  :(  
  Top

4 楼shou440(记得绿罗裙,处处怜芳草)回复于 2005-11-09 16:41:28 得分 0

vivianfdlpw大哥可能没看清要求:  
             
                  求每个Company的销量最高的5个Product  
   
      可以用游标实现。Top

5 楼GameLu(GameKing)回复于 2005-11-09 16:43:36 得分 0

SELECT   *  
  FROM   SrvCount   t  
  WHERE   (sales   IN  
                      (SELECT   DISTINCT   TOP   5   sales  
                    FROM   srvcount  
                    WHERE   company   =   t.company  
                    ORDER   BY   sales   DESC))  
  -----------------------------------------------------------  
  sales Company Product  
  1 1195 AAAQ  
  2 1195 AAAR  
  599 1195 HHTCA  
  1 1195 DDYCJT  
  1 1195 HHWRZQ  
  2 1195 HHXXBB  
  90 1203 JXT  
  5 1203 DXCLUB  
  20 1203 MLCLUB  
  23 1203 JDXYCLUB  
  1 1211 SB  
  5502 1211 TQ  
  12 1211 UA  
  1 1211 WT  
  71 1212 XXCHBTop

6 楼GameLu(GameKing)回复于 2005-11-09 16:44:08 得分 0

1195   Company   有6条记录丫  
  Top

7 楼wangdehao(找找找(现在很幸福))回复于 2005-11-09 17:05:11 得分 0

 
  select   *   from   (select   Company,Product,sum(sales)as   sales   from   [table]   group   by   Company,Product)a  
    where    
  (  
    (  
        select   count(*)   from  
            (select   Company,Product,sum(sales)as   sales   from   [table]   group   by   Company,Product)b  
              where   a.Company   =   b.Company   and   b.sales>a.sales  
  ))<=4Top

8 楼zlp321002(Life Is Good,Let's Shine)回复于 2005-11-09 17:14:11 得分 40

--try  
  select   *   from   表   A  
  where   Product   in  
  (select   top   5   Product   from   表   where   Company=A.Company   order   by   sales   desc)  
  order   by   CompanyTop

9 楼GameLu(GameKing)回复于 2005-11-09 17:17:23 得分 0

汗  
  我用游标做的,还是结果不对  
   
  DECLARE   @CompanyName   varchar(11),   @Product   varchar(20),   @Counts   varchar(40),  
        @message   varchar(80)  
   
  DECLARE   Company_cursor   CURSOR   FOR    
  SELECT   distinct   Company  
  FROM   SrvCount  
  ORDER   BY   Company  
   
  OPEN   Company_cursor  
   
  FETCH   NEXT   FROM   Company_cursor    
  INTO   @CompanyName  
   
  WHILE   @@FETCH_STATUS   =   0  
  BEGIN  
   
        PRINT   @message  
   
        DECLARE   Product_cursor   CURSOR   FOR    
        SELECT   Top   5   Products,Sales  
        FROM   SrvCount  
        WHERE   Company   =   @CompanyName  
        ORDER   BY   Sales   Desc  
   
        OPEN   Product_cursor  
        FETCH   NEXT   FROM   Product_cursor   INTO   @Product,@Counts  
   
        IF   @@FETCH_STATUS   <>   0    
              PRINT   '                   <<No   Items>>'  
   
        WHILE   @@FETCH_STATUS   =   0  
        BEGIN  
               
              SELECT   @message   =   @CompanyName   +   '|'   +   @Product   +   '|'   +   @Counts  
              PRINT   @message  
              FETCH   NEXT   FROM   Product_cursor   INTO   @Product,@Counts  
        END  
   
        CLOSE   Product_cursor  
        DEALLOCATE   Product_cursor  
         
        --   Get   the   next   author.  
        FETCH   NEXT   FROM   Company_cursor    
        INTO   @CompanyName  
   
  END  
   
  CLOSE   Company_cursor  
  DEALLOCATE   Company_cursor  
  -------------------------------------------------  
   
     
  1195|HHTCA|599  
  1195|AAAR|2  
  1195|HHXXBB|2  
  1195|HHWRZQ|1  
  1195|DDYCJT|1  
  1195|DDYCJT|1  
  1203|JXT|90  
  1203|JDXYCLUB|23  
  1203|MLCLUB|20  
  1203|DXCLUB|5  
  1203|DXCLUB|5  
  1211|TQ|5502  
  1211|UA|12  
  1211|WT|1  
  1211|SB|1  
  1211|SB|1  
  1212|XXSNRJ|727  
  1212|SHRJZN|307  
  1212|YXBYA|97  
  1212|XXCHB|71  
  1212|YXZXCS|12  
  1212|YXZXCS|12Top

10 楼vivianfdlpw()回复于 2005-11-09 17:19:56 得分 0

1195   Company   有6条记录丫  
  =========>  
  因为存在销量并列的的情况Top

11 楼GameLu(GameKing)回复于 2005-11-09 18:14:34 得分 0

可是我是     SELECT   Top   5   Products,Sales  
        FROM   SrvCount  
   
  Top   5   出来的  
  Top

12 楼GameLu(GameKing)回复于 2005-11-09 18:15:06 得分 0

zlp321002     的结果正确  
   
  结帖  
  谢谢各位  
  Top

相关问题

  • 这个查询怎么写?
  • 这个查询怎么写?
  • 这个查询怎么写?
  • 这个查询怎么写?
  • 这个查询怎么写?
  • 这个查询怎么写。
  • 这个查询怎么写?
  • 这个sql查询语句怎么写?
  • 这个sql查询语句怎么写?
  • 这个查询语句怎么写?

关键词

  • sales
  • 销量
  • top
  • company
  • ddycjt
  • dxclub
  • twhere
  • cursor
  • products
  • fetch

得分解答快速导航

  • 帖主:GameLu
  • vivianfdlpw
  • zlp321002

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo