我有以下SQL语句:
select top 1 scrr.name, sess.end_time
from tbl_session sess
inner join tbl_scripts scrr on sess.script_id = scrr.script_id
where end_time >= '5-May-2015 14:58:00'
and end_time < '06-May-2015 14:58:00'
and scrr.script_type in (1,3,4)
and sess.operator_id = 95
UNION
select top 1 scr.name, oh.end_time
from tbl_outbound_history oh
inner join tbl_outbound o on oh.outbound_id = o.outbound_id
inner join tbl_session ses on o.session_id = ses.id
inner join tbl_scripts scr on ses.script_id = scr.script_id
where oh.end_time >= '5-May-2015 14:58:00'
and oh.end_time < '06-May-2015 14:58:00'
and oh.start_time <> oh.end_time
and oh.user_id = 95
order by end_time数据没有变化,但每次结果是不同的。就好像命令被忽略了一样。
在本例中,第二个select没有返回任何内容,我试图简化SQL以尽量减少问题,但无法重现它。
如果我删除第二个选择,我每次都会得到相同的结果,所以关于一个联盟的事情可能会变成零和前1,尽管我不能让它用更简单的选择来完成。
以前有人遇到过这样的错误,如果是的话,有解决办法吗?
任何指向正确方向的指示都将不胜感激。
我在用:
Microsoft SQL Server 2012 (SP1) - 11.0.3000.0 (X64)
Oct 19 2012 13:38:57
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (Hypervisor)发布于 2015-06-09 08:31:03
您需要在外部查询中订购和使用top 1
select top 1 from (
select scrr.name, sess.end_time
from tbl_session sess
inner join tbl_scripts scrr on sess.script_id = scrr.script_id
where end_time >= '5-May-2015 14:58:00'
and end_time < '06-May-2015 14:58:00'
and scrr.script_type in (1,3,4)
and sess.operator_id = 95
UNION
select scr.name, oh.end_time
from tbl_outbound_history oh
inner join tbl_outbound o on oh.outbound_id = o.outbound_id
inner join tbl_session ses on o.session_id = ses.id
inner join tbl_scripts scr on ses.script_id = scr.script_id
where oh.end_time >= '5-May-2015 14:58:00'
and oh.end_time < '06-May-2015 14:58:00'
and oh.start_time <> oh.end_time
and oh.user_id = 95
) a
order by a.end_time我也相信,选角时间会很好
CAST('06-May-2015 14:58:00' as datetime)https://stackoverflow.com/questions/30726775
复制相似问题