Help joining two tables and matching on multiple conditions

  • Hello, I need help formulating the following.....

    I would like to join cwm_cust table to cwm_cut_upload in the following query.. That part I am good with. What I cannot figure out how to do is match the join on 2 criteria.

    The criteria are as follows:

    cwm_cust_upload columns company and customer number

    must match

    cwm_cust columns ccmpny and ccust#

    so , cwm_cust_upload company = cwm_cust ccmpny AND cwm_cust_upload [customer number] = cwm_cust ccust#

    SELECT [customer number],company,[customer name],division,[exp date],Revenue

    from cwm_cust_upload

    where [customer type] = 'commercial'

    and [customer name] <> 'resident'

    group by [customer number],company,[customer name],company,division,[exp date],Revenue

  • is it something like this you are after...

    SELECT ......

    FROM cwm_cust_upload

    INNER JOIN cwm_cust

    ON cwm_cust_upload.company = cwm_cust.ccmpny AND cwm_cust_upload.[customer number] = cwm_cust.ccust#

    WHERE.....

  • mbrady5 (1/27/2009)


    Hello, I need help formulating the following.....

    I would like to join cwm_cust table to cwm_cut_upload in the following query.. That part I am good with. What I cannot figure out how to do is match the join on 2 criteria.

    The criteria are as follows:

    cwm_cust_upload columns company and customer number

    must match

    cwm_cust columns ccmpny and ccust#

    so , cwm_cust_upload company = cwm_cust ccmpny AND cwm_cust_upload [customer number] = cwm_cust ccust#

    SELECT [customer number],company,[customer name],division,[exp date],Revenue

    from cwm_cust_upload

    where [customer type] = 'commercial'

    and [customer name] <> 'resident'

    group by [customer number],company,[customer name],company,division,[exp date],Revenue

    You say that you want to match two tables, cwm_cust to cwm_cut_upload, but the sample doesn't have two tables. Am I misunderstanding the explanation?

    If you're looking to JOIN the data:

    SELECT...

    FROM cwm_cust_upload ccu

    JOIN cwm_cust cc

    ON ccu.company = cc.ccmpny

    AND ccu.[customer number] = cc.[ccust#]

    WHERE ...

    This assumes an INNER JOIN. You might want to read up on INNER & OUTER JOIN in Books Online.

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

  • Thanks for responding.

    The 2 tables refernced are

    1. cwm_cust_upload

    2. cwm_cust

    Thanks again.

  • Sorry, I meant the example code. The explanation clearly mentioned two tables.

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

  • Thanks to all. I have successfully run my query.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply