CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

关于group?

楼主jbas(jbas)2004-12-03 12:30:44 在 MS-SQL Server / 基础类 提问

table:TMytable  
  autoid       code       memo  
  1                   a           aaa  
  2                   a           bbb  
  3                   a           cccccc  
  4                   b           kk  
  5                   b           kkkkk  
  6                   c             kkkk  
   
  我现在想取  
  autoid       code       memo  
  1                 a             aaa  
  4                 b             kk  
  6                 c             kkkk  
   
  我该怎样写这个sql语句?  
  问题点数:100、回复次数:16Top

1 楼YangYuWeb(飘邈...)回复于 2004-12-03 12:35:25 得分 30

select   *   from   tmytable   where   autoid=(select   max(autoid)   from   tmytable)   group   by   codeTop

2 楼netcoder(朱二)回复于 2004-12-03 12:35:42 得分 20

select   *     from   TMytable   T  
        where   autoid=(select   min(autoid)   from   TMytable   where   code=T.code)Top

3 楼skyboy0720(曲终人散)回复于 2004-12-03 12:36:08 得分 10

select   min(autoid)   as   autoid,code,min(memo)   as   memo   from   TMytable   group   by   codeTop

4 楼YangYuWeb(飘邈...)回复于 2004-12-03 12:38:24 得分 10

select   max(autoid),code,max(memo)   from   tmytable   group   by   codeTop

5 楼yyhyy23(只爱猪猪)回复于 2004-12-03 12:38:28 得分 0

autoid       code       memo  
  1                   a           aaa  
  2                   a           bbb  
  3                   a           cccccc  
  4                   b           kk  
  5                   b           kkkkk  
  6                   c             kkkk  
   
  我现在想取  
  autoid       code       memo  
  1                 a             aaa  
  4                 b             kk  
  6                 c             kkkk  
  select   *   from   (select   autoid,codefrom   tablename   group   by   autoid,code)a   inner   join   tablename   b   on   a.autoid=b.autoidTop

6 楼YangYuWeb(飘邈...)回复于 2004-12-03 12:39:32 得分 0

晕,是取最小的,用minTop

7 楼yyhyy23(只爱猪猪)回复于 2004-12-03 12:40:08 得分 0

yun,写错了  
   
  select   min(autoid),code,memo   as   memo   group   by   memoTop

8 楼netcoder(朱二)回复于 2004-12-03 12:40:48 得分 0

或者用连接:  
   
  select   A.*    
      from   tmytable   A   JOIN   (select   autoid=min(autoid),code   from   tmytable   group   by   code)   B  
      ON   A.autoid=B.autoid  
  Top

9 楼yyhyy23(只爱猪猪)回复于 2004-12-03 12:41:03 得分 0

select   min(autoid)   as   autoid,code,memo   as   memo   group   by   memoTop

10 楼YangYuWeb(飘邈...)回复于 2004-12-03 12:41:59 得分 0

select   *     from   TMytable   where    
  autoid   in   (select   min(autoid)   from   TMytable   group   by   code)Top

11 楼yyhyy23(只爱猪猪)回复于 2004-12-03 12:42:51 得分 0

select   *   from   (select   min(autoid)   as   autoid,code   from   tablename   group   by   code)a   inner   join   tablename   b   on   a.autoid=b.autoidTop

12 楼yelook(香槟酒)回复于 2004-12-03 12:49:07 得分 10

