Joining tables

  • Hi,

    I am trying to build sql statement, but got struck. Any inputs please?

  • Table ATable B
    Column1Column2ActiveColumn1Column2Count
    aC1YaC11
    aC2NaC22
    bC1NbC13
    bC2YbC24
    cC1YcC15
    cC2YcC26
    dC1NdC17
    dC2NdC28
    Output
    Make C2 = C1 
    Column1Column2ActiveCount
    aC1Y1
    bC1Y4
    cC1Y5
    cC1Y6
    dC1N7
    dC1N8

  • You're going to learn a lot more if you do your own homework.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • drew.allen - Tuesday, August 21, 2018 11:51 AM

    You're going to learn a lot more if you do your own homework.

    Drew

    Drew,  Thank You for the reply. It's not homework, I am new to SQL and started to learn from w3schools.
               This is something I got from one of the online tutorial and struck building it.

  • venuvedham16 - Tuesday, August 21, 2018 11:43 AM

    Hi,

    I am trying to build sql statement, but got struck. Any inputs please?

  • Table ATable B
    Column1Column2ActiveColumn1Column2Count
    aC1YaC11
    aC2NaC22
    bC1NbC13
    bC2YbC24
    cC1YcC15
    cC2YcC26
    dC1NdC17
    dC2NdC28
    Output
    Make C2 = C1 
    Column1Column2ActiveCount
    aC1Y1
    bC1Y4
    cC1Y5
    cC1Y6
    dC1N7
    dC1N8

  • I didn't understand your output. How count 2 and 3 are excluded from the expected results.

    Saravanan

  • saravanatn - Tuesday, August 21, 2018 12:22 PM

    venuvedham16 - Tuesday, August 21, 2018 11:43 AM

    Hi,

    I am trying to build sql statement, but got struck. Any inputs please?

  • Table ATable B
    Column1Column2ActiveColumn1Column2Count
    aC1YaC11
    aC2NaC22
    bC1NbC13
    bC2YbC24
    cC1YcC15
    cC2YcC26
    dC1NdC17
    dC2NdC28
    Output
    Make C2 = C1 
    Column1Column2ActiveCount
    aC1Y1
    bC1Y4
    cC1Y5
    cC1Y6
    dC1N7
    dC1N8

  • I didn't understand your output. How count 2 and 3 are excluded from the expected results.

    Count 2 and 3 are inactive(active = n) for a given column 1 and column 2.
    As we are changing c2=c1 in column 2, we are making sure active status of column 1& column 2 is consistent.

  • venuvedham16 - Tuesday, August 21, 2018 12:09 PM

    drew.allen - Tuesday, August 21, 2018 11:51 AM

    You're going to learn a lot more if you do your own homework.

    Drew

    Drew,  Thank You for the reply. It's not homework, I am new to SQL and started to learn from w3schools.
               This is something I got from one of the online tutorial and struck building it.

    If you got it from a tutorial, then it's homework. Go back to the lesson where you got this, and it should give you an idea about how to approach this, assuming that the tutorial is well written.

    If you are still having problems, show us what you've tried and we can help you.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • venuvedham16 - Tuesday, August 21, 2018 11:43 AM

    Hi,

    I am trying to build sql statement, but got struck. Any inputs please?

  • Table ATable B
    Column1Column2ActiveColumn1Column2Count
    aC1YaC11
    aC2NaC22
    bC1NbC13
    bC2YbC24
    cC1YcC15
    cC2YcC26
    dC1NdC17
    dC2NdC28
    Output
    Make C2 = C1 
    Column1Column2ActiveCount
    aC1Y1
    bC1Y4
    cC1Y5
    cC1Y6
    dC1N7
    dC1N8

  • Hi,
    Welcome to SQLcentral. Drew is absolutely right and try to solve on your own . Also post create and insert statement so will help create test data and consume time.

    create table table_a
    (
    Column1 varchar(200),
    Column2 varchar(200),
    active varchar(200)
      );

    insert into table_a values('a','c1','y');
    insert into table_a values('a','c2','n');
    insert into table_a values('b','c1','n');
    insert into table_a values('b','c2','y');
    insert into table_a values('c','c1','y');
    insert into table_a values('c','c2','y');
    insert into table_a values('d','c1','n');
    insert into table_a values('d','c2','n');

    create table table_b
    (
    Column1 varchar(200),
    Column2 varchar(200),
    count int
      );

    insert into table_b values('a','c1',1);
    insert into table_b values('a','c2',2);
    insert into table_b values('b','c1',3);
    insert into table_b values('b','c2',4);
    insert into table_b values('c','c1',5);
    insert into table_b values('c','c2',6);
    insert into table_b values('d','c1',7);
    insert into table_b values('d','c2',8);

    select Column1,Column2,active,count
    from
    (
    select distinct Column1,Column2,active,count,
    rank()over(partition by Column1,Column2 order by active desc) as rnk
    from
    (
    select act.Column1,
    case when act.Column2='c2' then 'c1' else act.column2 end as Column2,
    act.active,
    cnt.count
    from table_a act
    inner join table_b cnt
    on act.Column1=cnt.Column1
    and act.Column2=cnt.Column2
    )ranking
    )eli
    where rnk=1

    Output:

    Column1Column2activecount
    ac1y1
    bc1y4
    cc1y5
    cc1y6
    dc1n7
    dc1n8

    6 rows

    Saravanan

  • saravanatn, worked like a charm. Thank You!!

  • Viewing 8 posts - 1 through 7 (of 7 total)

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