SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


One To Many and Many to One Selection


One To Many and Many to One Selection

Author
Message
hoseam
hoseam
SSC-Addicted
SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)

Group: General Forum Members
Points: 472 Visits: 430
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 Code Error Code
Scheme 1 100002
Scheme 1 100003
Scheme 1 100004
Scheme 2 100002
Scheme 2 100006
Scheme 3 100001
Scheme 3 100002


please help.
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27643 Visits: 13268
Where do the schema_code come from?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
hoseam
hoseam
SSC-Addicted
SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)

Group: General Forum Members
Points: 472 Visits: 430
what do you mean?
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15306 Visits: 18612
From what I understand, this should get you started
Cool

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

Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27643 Visits: 13268
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?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
hoseam
hoseam
SSC-Addicted
SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)

Group: General Forum Members
Points: 472 Visits: 430
Yes Scheme Code is s_code is the table.
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27643 Visits: 13268
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;




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search