我现在有一个查询,通过加入它们将两个查询合并为一个。此查询的代码是:
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;
然而,当我运行此查询时,它会给我这个错误:
结束[位置],
第15行,第15行,第1行,第22行 关键字“AS”附近的语法不正确。
FROM JnlDataSection)A Msg 102,Level 15,State 1,Line 29
“A”附近的语法不正确。
我想知道是否有人可以编辑我的第二个查询,以便它可以使用Case
子句运行无错误?
非常感谢。另请注意,SiteId
仅出现在“媒体”列中。我也在使用SQL Server 2008.