导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

问个group by 子句的问题!

BD131031 孤独分子  2008-01-17 11:28:53
select id,xm,sum(cj),dh from abc group by id,xm,dh
这样是对的,但是我想要的是分组的时候忽略id字段!仅以xm,dh来区别字段!
应该怎么写???
...全文
88 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ojuju10 2008-01-17

select b.name ,xm,sum(cj),dh from abc a ,aaa b
where a.id=b.id
group by a.id,xm,dh
回复
dawugui 2008-01-17
create table A(id  int,     name varchar(10))
insert into A values(1, '张三')
insert into A values(2, '李四')
create table B( id int, cj int, xm int)
insert into B values(1, 2, 3 )
insert into B values(1, 3, 3 )
insert into B values(1, 4, 4 )
insert into B values(2, 2, 5 )
insert into B values(2, 3, 6 )
go

select a.name , b.xm , sum(b.cj) cj
from a,b
where a.id = b.id
group by a.name , b.xm

drop table a,b

/*
name xm cj
---------- ----------- -----------
张三 3 5
张三 4 4
李四 5 2
李四 6 3

(所影响的行数为 4 行)
*/
回复
scow 2008-01-17
select (select distinct name from aaa where id=min(e.id)) as a,xm,sum(cj),dh from abc e group by xm,dh
回复
areswang 2008-01-17
select id,b.xm,b.cj sum(cj),b.dh from abc a
left join(select xm,sum(cj) cj,dh from abc b group by xm,dh)b
on a.xm = b.xm
and a.dh = b.dh
回复
dawugui 2008-01-17
select e.xm , sum(e.cj) , e.dh 
from aaa a , abc e
where a.id = e.id
group by e.xm , e.dh
回复
BD131031 2008-01-17
不好意思...确切的说,id是用来子查询的
select (select name from aaa where id=e.id) as a,xm,sum(cj),dh from abc e group by xm,dh
回复
select   min(id) as id,xm,sum(cj),dh   from   abc   group   by   xm,dh 

回复
月下之木 2008-01-17


select max(id),xm,sum(cj),dh from abc group by xm,dh
回复
areswang 2008-01-17
用子查询
回复
yang_dgut 2008-01-17
不要查詢 id 儸!
回复
JL99000 2008-01-17
select id,xm,sum(cj),dh from abc group by id,xm,dh
这样是对的,但是我想要的是分组的时候忽略id字段!仅以xm,dh来区别字段!
应该怎么写???
--
既然这样的话为什么还要id字段,他已经失去意义了
group by 分组查询的时候,只要from前面的字段没有包含在集合函数里,就要把他写在group by 后面
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告