使用   GROUP   BY   分组多行  
  GROUP   BY   子句用来为结果集中的每一行产生聚合值。如果聚合函数没有使用   GROUP   BY   子句,则只为   SELECT   语句报告一个聚合值。  
   
  以下示例返回分类   2   中每种产品已销售的单位数量:  
   
  USE   Northwind  
  SELECT   OrdD.ProductID   AS   ProdID,  
                SUM(OrdD.Quantity)   AS   AmountSold  
  FROM   [Order   Details]   AS   OrdD   JOIN   Products   as   Prd  
            ON   OrdD.ProductID   =   Prd.ProductID  
            AND   Prd.CategoryID   =   2  
  GROUP   BY   OrdD.ProductID  
   
  下面是结果集:  
   
  ProdID             AmountSold      
  -----------   -----------    
  3                       328                    
  4                       453                    
  5                       298                    
  6                       301                    
  8                       372                    
  15                     122                    
  44                     601                    
  61                     603                    
  63                     445                    
  65                     745                    
  66                     239                    
  77                     791                    
   
  (12   row(s)   affected)  
   
  GROUP   BY   关键字后面跟着列的列表,称为分组列。GROUP   BY   子句限制结果集中的行;对于分组列中的每个非重复值只有一行。每个结果集行都包含与其分组列中特定值相关的汇总数据。  
   
  当   SELECT   语句中包含   GROUP   BY   关键字时,对可以在选择列表中指定的项有一些限制。在该选择列表中所允许的项目是:    
   
  分组列。  
   
   
  为分组列中的每个值只返回一个值的表达式,例如将列名作为其中一个参数的聚合函数。这些函数称为矢量聚合。    
  例如,TableX   包含:  
   
  ColumnA   ColumnB   ColumnC    
  -------   -------   -------    
  1   abc   5    
  1   def   4    
  1   ghi   9    
  2   jkl   8    
  2   mno   3    
   
   
   
  如果   ColumnA   是分组列,则结果集中将有两行,其中一行汇总值   1   的信息,而另一行汇总值   2   的信息。  
   
  如果   ColumnA   是分组列,要引用   ColumnB   或   ColumnC,这两列必须能为   ColumnA   中的每个值返回单个值的聚合函数中的参数。选择列表中包含诸如   MAX   (ColumnB)、SUM   (ColumnC)   或   AVG   (ColumnC)   之类的表达式是合法的:  
   
  SELECT   ColumnA,  
                MAX(ColumnB)   AS   MaxB,  
                SUM(ColumnC)   AS   SumC  
  FROM   TableX  
  GROUP   BY   ColumnA  
   
  该选择语句返回两行,为   ColumnA   中的每个唯一值各返回一行:  
   
  ColumnA           MaxB   SumC                  
  -----------   ----   -----------    
  1                       ghi     18                      
  2                       mno     11                      
   
  (2   row(s)   affected)  
   
  但是,选择列表中只包含   ColumnB   表达式是不合法的:  
   
  SELECT   ColumnA,  
                ColumnB,  
                SUM(ColumnC)   AS   SumC  
  FROM   TableX  
  GROUP   BY   ColumnA  
   
  由于   GROUP   BY   关键字只能返回一行,该行   ColumnA   中的值为   1,因此无法返回与   ColumnA   中的值   1   关联的   ColumnB   的三个值(abc、def   和   ghi)。  
   
  不能对   ntext、text、image   或   bit   列使用   GROUP   BY   或   HAVING   子句,除非它们所在的函数返回的值具有其它数据类型。这样的函数包括   SUBSTRING   和   CAST。  
  Top

13 楼zhang_yzy(六子儿)回复于 2004-12-03 12:55:22 得分 10

select   *    
      from   Tmytable   a  
      where   memo   =(select   top   1   memo   from   tmytable   b   where   a.code=b.code)Top

14 楼78777675(刀无名)回复于 2004-12-03 13:13:46 得分 10

select   *    
      from   tablename  
      where   autoid   in   (select   min(autoid)   from   test   group   by   code)Top

15 楼jbas(jbas)回复于 2004-12-03 13:18:02 得分 0

大家真是太热情了,谢谢大家了。Top

16 楼vinsonshen(为了明天)回复于 2004-12-03 13:20:23 得分 0

select   *   from   TMytable   a   where   not   exists   (select   *   from   TMytable   where   code=a.code   and   autoid<a.autoid)Top

相关问题

  • Group by
  • group by and distinct
  • 关于group by
  • group by 的问题
  • 关与GROUP BY !
  • group by 问题
  • distinct与group by?
  • group by的问题
  • select , count ,group by ????
  • google group? (100分!!!)

关键词

  • 函数
  • 聚合
  • 语句
  • code
  • 选择
  • autoid
  • columna
  • columnc
  • columnb
  • ordd

得分解答快速导航

  • 帖主:jbas
  • YangYuWeb
  • netcoder
  • skyboy0720
  • YangYuWeb
  • yelook
  • zhang_yzy
  • 78777675

相关链接

  • SQL Server类图书

广告也精彩

反馈

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