Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

One To Many and Many to One Selection Expand / Collapse
Author
Message
Posted Monday, August 25, 2014 5:24 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 25, 2014 11:05 PM
Points: 188, Visits: 322
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.
Post #1607015
Posted Monday, August 25, 2014 5:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:16 AM
Points: 13,569, Visits: 11,378
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1607016
Posted Monday, August 25, 2014 5:29 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 25, 2014 11:05 PM
Points: 188, Visits: 322
what do you mean?
Post #1607017
Posted Monday, August 25, 2014 5:43 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:54 AM
Points: 2,420, Visits: 6,726
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
Post #1607021
Posted Monday, August 25, 2014 5:46 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:16 AM
Points: 13,569, Visits: 11,378
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1607022
Posted Monday, August 25, 2014 5:55 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 25, 2014 11:05 PM
Points: 188, Visits: 322
Yes Scheme Code is s_code is the table.
Post #1607024
Posted Monday, August 25, 2014 6:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:16 AM
Points: 13,569, Visits: 11,378
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1607041
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse