求一条简单SQL语句

happy_sky 2008-07-11 08:42:46
表T:

name type price company time
(产品名称) (类型) (单价) (公司名称) (加入时间)

A 2 2 A 2008-7-11 09:16:07
A 2 2 A 2008-7-11 09:16:08
B 2 2 B 2008-7-11 09:16:03
C 2 2 C 2008-7-11 09:16:04
A 1 1 A 2008-7-11 09:16:09
D 1 1 D 2008-7-11 09:16:10
E 1 1 E 2008-7-11 09:16:06

要求取前3条记录 时间是最新的 单价等于1 类型等1 并且公司名称不能重复

结果:

D D1 1 D 2008-7-11 09:16:10
A A3 1 A 2008-7-11 09:16:09
E E1 1 E 2008-7-11 09:16:06

在线等,分不够再加~~
...全文
121 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ws_hgo 2008-07-13
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 happy_sky 的回复:]
zhangting1987 :

虽然我也是菜鸟 但我给别人答案的时候 首先会自己测试下
[/Quote]
LZ应该这样, 你做的很对
  • 打赏
  • 举报
回复
呵呵……不好意思啊
应该是这样:

select top 3 * from ab where type =1 and company in(
select t.company from (
select count(1) as a, company from ab where type=1 and price=1 group by company
) as t where t.a = 1) order by time desc
happy_sky 2008-07-11
  • 打赏
  • 举报
回复
zhangting1987 :

虽然我也是菜鸟 但我给别人答案的时候 首先会自己测试下
happy_sky 2008-07-11
  • 打赏
  • 举报
回复
wzy_love_sly :

你中午给我写过一个了 要是这么简单 我自己不就搞定了

我填了几条数据 显示是不正确的

这个表是随便写的

我用的正规的表测试的 显示是不正确的

roy_88 :

目前测试没有什么问题

再测试下

这回可不这么草率的结贴了

星期一结贴拉。。大家期待把。。
  • 打赏
  • 举报
回复

select top 3 * from 表 where company in(
select t.company from (
select count(1) as a, company from 表 where type=1 and price=1 group by company
) as t where t.a = 1) order by time desc
wzy_love_sly 2008-07-11
  • 打赏
  • 举报
回复
?怎么会不对
happy_sky 2008-07-11
  • 打赏
  • 举报
回复
-.- 没一个是正确的 中午的时候 忘了个条件 现在死活加不上去了 郁闷。。
中国风 2008-07-11
  • 打赏
  • 举报
回复
楼主举例时要举特殊的例子!!!
中国风 2008-07-11
  • 打赏
  • 举报
回复
--> --> (Roy)生成測試數據

set nocount on;
declare @T table([name] nvarchar(1),[type] int,[price] int,[company] nvarchar(1),[time] Datetime)
Insert @T
select N'A',2,2,N'A','2008-7-11 09:16:07' union all
select N'A',2,2,N'A','2008-7-11 09:16:08' union all
select N'B',2,2,N'B','2008-7-11 09:16:03' union all
select N'C',2,2,N'C','2008-7-11 09:16:04' union all
select N'A',1,1,N'A','2008-7-11 09:16:09' union all
select N'D',1,1,N'D','2008-7-11 09:16:10' union all
select N'E',1,1,N'E','2008-7-11 09:16:06'

Select
a.*
from
@T a
join
(select top 3 [company],max([time]) [time] from @T where [type]=1 and [price]=1 group by [company] order by [time] desc) b
on a.[company]=b.[company] and a.[time]=b.[time]
where
a.[type]=1 and a.[price]=1
order by a.[time] desc
wzy_love_sly 2008-07-11
  • 打赏
  • 举报
回复
name type price company time
(产品名称) (类型) (单价) (公司名称) (加入时间)

A 2 2 A 2008-7-11 09:16:07
A 2 2 A 2008-7-11 09:16:08
B 2 2 B 2008-7-11 09:16:03
C 2 2 C 2008-7-11 09:16:04
A 1 1 A 2008-7-11 09:16:09
D 1 1 D 2008-7-11 09:16:10
E 1 1 E 2008-7-11 09:16:06

要求取前3条记录 时间是最新的 单价等于1 类型等1 并且公司名称不能重复


select top 3 * from 表 t where not exists(
select 1 from 表 where company=t.company and time>t.time
) and type=1 and price=1
order by time desc
tianhuo_soft 2008-07-11
  • 打赏
  • 举报
回复

[code=SQL]
create table #t0711(name char,type int, price int,company char,time datetime)

insert into #t0711
select 'A',2,2,'A','2008-7-11 09:16:07' union all
select 'A',2,2,'A','2008-7-11 09:16:08' union all
select 'B',2,2,'B','2008-7-11 09:16:03' union all
select 'C',2,2,'A','2008-7-11 09:16:04' union all
select 'A',1,1,'A','2008-7-11 09:16:09' union all
select 'D',1,1,'D','2008-7-11 09:16:10' union all
select 'E',1,1,'E','2008-7-11 09:16:06'

select top 3 * from #t0711
where type=1 and price=1
order by time desc

[/code]
happy_sky 2008-07-11
  • 打赏
  • 举报
回复
结果应该是:

D 1 1 D 2008-7-11 09:16:10
A 1 1 A 2008-7-11 09:16:09
E 1 1 E 2008-7-11 09:16:06

34,590

社区成员

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

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