2011-01-25 16 views
5

Dadas las siguientes dos tablas:SQL múltiple une

CREATE TABLE [dbo].[MTCorrelations] 
(
    [CorrelationID] [int] IDENTITY(1,1) NOT NULL, 
    [StockA] [nvarchar](5) NOT NULL, 
    [StockB] [nvarchar](5) NOT NULL, 
    [Correlation] [float] NOT NULL, 
    [LengthStr] [nvarchar](5) NOT NULL, 
    [Date] [datetime] NOT NULL 
) 

CREATE TABLE [dbo].[Industries] 
(
    [IndustryID] [int] IDENTITY(1,1) NOT NULL, 
    [Symbol] [nvarchar](5) NOT NULL, 
    [Sector] [nvarchar](50) NULL, 
    [Industry] [nvarchar](50) NULL 
) 

que estoy tratando de buscar las industrias de Stocka y StockB de la mesa de Industrias. Sin embargo, no sé cómo hacer múltiples uniones. Esto es lo mejor que se me ocurre:

SELECT TOP 1000 
[CorrelationID] 

     ,[StockA] 
     ,[StockB] 
     ,[Correlation] 
     ,b.Industry 
     ,c.Industry 
    FROM [MarketTopology].[dbo].[MTCorrelations] as a JOIN [MarketTopology].[dbo].[Industries] as b ON a.StockA = b.Symbol 
    AND a JOIN [MarketTopology].[dbo].[Industries] as c ON a.StockB = c.Symbol 

Recibo un error en el AND. ¿Cuál es la forma correcta de hacer esto?

Respuesta

2

Retire la AND a y sólo tiene la siguiente JOIN

SELECT TOP 1000 
      [CorrelationID], 
      [StockA], 
      [StockB], 
      [Correlation], 
      b.Industry, 
      c.Industry 
    FROM [MarketTopology].[dbo].[MTCorrelations] AS a 
    JOIN [MarketTopology].[dbo].[Industries] AS b 
    ON a.StockA = b.Symbol 
    JOIN [MarketTopology].[dbo].[Industries] AS c 
    ON a.StockB = c.Symbol 
3
SELECT TOP 1000 
     [CorrelationID] 
     ,[StockA] 
     ,[StockB] 
     ,[Correlation] 
     ,b.Industry 
     ,c.Industry 
FROM [MarketTopology].[dbo].[MTCorrelations] AS a 
JOIN [MarketTopology].[dbo].[Industries] AS b 
ON  b.Symbol = a.StockA 
JOIN [MarketTopology].[dbo].[Industries] AS c 
ON  c.Symbol = a.StockB 
2

Se puede utilizar:

SELECT TOP 1000 
     [CorrelationID], 
     [StockA], 
     [StockB], 
     [Correlation], 
     b.Industry, 
     c.Industry 
FROM [MarketTopology].[dbo].[MTCorrelations] as a 
      JOIN [MarketTopology].[dbo].[Industries] as b ON a.StockA = b.Symbol 
      JOIN [MarketTopology].[dbo].[Industries] as c ON a.StockB = c.Symbol 
2

probar este

SELECT TOP 1000 
    [CorrelationID] 
    ,[StockA] 
    ,[StockB] 
    ,[Correlation] 
    ,b.Industry 
    ,c.Industry 
FROM 
    [MarketTopology].[dbo].[MTCorrelations] as a 
    INNER JOIN [MarketTopology].[dbo].[Industries] as b 
     ON a.StockA = b.Symbol 
    INNER JOIN [MarketTopology].[dbo].[Industries] as c 
     ON a.StockB = c.Symbol 

Además, la OMI que debiera ge No utilice las convenciones de alias A, B, C y proporcione alias de tablas lo que signifique algo. De esta manera, no importa qué consulta esté viendo, Ind siempre puede ser de forma abreviada para Industries.

0

No es necesario el AND a antes de la segunda unión.

SELECT TOP 1000 [CorrelationID] 
       ,[StockA] 
       ,[StockB] 
       ,[Correlation] 
       ,b.Industry 
       ,c.Industry 
    FROM [MarketTopology].[dbo].[MTCorrelations] as a 
     JOIN [MarketTopology].[dbo].[Industries] as b 
      ON a.StockA = b.Symbol 
     JOIN [MarketTopology].[dbo].[Industries] as c 
      ON a.StockB = c.Symbol 
0

Su consulta tiene un error tipográfico. Cambiar a:

SELECT TOP 1000 
[CorrelationID] 

     ,[StockA] 
     ,[StockB] 
     ,[Correlation] 
     ,b.Industry 
     ,c.Industry 
    FROM [MarketTopology].[dbo].[MTCorrelations] as a JOIN [MarketTopology].[dbo].[Industries] as b ON a.StockA = b.Symbol 
    JOIN [MarketTopology].[dbo].[Industries] as c ON a.StockB = c.Symbol