SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Determine the row of data in a table through SSIS
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

lahsiv2004
Starting Member

9 Posts

Posted - 06/13/2013 :  11:12:00  Show Profile  Reply with Quote
Hi,

I have a requirement based on 2 parts to determine a row of data in a table through SSIS :

a. Work out the LEG_SEQ_NBR of the VESSEL, VOYAGE and LEG already stored in table1.
To do this join table1 and join table2 through BL_ID column.

b. Work out the 2ndVESSEL, 2ndVOYAGE and 2ndLEG.

Once we have identified the LEG_SEQ_NBR of the VESSEL,VOYAGE, LEG already stored in table1 we need to add '1' to this value and then find that LEQ_SEQ_NBR in table2.

The DDL of table1 and table2 along with the test data are as below:


------------------------------------------------------------
CREATE TABLE [dbo].[table1](
[BL_ID] [decimal](10, 0) NOT NULL,
[VESSEL] [nvarchar](10) NULL,
[VOYAGE] [nvarchar](12) NULL,
[LEG] [nchar](3) NULL,
[BLNO] [nvarchar](17) NULL

) ON [PRIMARY]


CREATE TABLE [dbo].[table2](

[BL_ID] [numeric](10, 0) NULL,
[LEG_SEQ_NBR] [numeric](3, 0) NULL,
[VESSEL_CD] [varchar](10) NULL,
[VOYAGE_CD] [varchar](12) NULL,
[LEG_CD] [char](1) NULL,

) ON [PRIMARY]

INSERT INTO [table1]
VALUES('1','CEUR','032E','E','21')

INSERT INTO [table2]
VALUES('1','1','CEUR','032E','E')

INSERT INTO [table2]
VALUES('1','2','MARB','794S','S')
---------------------------------------------------------

For Example:

BLNO = 21 and BL_ID = 1

Current Vessel/Voyage/Leg in table1 = CEUR 032E E

LEG_SEQ_NBR of this Vessel/Voyage/Leg in table1 = '1'

Therefore if we add '1' to this value we'd be looking for LEG_SEQ_NBR '2' as the 2ndVESSEL,2ndVOYAGE,2ndLEG.

In this case that would = MARB 794S S

Does somebody pls know how this can be worked out through an SSIS job ?

Thanks.

Lamprey
Flowing Fount of Yak Knowledge

4364 Posts

Posted - 06/13/2013 :  12:44:57  Show Profile  Reply with Quote
I'm not sure I'm understanding the requirement, but maybe this will help:
SELECT
	*
FROM
	(
		SELECT 
			Table2.VESSEL_CD,
			Table2.VOYAGE_CD,
			Table2.LEG_CD,
			ROW_NUMBER() OVER (PARTITION BY Table2.BL_ID ORDER BY Table2.LEG_SEQ_NBR) AS RowNum
		FROM 
			Table1
		INNER JOIN
			table2
			ON Table1.BL_ID = Table2.BL_ID
	) AS T
WHERE 
	RowNum = 2
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 06/13/2013 :  12:55:57  Show Profile  Reply with Quote
Or this?


DECLARE @LegSN numeric;
DECLARE @BL_ID Numeric;
SELECT @LegSN =  [LEG_SEQ_NBR], @BL_ID = T1.BL_ID FROM Table2 T2 inner join Table1 T1 on T1.BL_ID = T2.BL_ID and VESSEL = VESSEL_CD and VOYAGE = VOYAGE_CD;
SELECT @LegSN;
SET @LegSN = @LegSN + 1;
SELECT * FROM Table2 where [LEG_SEQ_NBR] = @LegSN and BL_ID =  @BL_ID;

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 06/14/2013 :  01:17:39  Show Profile  Reply with Quote
As per what I understood this is what you're after


SELECT 
			Table2.VESSEL_CD,
			Table2.VOYAGE_CD,
			Table2.LEG_CD,
                        Table1.[VESSEL],
                        Table1.[VOYAGE],
                        Table1.[LEG],
                        t1.[VESSEL] AS [2ndVESSEL],
                        Table1.[VOYAGE] AS [2ndVOYAGE],
                        Table1.[LEG] AS [2ndLEG]

		FROM 
			Table1
		INNER JOIN
			table2
			ON Table1.BL_ID = Table2.BL_ID
                INNER JOIN 
                        table2 t1
                        ON t2.LEG_SEQ_NBR = table2.LEG_SEQ_NBR + 1
                INNER JOIN 
                        table1 t1
                        ON t1.BL_ID = t2.BL_ID


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

lahsiv2004
Starting Member

9 Posts

Posted - 09/09/2013 :  05:45:26  Show Profile  Reply with Quote
quote:
Originally posted by Lamprey

I'm not sure I'm understanding the requirement, but maybe this will help:
SELECT
	*
FROM
	(
		SELECT 
			Table2.VESSEL_CD,
			Table2.VOYAGE_CD,
			Table2.LEG_CD,
			ROW_NUMBER() OVER (PARTITION BY Table2.BL_ID ORDER BY Table2.LEG_SEQ_NBR) AS RowNum
		FROM 
			Table1
		INNER JOIN
			table2
			ON Table1.BL_ID = Table2.BL_ID
	) AS T
WHERE 
	RowNum = 2




Thanks very much for your code, it worked !! Sorry for the very late reply though.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000