Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2011
    Posts
    13

    Unanswered: join between two tables

    Hi Pls tell me
    There are two tables A and B where asset tags are present, but in one table in rows and in another in column wise.

    for eg
    ASSet Tag
    SR-062009-00032966
    SR-062009-00032962
    SR-072009-00020572
    SR-072009-00020571
    SR-072009-00020585
    HH-092009-00038342




    Table B
    feild 1 -->Asset TAG
    Record 1-->SR-072009-00020572,SR-072009-00020571,SR-062009-00020685,SR-072009-00001592,SR-072009-00001376,SR-062009-00020683,SR-092009-00001617


    filed 2 --> Material code
    REcord 1-->121
    REcord 2-->123


    pls tell me the query so that asset tag of A matches with each and every asset tag table of B and output comes as




    Output
    Asset TAg -------- MAterial Code
    SR-062009-00032966
    SR-062009-00032962
    SR-072009-00020572 ------121
    SR-072009-00020571 -------121
    SR-072009-00020585
    Last edited by amurark; 12-20-12 at 12:46. Reason: there was one column left

  2. #2
    Join Date
    Apr 2012
    Posts
    213
    Hi,
    I have not found the asset tags SR-062009-00032966, SR-062009-00032962 and SR-072009-00020585 in Table B.
    Considering the data sample, the result you posted is correct?

  3. #3
    Join Date
    Oct 2011
    Posts
    13
    yes its correct because there will less reocrds in tbale B......

  4. #4
    Join Date
    Apr 2012
    Posts
    213
    I do not know if that's what you want, but try the query below:

    Code:
    with CTE_B as
    (
        SELECT 
            B.MaterialCode,
            O.splitdata 
        FROM
        (
             SELECT 
                 MaterialCode,
                 cast('<X>' + replace(AssetTag, ',', '</X><X>') + '</X>' as XML) as xmlfilter 
             from TableB 
        ) B
        CROSS APPLY
        ( 
             SELECT 
                 fdata.D.value('.', 'varchar(50)') as splitdata 
             FROM B.xmlfilter.nodes('X') as fdata(D)
        ) O
    )
    
    select a.ASSetTag, b.MaterialCode
    from TableA as a
    left join CTE_B as b
        on b.splitdata = a.ASSetTag
    Hope this helps.

  5. #5
    Join Date
    Sep 2006
    Location
    Surrey, UK
    Posts
    995
    Provided Answers: 2
    Alternatively, you could try normalising your data so that you don't have fields containing multiple values.
    10% of magic is knowing something that no-one else does. The rest is misdirection.

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Quote Originally Posted by weejas View Post
    Alternatively, you could try normalising your data so that you don't have fields containing multiple values.
    Oh my! What a radical idea!

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •