|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, November 21, 2012 6:57 AM
Points: 18,
Visits: 15
|
|
Hi,
CREATE TABLE [T_Master_lookup]( [F_lookup_id] [int] NOT NULL, [F_data_id] [int] NOT NULL, [F_looup_desc] [nchar](10) NOT NULL ) ON [PRIMARY]
GO
INSERT INTO [T_Master_lookup] ([F_lookup_id],[F_data_id],[F_looup_desc]) VALUES (1001,1,'A'); INSERT INTO [T_Master_lookup] ([F_lookup_id],[F_data_id],[F_looup_desc]) VALUES (1002,2,'B'); INSERT INTO [T_Master_lookup] ([F_lookup_id],[F_data_id],[F_looup_desc]) VALUES (1003,3,'C'); INSERT INTO [T_Master_lookup] ([F_lookup_id],[F_data_id],[F_looup_desc]) VALUES (1004,4,'D'); INSERT INTO [T_Master_lookup] ([F_lookup_id],[F_data_id],[F_looup_desc]) VALUES (1005,5,'E');
CREATE TABLE [T_Lookup_Ref]( [F_ref_id] [int] NOT NULL, [F_data_id] [int] NOT NULL, [F_ref_desc] [nchar](10) NOT NULL ) ON [PRIMARY]
INSERT INTO [T_Lookup_Ref]([F_ref_id],[F_data_id],[F_ref_desc])VALUES (2001,1,'Desc 1'); INSERT INTO [T_Lookup_Ref]([F_ref_id],[F_data_id],[F_ref_desc])VALUES (2002,2,'Desc 2'); INSERT INTO [T_Lookup_Ref]([F_ref_id],[F_data_id],[F_ref_desc])VALUES (2003,3,'Desc 3'); INSERT INTO [T_Lookup_Ref]([F_ref_id],[F_data_id],[F_ref_desc])VALUES (2004,4,'Desc 4'); INSERT INTO [T_Lookup_Ref]([F_ref_id],[F_data_id],[F_ref_desc])VALUES (2005,5,'Desc 5'); INSERT INTO [T_Lookup_Ref]([F_ref_id],[F_data_id],[F_ref_desc])VALUES (2006,1,'Desc 6'); INSERT INTO [T_Lookup_Ref]([F_ref_id],[F_data_id],[F_ref_desc])VALUES (2007,2,'Desc 7'); INSERT INTO [T_Lookup_Ref]([F_ref_id],[F_data_id],[F_ref_desc])VALUES (2008,1,'Desc 8'); INSERT INTO [T_Lookup_Ref]([F_ref_id],[F_data_id],[F_ref_desc])VALUES (2009,2,'Desc 9'); INSERT INTO [T_Lookup_Ref]([F_ref_id],[F_data_id],[F_ref_desc])VALUES (2010,3,'Desc 10');
My output must be like that all the records in the T_Master_lookup must be shown like the below
2001 1 'Desc 1' 'A' 2002 2 'Desc 2' 'B' 2003 3 'Desc 3' 'C' 2004 4 'Desc 4' 'D' 2005 5 'Desc 5' 'E'
For the second set of records
2006 1 'Desc 6' A 2007 2 'Desc 7' B - - - C - - - D - - - E
similarly for the thrid
2008 1 'Desc 8' A 2009 2 'Desc 9' B 2010 3 'Desc 10' C - - - D - - - E
The left outer join or the cross join does not produce my excepted result. Please suggest.
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 2:11 AM
Points: 9,377,
Visits: 6,473
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 11:00 AM
Points: 2,543,
Visits: 4,384
|
|
How will you differentiate between first set of:
- - - D - - - E and the second set of - - - D - - - E
What makes them first or second? You need some other key as there is nothing to enforce its order...
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 11:00 AM
Points: 2,543,
Visits: 4,384
|
|
I honestly believe that you are trying to do something wrong, but... Here we go:
;WITH cte_ref AS ( SELECT R.*, ROW_NUMBER() OVER (PARTITION BY F_data_id ORDER BY F_ref_id) N FROM [T_Lookup_Ref] R ) , cte_set AS ( SELECT S.N ,M.F_data_id ,M.F_looup_desc FROM T_Master_lookup M CROSS JOIN (SELECT DISTINCT N FROM cte_ref) S(N) )
SELECT R.F_ref_id ,R.F_data_id ,R.F_ref_desc ,M.F_looup_desc FROM cte_set M LEFT JOIN cte_ref R ON R.N = M.N AND R.F_data_id = M.F_data_id ORDER BY M.N, M.F_looup_desc, R.[F_ref_id]
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, November 21, 2012 6:57 AM
Points: 18,
Visits: 15
|
|
Many thanks for the reply
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 11:00 AM
Points: 2,543,
Visits: 4,384
|
|
Please note, that the order of output is a bit different...
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|