我现在有一个查询,通过加入它们将两个查询合并为一个。此查询的代码是:
Select TOP 1 *
from Media
join (SELECT convert(date,
DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3)))) [Date],
convert(varchar(8), convert(time,
DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))))) [Time],
[Object] AS [Dataset],
SUBSTRING(Parms,1,6) AS [Media]
FROM (Select CONVERT(VARCHAR(18),[Date]) [Date],
[Object],
MsgId,
Parms
FROM JnlDataSection) A
Where MsgID = '325' AND
SUBSTRING(Parms,1,6) = 'V40449' ) b on Media.SerialNum = b.Media order by Date Desc, Time Desc;
我已将 Case When 子句添加到此查询中:
Select TOP 1 * from Media
join (SELECT convert(date,
DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3)))) [Date],
convert(varchar(8), convert(time,
DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))))) [Time],
CASE WHEN SiteId = '2' THEN 'IM'
WHEN SiteId ='3' THEN 'Recall'
WHEN SiteId ='4' THEN 'SM'
WHEN SiteId ='5' THEN 'VB'
WHEN SiteId ='6' THEN 'WM'
WHEN SiteId ='7' THEN 'IM - F'
WHEN SiteId ='8' THEN 'IM - C'
ELSE '' --Optional ELSE
END AS [Location],
[Object] AS [Dataset],
SUBSTRING(Parms,1,6) AS [Media]
FROM (Select CONVERT(VARCHAR(18),[Date]) [Date],
[Object],
MsgId,
Parms
FROM JnlDataSection) A
Where MsgID = '325' AND
SUBSTRING(Parms,1,6) = 'V40449' ) b on Media.SerialNum = b.Media order by Date Desc, Time Desc;
但是,当我运行此查询时,它给了我这个错误:
END AS [位置],
消息 156,级别 15,状态 1,第 22 行
关键字“AS”附近的语法不正确。
FROM JnlDataSection)消息 102,级别 15,状态 1,第 29 行
“A”附近的语法不正确。
Case
我想知道是否有人可以编辑我的第二个查询,以便它可以在子句中无错误地运行?
那将不胜感激。另请注意,SiteId
仅存在于媒体列中。我也在使用 SQL Server 2008。