比对同表中不同天数,时间最大的数据。

细嗅蔷薇 2013-03-15 12:03:43
表结构:
create table test
(
id int primary key identity(1,1),
brandid int,
name varchar(50),
[datetime] datetime
)
数据:
insert into test values(1,'第一个品牌14日第一条','2013-03-14 14:14:14')
insert into test values(1,'第一个品牌14日第二条','2013-03-14 15:14:14')
insert into test values(1,'第一个品牌15日第一条','2013-03-15 11:14:14')
insert into test values(1,'第一个品牌15日第二条','2013-03-15 12:14:14')

insert into test values(2,'第二个品牌14日第一条','2013-03-14 14:14:14')
insert into test values(2,'第二个品牌14日第二条','2013-03-14 15:14:14')
insert into test values(2,'第二个品牌15日第一条','2013-03-15 13:14:14')
insert into test values(2,'第二个品牌15日第二条','2013-03-15 16:14:14')

想要查询出的效果:
1 第一个品牌14日第二条 2013-03-14 15:14:14.000 1 第一个品牌15日第二条 2013-03-15 12:14:14.000
2 第二个品牌14日第二条 2013-03-14 15:14:14.000 2 第二个品牌15日第二条 2013-03-15 16:14:14.000

现在可以并行显示,但是都很混乱,不是自己想要的结果,求教高手
...全文
269 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
细嗅蔷薇 2013-03-17
  • 打赏
  • 举报
回复
引用 6 楼 hjywyj 的回复:
引用 5 楼 lovesheng1212 的回复:引用 4 楼 hjywyj 的回复:SQL code?1234select t1.brandid,t1.name,t1.datetime,t2.brandid,t2.name,t2.datetime from test t1,test t2 where convert(varchar(10),dateadd(day,1, ……
例如:15日,16日,17日三天的数据 您这样会显示出同品牌15日与16日的对比数据,我只需要同品牌16日与17日的数据,这样子。 就是当前日期与前一天的数据,两天的,其余的不需要。
细嗅蔷薇 2013-03-17
  • 打赏
  • 举报
回复
引用 8 楼 hjywyj 的回复:
引用 7 楼 lovesheng1212 的回复:就是当前日期与前一天的数据,两天的,其余的不需要 我勒个去,你不会加个条件啊 where convert(varchar(10), t2.[datetime],120)=convert(varchar(10),getdate(),120)
我自己加过了。。。。。 and (convert(varchar(10), t1.[datetime],120)>(select convert(varchar(10),getdate() - 2,120) ) and convert(varchar(10), t2.[datetime],120)>(select convert(varchar(10),getdate() - 2,120) ))
  • 打赏
  • 举报
回复
引用 7 楼 lovesheng1212 的回复:
就是当前日期与前一天的数据,两天的,其余的不需要
我勒个去,你不会加个条件啊 where convert(varchar(10), t2.[datetime],120)=convert(varchar(10),getdate(),120)
  • 打赏
  • 举报
回复
引用 5 楼 lovesheng1212 的回复:
引用 4 楼 hjywyj 的回复:SQL code?1234select t1.brandid,t1.name,t1.datetime,t2.brandid,t2.name,t2.datetime from test t1,test t2 where convert(varchar(10),dateadd(day,1, t1.[datetime]),120)=conve……
后面掉了个条件
select t1.brandid,t1.name,t1.datetime,t2.brandid,t2.name,t2.datetime from test t1,test t2 
where convert(varchar(10),dateadd(day,1, t1.[datetime]),120)=convert(varchar(10), t2.[datetime],120)
and t1.brandid=t2.brandid
and not exists(select 1 from test t3 where convert(varchar(10), t1.[datetime],120)=convert(varchar(10), t3.[datetime],120) and t1.[datetime]<t3.[datetime] and t1.brandid=t3.brandid)
and not exists(select 1 from test t3 where convert(varchar(10), t2.[datetime],120)=convert(varchar(10), t3.[datetime],120) and t2.[datetime]<t3.[datetime] and t1.brandid=t3.brandid)
细嗅蔷薇 2013-03-16
  • 打赏
  • 举报
回复
引用 4 楼 hjywyj 的回复:
SQL code?1234select t1.brandid,t1.name,t1.datetime,t2.brandid,t2.name,t2.datetime from test t1,test t2 where convert(varchar(10),dateadd(day,1, t1.[datetime]),120)=convert(varchar(10), t2……
 select t1.brandid,t1.name,t1.datetime,t2.brandid,t2.name,t2.datetime from test t1,test t2 
where convert(varchar(10),dateadd(day,1, t1.[datetime]),120)=convert(varchar(10), t2.[datetime],120)
and t1.brandid=t2.brandid
and (convert(varchar(10), t1.[datetime],120)>(select convert(varchar(10),getdate() - 2,120) ) and convert(varchar(10), t2.[datetime],120)>(select convert(varchar(10),getdate() - 2,120) ))
and not exists(select 1 from test t3 where convert(varchar(10), t1.[datetime],120)=convert(varchar(10), t3.[datetime],120) and t1.[datetime]<t3.[datetime])
and not exists(select 1 from test t3 where convert(varchar(10), t2.[datetime],120)=convert(varchar(10), t3.[datetime],120) and t2.[datetime]<t3.[datetime])
更改了一下 加上了只对同品牌对比和当前日期与昨天的对比,您觉得这样些合适么
细嗅蔷薇 2013-03-15
  • 打赏
  • 举报
回复
就是查询出来的数据 总是显示今天最后一条数据,和前一天最后一条数据。 其实想要的就是用查询出来的数据做对比, 在一行中显示 今天最后一条数据和前一天最后一行数据
  • 打赏
  • 举报
回复
select t1.brandid,t1.name,t1.datetime,t2.brandid,t2.name,t2.datetime from test t1,test t2 
where convert(varchar(10),dateadd(day,1, t1.[datetime]),120)=convert(varchar(10), t2.[datetime],120)
and not exists(select 1 from test t3 where convert(varchar(10), t1.[datetime],120)=convert(varchar(10), t3.[datetime],120) and t1.[datetime]<t3.[datetime])
and not exists(select 1 from test t3 where convert(varchar(10), t2.[datetime],120)=convert(varchar(10), t3.[datetime],120) and t2.[datetime]<t3.[datetime])
细嗅蔷薇 2013-03-15
  • 打赏
  • 举报
回复
有人来帮忙看看 吗?
细嗅蔷薇 2013-03-15
  • 打赏
  • 举报
回复
木有人么?

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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