0

我有一个复杂的查询,将日期和时间变量从数值转换为更用户友好的输出,并且我有第二个 sql 查询,它是两个表之间的简单连接。我正在尝试将这两个 SQL 查询合并为一个,这样我可以更高效地访问所有信息。

这是我的第一个查询:

   Select * 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) like 'V754%') b on Media.SerialNum = b.Media order by Date  Desc, Time Desc

这是我的第二个查询:

   SELECT SerialNum, m.MediaId, n.Note  as Note 
   from Media m join Note n on (m.NoteId = n.NoteId)
   where SerialNum like 'V754%';

我试过的:

   Select * m.MediaId, n.Note  as Note from Media m join Note n on (m.NoteId =  n.NoteId)
   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) like 'V754%') b on Media.SerialNum = b.Media order by Date  Desc, Time Desc

但这给了我这个错误:

消息 102,级别 15,状态 1,第 1 行
'm' 附近的语法不正确。
消息 102,级别 15,状态 1,第 22 行
'b' 附近的语法不正确。

请注意,我使用的是 SQL Server 2008 Management Studio。此外,我对更改查询的任何列或数据操作不感兴趣,因为它已经适当地满足了我的需求。我只是在寻找在 SQL 中无错误运行的组合查询。

4

3 回答 3

1

在“您尝试过”的代码中,语法错误...尝试下面的代码,但我只修复了错误。

SELECT   m.MediaId,
             n.Note AS Note
    FROM     Media AS m
             INNER JOIN
             Note AS n
             ON (m.NoteId = n.NoteId)
             INNER 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)))) AS [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))))) AS [Time],
                     [Object] AS [Dataset],
                     SUBSTRING(Parms, 1, 6) AS [Media]
              FROM   (SELECT CONVERT (VARCHAR (18), [Date]) AS [Date],
                             [Object],
                             MsgId,
                             Parms
                      FROM   JnlDataSection) AS A
              WHERE  MsgID = '325'
                     AND SUBSTRING(Parms, 1, 6) LIKE 'V754%') AS b
             ON m.SerialNum = b.Media
    ORDER BY Date DESC, Time DESC;

与前两个查询的联合...

SELECT  Media.SerialNum ,
        Media.MediaId ,
        'Note' AS Notes
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) LIKE 'V754%'
             ) b ON m.SerialNum = b.Media
UNION
SELECT  SerialNum ,
        m.MediaId ,
        n.Note AS Notes
FROM    Media m
        JOIN Note n ON ( m.NoteId = n.NoteId )
WHERE   SerialNum LIKE 'V754%'

但是,我不知道Note第一个查询中的数据类型是什么?

于 2013-08-13T19:16:24.380 回答
0

至少你在前几行有一个错误: 你有这个: Select * m.MediaId, n.Note as Note 但你需要这个: Select m.MediaId, n.Note as Note, *

于 2013-08-13T19:16:54.223 回答
0

我相信 UNION 或 UNION ALL 适合这个问题:

http://www.w3schools.com/sql/sql_union.asp

最好的

于 2013-08-13T19:17:42.893 回答