标题:这个top查询该怎么写
只看楼主
jack_xu2046
Rank: 2
等 级:论坛游民
帖 子:72
专家分:11
注 册:2008-10-1
结帖率:81.82%
已结贴  问题点数:5 回复次数:4 
这个top查询该怎么写

我用这条语句时
SELECT LEFT(SN,6) as Model,count(*) as TestQty,avg(SpanTime) as TestingTime FROM FTNTRACK Where STATION='FT01'and result = 'true'group by LEFT([SN],6)
返回这个结果

但如果我想得到更精确一点,得到每个model前10片的 avg(SpanTime) ??
搜索更多相关主题的帖子: 查询 top 
2010-03-27 20:28
MIQIKing
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:36
专家分:106
注 册:2010-3-21
得分:5 
SELECT 10 top LEFT(SN,6) as Model,
count(*) as TestQty,
avg(SpanTime) as TestingTime
FROM FTNTRACK
Where STATION='FT01'
and result = 'true'
group by LEFT([SN],6)
2010-03-28 07:40
jack_xu2046
Rank: 2
等 级:论坛游民
帖 子:72
专家分:11
注 册:2008-10-1
得分:0 
SELECT TOP 5  * from FTNTRACK where STATION = 'FT01' AND LEFT(SN,6) = 'FWF50B' and result = 'true' ORDER BY ENDTIME DESC
SELECT TOP 5 * from FTNTRACK where STATION = 'FT01' AND LEFT(SN,6) = 'FG100C' and result = 'true' ORDER BY ENDTIME DESC
这两句分别运行都不出错,为什么之间添加union 后不能运行呢?


菜鸟中的菜鸟
2010-03-28 12:25
jack_xu2046
Rank: 2
等 级:论坛游民
帖 子:72
专家分:11
注 册:2008-10-1
得分:0 
SELECT LEFT(SN,6) as Model,count(*) as TestQty,avg(SpanTime) as TestingTime FROM (SELECT * FROM (SELECT TOP 10  SN,STATION,STARTTIME,ENDTIME,RESULT,SpanTime from FTNTRACK where STATION = 'FT01' AND LEFT(SN,6) = 'FWF50B' and result = 'true' ORDER BY ENDTIME desc) A  union all SELECT * FROM (SELECT TOP 10  SN,STATION,STARTTIME,ENDTIME,RESULT,SpanTime from FTNTRACK where STATION = 'FT01' AND LEFT(SN,6) = 'FG100C' and result = 'true' ORDER BY ENDTIME DESC) B
) C Where STATION='FT01'and result = 'true'group by LEFT([SN],6)

OK啦。。。。。。高

菜鸟中的菜鸟
2010-03-28 14:44
lshdn
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-4-6
得分:0 
如果用union all 的话,order by 只能在最后一个句子中用。
2010-04-06 13:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-300732-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.951871 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved