“Group By”和常用函数,疑问
“Group By”聚合函数应该怎样使?
实际问题一:
select w.MeterNo,w.DATime,m.MeterName,m.buildingNo from WarnUseEnergy w left join Meter m on w.MeterNo=m.MeterNo where DATime>'2012-04-01' group by w.MeterNo
这一句“SQL”语句想让其根据“w.MeterNo”分组,“Group by”后面应该写那些字段?
是不是像下面这样,把“Select w.MeterNo”去掉,然后“group by w.MeterNo,w.DATime,m.MeterName,m.buildingNo ”,如下:
select w.DATime,m.MeterName,m.buildingNo from WarnUseEnergy w left join Meter m on w.MeterNo=m.MeterNo where DATime>'2012-04-01' group by w.MeterNo,w.DATime,m.MeterName,m.buildingNo
问题二: 如果想看“Select w.MeterNo” 又想按其分组应该怎么办?
问题三:SQL中的常用函数都有那些,有没有相关的资料方便查阅的?
[解决办法]
实际问题一:(你没有聚合函数,这个分组统计没有实际意义)
select w.MeterNo,w.DATime,m.MeterName,m.buildingNo
from WarnUseEnergy w left join Meter m
on w.MeterNo=m.MeterNo
where DATime>'2012-04-01'
group by w.MeterNo,w.DATime,m.MeterName,m.buildingNo
问题二: 如果想看“Select w.MeterNo” 又想按其分组应该怎么办?
不知道你问的是不是这个意思:
select max(w.MeterNo),w.DATime,m.MeterName,m.buildingNo
from WarnUseEnergy w left join Meter m
on w.MeterNo=m.MeterNo
where DATime>'2012-04-01'
group by w.DATime,m.MeterName,m.buildingNo
select min(w.MeterNo),w.DATime,m.MeterName,m.buildingNo
from WarnUseEnergy w left join Meter m
on w.MeterNo=m.MeterNo
where DATime>'2012-04-01'
group by w.DATime,m.MeterName,m.buildingNo
select count(w.MeterNo),w.DATime,m.MeterName,m.buildingNo
from WarnUseEnergy w left join Meter m
on w.MeterNo=m.MeterNo
where DATime>'2012-04-01'
group by w.DATime,m.MeterName,m.buildingNo
select avg(w.MeterNo),w.DATime,m.MeterName,m.buildingNo
from WarnUseEnergy w left join Meter m
on w.MeterNo=m.MeterNo
where DATime>'2012-04-01'
group by w.DATime,m.MeterName,m.buildingNo
还是如下的意思?
--按某一字段分组取最大(小)值所在行的数据--(爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23于浙江杭州)/*数据如下:name val memoa 2 a2(a的第二个值)a 1 a1--a的第一个值a 3 a3:a的第三个值b 1 b1--b的第一个值b 3 b3:b的第三个值b 2 b2b2b2b2b 4 b4b4b 5 b5b5b5b5b5*/--创建表并插入数据:create table tb(name varchar(10),val int,memo varchar(20))insert into tb values('a', 2, 'a2(a的第二个值)')insert into tb values('a', 1, 'a1--a的第一个值')insert into tb values('a', 3, 'a3:a的第三个值')insert into tb values('b', 1, 'b1--b的第一个值')insert into tb values('b', 3, 'b3:b的第三个值')insert into tb values('b', 2, 'b2b2b2b2')insert into tb values('b', 4, 'b4b4')insert into tb values('b', 5, 'b5b5b5b5b5')go--一、按name分组取val最大的值所在行的数据。--方法1:select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name--方法2:select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)--方法3:select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name--方法4:select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name--方法5select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name/*name val memo ---------- ----------- -------------------- a 3 a3:a的第三个值b 5 b5b5b5b5b5*/--二、按name分组取val最小的值所在行的数据。--方法1:select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name--方法2:select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)--方法3:select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name--方法4:select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name--方法5select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name/*name val memo ---------- ----------- -------------------- a 1 a1--a的第一个值b 1 b1--b的第一个值*/--三、按name分组取第一次出现的行所在的数据。select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name/*name val memo ---------- ----------- -------------------- a 2 a2(a的第二个值)b 1 b1--b的第一个值*/--四、按name分组随机取一条数据。select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name/*name val memo ---------- ----------- -------------------- a 1 a1--a的第一个值b 5 b5b5b5b5b5*/--五、按name分组取最小的两个(N个)valselect a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.valselect a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.valselect a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name,a.val/*name val memo ---------- ----------- -------------------- a 1 a1--a的第一个值a 2 a2(a的第二个值)b 1 b1--b的第一个值b 2 b2b2b2b2*/--六、按name分组取最大的两个(N个)valselect a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.valselect a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.valselect a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name , a.val/*name val memo ---------- ----------- -------------------- a 2 a2(a的第二个值)a 3 a3:a的第三个值b 4 b4b4b 5 b5b5b5b5b5*/--七,如果整行数据有重复,所有的列都相同。/*数据如下:name val memoa 2 a2(a的第二个值)a 1 a1--a的第一个值a 1 a1--a的第一个值a 3 a3:a的第三个值a 3 a3:a的第三个值b 1 b1--b的第一个值b 3 b3:b的第三个值b 2 b2b2b2b2b 4 b4b4b 5 b5b5b5b5b5*/--在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。--创建表并插入数据:create table tb(name varchar(10),val int,memo varchar(20))insert into tb values('a', 2, 'a2(a的第二个值)')insert into tb values('a', 1, 'a1--a的第一个值')insert into tb values('a', 1, 'a1--a的第一个值')insert into tb values('a', 3, 'a3:a的第三个值')insert into tb values('a', 3, 'a3:a的第三个值')insert into tb values('b', 1, 'b1--b的第一个值')insert into tb values('b', 3, 'b3:b的第三个值')insert into tb values('b', 2, 'b2b2b2b2')insert into tb values('b', 4, 'b4b4')insert into tb values('b', 5, 'b5b5b5b5b5')goselect * , px = identity(int,1,1) into tmp from tbselect m.name,m.val,m.memo from( select t.* from tmp t where val = (select min(val) from tmp where name = t.name)) m where px = (select min(px) from( select t.* from tmp t where val = (select min(val) from tmp where name = t.name)) n where n.name = m.name)drop table tb,tmp/*name val memo---------- ----------- --------------------a 1 a1--a的第一个值b 1 b1--b的第一个值(2 行受影响)*/--在sql server 2005中可以使用row_number函数,不需要使用临时表。--创建表并插入数据:create table tb(name varchar(10),val int,memo varchar(20))insert into tb values('a', 2, 'a2(a的第二个值)')insert into tb values('a', 1, 'a1--a的第一个值')insert into tb values('a', 1, 'a1--a的第一个值')insert into tb values('a', 3, 'a3:a的第三个值')insert into tb values('a', 3, 'a3:a的第三个值')insert into tb values('b', 1, 'b1--b的第一个值')insert into tb values('b', 3, 'b3:b的第三个值')insert into tb values('b', 2, 'b2b2b2b2')insert into tb values('b', 4, 'b4b4')insert into tb values('b', 5, 'b5b5b5b5b5')goselect m.name,m.val,m.memo from( select * , px = row_number() over(order by name , val) from tb) m where px = (select min(px) from( select * , px = row_number() over(order by name , val) from tb) n where n.name = m.name)drop table tb/*name val memo---------- ----------- --------------------a 1 a1--a的第一个值b 1 b1--b的第一个值(2 行受影响)*/
[解决办法]
问题三:SQL中的常用函数都有那些,有没有相关的资料方便查阅的?
常用函数
http://www.yongfa365.com/Item/SQL-Server-Function.html
[解决办法]
建议你提供详细的资料:例如表的结构,表之间的关系,测试数据,相关算法及需要的结果。这样有助于我们理解你的意思,更主要的是能尽快让你获得答案或解决问题的方法。
[解决办法]
想起我初学group by的时候了.
LZ,你先从结果往回想.
假设你数据中的MeterNo有个值为'1001',而且出现过多次,像下面这样
MeterNo DATime
1001 2011-01-01
1002 2011-02-02
1001 2011-02-03
1001 2011-03-01
一旦group by MeterNo(按MeterNo列各个值来分组),即是要把结果集变成下面这样
MeterNo 列名?
1001 值?
1002 值?
显然,此时的?处已经不可能再是DATime值了.也不可能是你写的"select w.MeterNo,w.DATime,m.MeterName,m.buildingNo"这样,所以会有Group by要和聚合函数连用的约定.