locked
help with sql query to add a column RRS feed

  • Question

  • i'd like some general help. i need to add a column from 2 tables to a query.

    so, i have a vendor id in the main query.

    i have the vendor suffix in another table.

    i have to combine the vendor number and vendor suffix to retrieve the vendor location in another table.

    what command or function would i use? a join adds records and i don't want to do that.

    so i would have vendor id in table1 (the query with all of the records and columns i need), vendor suffix from table2 and vendor location from table 3

    thanks


    Gary

    Friday, December 16, 2016 4:24 AM

All replies

  • Hi Gkeramidas,

    The most common way to achieve this is by using LEFT JOIN as shown below.

    SELECT t1.*
    	, t2.[vendor suffix]
    	, t3.[vendor location]
    FROM table1 t1
    LEFT JOIN table2 t2 ON t1.[vendor suffix id] = t2.[vendor suffix id]
    LEFT JOIN table3 t3 ON t1.[vendor id] = t3.[vendor id]
    					AND t1.[vendor suffix id] = t3.[vendor suffix id]
    

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 16, 2016 5:26 AM
  • ok, thanks sam. i'll give that a try. it's basically just 2 lookups that i need to do.

    Gary

    Friday, December 16, 2016 2:04 PM
  • You need to tell the relationships among those three tables.

    A Fan of SSIS, SSRS and SSAS

    Friday, December 16, 2016 3:41 PM
  • i wasn't able to get it to work. i ended up usingh 2 queries to do what i needed to do.

    Gary

    Sunday, December 18, 2016 11:05 PM
  • Hi Gary,

    You'll need to adjust the join conditions accordingly. Using subquery is not a good choice, especially when you need to get more than one column from a related table.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 19, 2016 8:26 AM
  • sam:

    the data is stored in an excel worksheet. so now, i just have 2 worksheets. not a big deal for me to do this. i have to create a report so i can just use vba to do what i need from here.


    Gary

    Monday, December 19, 2016 2:14 PM
  • Hi Gkeramidas,

    Thanks for the clarification. For professional support on VBA, you may visit https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=isvvba.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 20, 2016 8:28 AM