组合2个SQL查询

时间:2013-08-13 19:08:48

标签: sql sql-server-2008

我有一个复杂的查询,它将日期和时间变量从数值转换为更友好的用户输出。我有第二个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

但是这给了我这个错误:

  

Msg 102,Level 15,State 1,Line 1
  'm'附近的语法不正确。
  Msg 102,Level 15,State 1,Line 22
  'b'附近的语法不正确。

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

3 个答案:

答案 0 :(得分: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是什么?

答案 1 :(得分:0)

至少你在前几行有一个错误: 你有这个:选择* m.MediaId,n。注意注意 但是你需要这个:选择m.MediaId,n。注意注意,*

答案 2 :(得分:0)

我认为UNION或UNION ALL适合这个问题:

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

最佳