我有两个查询从不同的表中返回单个值(并且不以任何方式通过关系加入),并且我尝试将两个查询组合在一起。输出到一行,但我得到语法错误。这就是我正在尝试的:
SELECT
(SELECT Timestamp As StartDate
FROM Events
WHERE Description = 'Inserted') AS StartDate,
(SELECT TOP (1) Timestamp As EndDate
FROM DataStore
ORDER BY Timestamp DESC) AS EndDate
这就是我回来的原因:
解析查询时出错。 [令牌行号= 2,令牌 line offset = 2,Token in error = SELECT]
查询1本身的回报:" 2015-06-10 11:43:34.000"和查询2返回:" 2015-06-11 13:59:47.000"
我想返回一行有两列,查询1的输出为" StartDate"列,以及查询2的输出为" EndDate"列。
答案 0 :(得分:1)
与第二个查询不同,第一个查询可能返回2个或更多值。
尝试在第一个查询中添加TOP (1)
,因为我认为您只是在寻找最佳结果。
SELECT
(SELECT TOP (1) Timestamp As StartDate FROM Events WHERE Description = 'Inserted') AS StartDate,
(SELECT TOP (1) Timestamp As EndDate FROM DataStore Order by Timestamp DESC) AS EndDate
请参阅此SQL Fiddle link了解我所做的测试。
答案 1 :(得分:0)
declare @q1 as varchar(max)
declare @q2 as varchar(max)
SELECT @q1= Timestamp FROM Events WHERE Description = 'Inserted'
SELECT TOP (1) @q2= Timestamp FROM DataStore Order by Timestamp DESC
select @q1 as StartDate, @q2 as EndDate
答案 2 :(得分:0)
SQL CE不支持嵌套这样的SELECT语句,因此您必须使用两个SELECT语句并使用UNION或两次调用ExecuteNonQuery。
答案 3 :(得分:-1)
试试这个:
SELECT StartDate.StartDate, EndDate.EndDate
FROM
(SELECT Timestamp As StartDate FROM Events WHERE Description = 'Inserted') AS StartDate
CROSS JOIN
(SELECT TOP (1) Timestamp As EndDate FROM DataStore Order by Timestamp DESC) AS EndDate