One To Many and Many to One Selection

  • Hi, I have this table below and data;

    create table error_staging

    (ld_date datetime

    ,s_code varchar(10)

    ,error_code varchar(10)

    ,[mbr_no])

    insert into error_staging

    values(20140811, 'S000310D', '200016','A023539B'),(20140812, 'S000430D', '200016','A544348B'),(20140813, 'S001204D', '200016','A240640B'),

    (20140815, 'S000365D', '200160','A398944B'),(20140815, 'S000446D', '200160','A395200B'),(20140815, 'U001206D', '200160','A399932B'),

    (20140815, 'S000417D', '200122','A090716B'),(20140816, 'S000403D', '200122','A554911B'),(20140816, 'S000432D', '200122','A162545B'),

    (20140817, 'R000003D', '200022','A051194B'),(20140817, 'R000003D', '200160','A038118B'),(20140817, 'R000003D', '200016','A037861B'),

    (20140818, 'U000005D', '200123','A587925B'),(20140818, 'U000005D', '200173','A587925B'),(20140818, 'U000005D', '200094','A587926B')

    With this relationships:

    •One error_code can belong to one or more scheme_code

    •One scheme_code can have one or more error_code

    I need to get all error_code associated with one Schem;

    For example,

    Scheme CodeError Code

    Scheme 1 100002

    Scheme 1 100003

    Scheme 1 100004

    Scheme 2 100002

    Scheme 2 100006

    Scheme 3 100001

    Scheme 3 100002

    please help.

  • Where do the schema_code come from?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • what do you mean?

  • From what I understand, this should get you started

    😎

    USE tempdb;

    GO

    create table dbo.error_staging

    (ld_date INT

    ,s_code varchar(10)

    ,error_code varchar(10)

    ,[mbr_no] varchar(10));

    insert into dbo.error_staging

    values(20140811, 'S000310D', '200016','A023539B'),(20140812, 'S000430D', '200016','A544348B'),(20140813, 'S001204D', '200016','A240640B'),

    (20140815, 'S000365D', '200160','A398944B'),(20140815, 'S000446D', '200160','A395200B'),(20140815, 'U001206D', '200160','A399932B'),

    (20140815, 'S000417D', '200122','A090716B'),(20140816, 'S000403D', '200122','A554911B'),(20140816, 'S000432D', '200122','A162545B'),

    (20140817, 'R000003D', '200022','A051194B'),(20140817, 'R000003D', '200160','A038118B'),(20140817, 'R000003D', '200016','A037861B'),

    (20140818, 'U000005D', '200123','A587925B'),(20140818, 'U000005D', '200173','A587925B'),(20140818, 'U000005D', '200094','A587926B');

    SELECT

    ROW_NUMBER() OVER (PARTITION BY ES.s_code ORDER BY ES.ld_date) AS E_RID

    ,DENSE_RANK() OVER (ORDER BY ES.s_code) AS E_RNK

    ,ES.s_code

    ,ES.error_code

    FROM dbo.error_staging ES;

    DROP TABLE dbo.error_staging;

    Results

    E_RID E_RNK s_code error_code

    -------------------- -------------------- ---------- ----------

    1 1 R000003D 200022

    2 1 R000003D 200160

    3 1 R000003D 200016

    1 2 S000310D 200016

    1 3 S000365D 200160

    1 4 S000403D 200122

    1 5 S000417D 200122

    1 6 S000430D 200016

    1 7 S000432D 200122

    1 8 S000446D 200160

    1 9 S001204D 200016

    1 10 U000005D 200123

    2 10 U000005D 200173

    3 10 U000005D 200094

    1 11 U001206D 200160

  • hoseam (8/25/2014)


    what do you mean?

    The schema_code column in your result set, is this the s_code column from the sample data?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Yes Scheme Code is s_code is the table.

  • hoseam (8/25/2014)


    Yes Scheme Code is s_code is the table.

    OK, but none of the codes - error_code or schema_code - from the sample data appear in your result set.

    Why is this?

    To me it seems you could just query your table like this:

    SELECT DISTINCT schema_code, error_code

    FROM error_staging;

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

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

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