Ingram
Ingram

Reputation: 674

Using an Inner join select statement

I am fairly new to SQL. I have created an inner join between 2 tables and further created some where clauses to pull out the appropriate data. As I understand it I have used an inner join to connect 2 tables. What I am trying to do now is connect my resultant select query to another table. How would I do this?

SELECT  
t.[Type]



from [MITS].[dbo].[monster] t 

inner join (

SELECT [MITS].[dbo].[BROKERTABLE].[BrokerID]
      ,[MITS].[dbo].[CustomerRates].[MPAN_ID]
      ,[MITS].[dbo].[BROKERTABLE].[Commission_Rate]
      ,[MITS].[dbo].[BROKERTABLE].[Rate_From]
      ,[MITS].[dbo].[BROKERTABLE].[Rate_To]
      ,[MITS].[dbo].[CustomerRates].[From_Date]
      ,[MITS].[dbo].[CustomerRates].[To_Date]


  from [MITS].[dbo].[CustomerRates]

   Inner Join [MITS].[dbo].[BROKERTABLE]
  on [MITS].[dbo].[BROKERTABLE].[MPAN_ID] =
  [MITS].[dbo].[CustomerRates].[MPAN_ID]

  where

  [MITS].[dbo].[CustomerRates].[To_Date] <=
  [MITS].[dbo].[BROKERTABLE].[Rate_To]

   and
   convert(datetime,'01/11/2015',103)
   between convert(datetime,[MITS].[dbo].[CustomerRates].[From_Date],103) 
   and convert(datetime,[MITS].[dbo].[CustomerRates].[To_Date],103)

   ) d on t.MITID  = d.MPAT_ID

Upvotes: 0

Views: 11070

Answers (1)

Paul Maxwell
Paul Maxwell

Reputation: 35603

Add extra table into existing query: guess 1

select *
from atable a
inner join btable b on a.somecol = b.somecol
inner join extra_table t on a.somecol = t.somecol and b.somecol = t.somecol2

Add existing query to a table, method 1

select *
from extra_table t
inner join (
            your existing query here
           ) d on t.somecol = d.somecol

Add existing query to a table, method 2

select *
from (
            your existing query here
      ) d 
inner join extra_table t on d.somecol = t.somecol

Upvotes: 2

Related Questions