帮忙看看

nianran520 2009-10-16 10:27:49

select * from fty_sjgz t where ny in('2009-07','2009-08','2009-09')
and not exists
(select 1 from fty_sjgz where gz > t.gz and mem_id = t.mem_id and ny in('2009-07','2009-08','2009-09'))
order by mem_id

select mem_id,max(gz) as gz from fty_sjgz where ny in('2009-07','2009-08','2009-09')
group by mem_id

两种写法出来的行数不同,为什么?
...全文
100 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
nianran520 2009-10-16
  • 打赏
  • 举报
回复
无。[Quote=引用 13 楼 xman_78tom 的回复:]
gz 列有 null 值吗?
[/Quote]
xman_78tom 2009-10-16
  • 打赏
  • 举报
回复
gz 列有 null 值吗?
rock1937 2009-10-16
  • 打赏
  • 举报
回复
select mem_id,max(gz) as gz from fty_sjgz where ny in('2009-07','2009-08','2009-09')
group by mem_id

第二条写法是:
1,先找出ny等于那三个月的记录;
2,然后在才在上面找到的记录里进行查询,
3,所以第二条查询并非从整个表里查询.

第一条写法是:从整个表里查询
小宏 2009-10-16
  • 打赏
  • 举报
回复
好像应该是一样的啊。。。
nianran520 2009-10-16
  • 打赏
  • 举报
回复
和我第一种写法结果集一样的[Quote=引用 8 楼 wufeng4552 的回复:]
引用 7 楼 nianran520 的回复:
引用 4 楼 rucypli 的回复:
group by了 当然要少了

两种写法都是得出员工三个月内的最高工资,照理说结果集应该相同啊

2#的試下
[/Quote]
nianran520 2009-10-16
  • 打赏
  • 举报
回复
第二种写法就是少了十几条记录,郁闷
水族杰纶 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 nianran520 的回复:]
引用 4 楼 rucypli 的回复:
group by了 当然要少了

两种写法都是得出员工三个月内的最高工资,照理说结果集应该相同啊
[/Quote]
2#的試下
nianran520 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 rucypli 的回复:]
group by了 当然要少了
[/Quote]
两种写法都是得出员工三个月内的最高工资,照理说结果集应该相同啊
华夏小卒 2009-10-16
  • 打赏
  • 举报
回复

---类似的,好像一样的

if object_id('tb') is not null drop table tb
go
create table tb(id int,name varchar(10))
insert tb select
1,'A' UNION ALL SELECT
2,'A' UNION ALL SELECT
3,'B' UNION ALL SELECT
4,'B' UNION ALL SELECT
5,'B' UNION ALL SELECT
6,'C' UNION ALL SELECT
7,'C'



select * from tb t where name in('A','B')
and not exists
(select 1 from TB where ID > t.ID and NAME = t.NAME and name in('A','B'))
order by NAME

select max(ID)as ID , NAME from TB where name in('A','B')
group by NAME


id name
----------- ----------
2 A
5 B

(所影响的行数为 2 行)

ID NAME
----------- ----------
2 A
5 B

(所影响的行数为 2 行)

华夏小卒 2009-10-16
  • 打赏
  • 举报
回复
按理说,应该是一样的
rucypli 2009-10-16
  • 打赏
  • 举报
回复
group by了 当然要少了
nianran520 2009-10-16
  • 打赏
  • 举报
回复
第二种写法查询出来的结果集少了,不知怎么回事
水族杰纶 2009-10-16
  • 打赏
  • 举报
回复
--try
select * from (
select * from fty_sjgz t where ny in('2009-07','2009-08','2009-09'))a
where not exists
(select 1 from (
select * from fty_sjgz t where ny in('2009-07','2009-08','2009-09'))b where b.gz > a.gz and b.mem_id = a.mem_id )
order by mem_id
sgtzzc 2009-10-16
  • 打赏
  • 举报
回复
前面的写法可以得到fty_sjgz表所有的字段的值
后面的写法只能得到mem_id,和最大的gz
dawugui 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 nianran520 的回复:]
查到原因了 第一种写法如果出现员工两个月的工资相同,就会出来两条记录。
[/Quote]
恭喜
nianran520 2009-10-16
  • 打赏
  • 举报
回复
查到原因了 第一种写法如果出现员工两个月的工资相同,就会出来两条记录。

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