求一句SQL语句

jrl5365 2010-06-21 04:34:01
要求是

根据电子邮件不重复的,加载出表中用户名,电子邮件,时间,最后根据时间倒排
...全文
156 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
伴老思源 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 shuangbaojun 的回复:]
datediff(year,时间字段名,getDate()=0表示去年的时间
[/Quote]

少了个")"
gll062 2010-06-22
  • 打赏
  • 举报
回复
有点晕!!!!!!!!!!!!!!!!!!!!!!!!
Mr_Nice 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 julietcheng 的回复:]

SQL code
select 用户名,电子邮件,时间
from tb t
where not exists(select 1 from tb where 电子邮件=t.电子邮件 and 时间>t.时间)
order by 时间 desc
[/Quote]

select 用户名,电子邮件,时间
from tb t
where not exists(select 1 from tb where 电子邮件=t.电子邮件 and 时间>t.时间) and PerFlag_1=0
order by 时间 desc


lz给个数据,然后给个要求结果吧。这样说很难完全理解。
jrl5365 2010-06-22
  • 打赏
  • 举报
回复
再顶!!等待高手帮助
jrl5365 2010-06-22
  • 打赏
  • 举报
回复
这个方法还有点问题!因为PerFlag_1!=0的记录里也有可能包含上面select distinct Email from Person where PerFlag_1=0查出来的电子邮件,所以数字上会出现问题!
shuangbaojun 2010-06-22
  • 打赏
  • 举报
回复


select 用户名,最后登录时间,电子邮件
from 表名
where 电子邮件 in
(select distinct 电子邮件 from 表名 where datediff(year,时间字段名,getDate()=0)
order by 最后登录时间 desc
datediff(year,时间字段名,getDate()=0表示去年的时间
jrl5365 2010-06-22
  • 打赏
  • 举报
回复
谢谢大家的回复!可能我有一个地方说少了!!

去掉重复项的时候还要有一个条件就是Flag=0的去年重复所有电子邮件
shuangbaojun 2010-06-21
  • 打赏
  • 举报
回复

select 用户名,最后登录时间,电子邮件
from 表名
where 电子邮件 in (select distinct 电子邮件 from 表名)
order by 最后登录时间 desc
IvyAngta 2010-06-21
  • 打赏
  • 举报
回复
大家动作很快 UP
SQL77 2010-06-21
  • 打赏
  • 举报
回复
select * from 表名 where Email in(select Email from 表名 group by 用户名 having count(Email)=1) order by 时间 desc
jrl5365 2010-06-21
  • 打赏
  • 举报
回复
谢谢你的回答

[Quote=引用 5 楼 shixixi1987 的回复:]
select * from 表名 where Email in(select Email from 表名 group by Email having count(Email)=1) order by 时间 desc
[/Quote]

你的方法查出来是143884条记录了
shixixi1987 2010-06-21
  • 打赏
  • 举报
回复
select * from 表名 where Email in(select Email from 表名 group by Email having count(Email)=1) order by 时间 desc
jrl5365 2010-06-21
  • 打赏
  • 举报
回复
谢谢楼上的这些回答,我的意思再说明点!!

我现在有一个表中现在想根据电子邮件不重复的查出些用户的 用户名,电子邮件,最后登录时间,最后是根据这个最后登录时间进行倒排序

我用
select distinct Email from Person where PerFlag_1=0
查出是115971条记录


那我想用以楼上这些方法 查出来的记录 也应该是115971条的吧
SQL77 2010-06-21
  • 打赏
  • 举报
回复
SELECT * FROM TB T WHERE NOT EXISTS(SELECT 1 FROM TB WHERE 用户名=t.用户名 and 电子邮件<>t.电子邮件) order by 时间 DESC
美女她妈 2010-06-21
  • 打赏
  • 举报
回复
select 用户名,电子邮件,时间
from tb t
where not exists(select 1 from tb where 电子邮件=t.电子邮件 and 时间>t.时间)
order by 时间 desc
claro 2010-06-21
  • 打赏
  • 举报
回复
好专业 ‘加载’

帮顶

34,590

社区成员

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

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