0

Okay, I'm relatively new to the more advanced uses of SQL Server.

I have several tables that I need to gather informations from, and several of these tables links to other tables where I need a specific information. As a result, I just want one row with all the information, preferential named with aliases.

For example:

Tab_Transcoders:

ID, VideoCamID, InputStreamID, OutputStreamID.

where InputStream links to another table where I need the row of the matching ID, where in this row are other ID's (e.g. StreamType_ID that belongs to a third table containing ID_StreamType and Description etc.)

Same with OutputStreamID, same with VideoCamID.

In the end, I need a row containing for example:

ID, VideoCamID, InputStreamID, InputStreamType, InputStreamTypeDesc, 
OutputStreamID, OutputStreamType, OutputStreamDesc, VideoCamID, etc. etc. etc.

It is important for me that I can set aliases, as for example InputStreamID & OutputStreamID links to the same table where all my Streams are listed (with IP's, Descs..)

I can accomplish this with doing like 100 SELECTS & SUBSELECTS, but I don't think that's an appropriate way.

I read some informations about things like CURSOR, UNION, FETCH, JOIN etc. etc.. but I don't know which one I have to use for my purpose.

eli

1

3 Answers 3

0

I think you want something like the following....

Select 
      t.ID,
      t.VideoCamID, 
      i.InputStreamID, 
      is.StreamType as InputStreamType, 
      is.StreamDesc as InputStreamDesc,
      o.OutputStreamID, 
      os.StreamType as OutputStreamType,
      os.StreamDesc as OutputStreamDesc,
      v.VideoCamID

from 
     Tab_Transcoders t
     inner join InputStreams i on i.InputStreamID=t.InputStreamId
     inner join Streams is on is.StreamId=i.StreamId
     inner join OutputStreams o on o.OutputStreamId=t.OutputStreamId
     inner join Streams os on os.StreamID=o.StreamId
     inner join VideoCams v on v.VideoCamId=t.VideoCamID
1
  • I'm not done writing my 500000 lines query, but it seems your suggestion did the trick. Thank you very much! Jul 24, 2012 at 11:27
0

If there is a defined relationship between your tables, then Use Join. e.g Customer Order

Order will have customer Id

Select Order.ID,Order.Quantity, Order.CustomerId, Customer.FullName, Customer.Address
From Orders Order
Join
Customer
On
Order.CustomerId = Customer.CustomerId

First start by getting data from two tables using the join and then if it works as per your requirement, add another required table in the join.

Read about SQl JOINS.. It is fairly simple.

0

I will recommend reading you some of the articles around CTE aka Common Table Expression.

Refer http://msdn.microsoft.com/en-us/library/ms190766%28v=sql.105%29.aspx.

Apart from this never use subqueries. Try to use inner join / any other join if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.