Link to home
Start Free TrialLog in
Avatar of ExpertSkills
ExpertSkills

asked on

SSIS package and using a Lookup tranformation

I am currently working on a SSIS package and using a Lookup tranformation to output a value.
               
Lookup transformation uses a custom query as follows but the query returns mulitple results.

select * from (SELECT Account_Main_Financial_Id,
       Account_Main_Start_Code,
       Account_Main_End_Code,
       Account_Main_Financial_Level_Nbr
FROM   cdr.Account_Main_Financial
WHERE  Account_Main_Financial_Id > 0
       AND current_usage_ind = 1
       ) [refTable]
WHERE 4390 BETWEEN [refTable].[Account_Main_Start_Code] AND [refTable].[Account_Main_End_Code]
AND [refTable].Account_Main_Financial_Level_Nbr =
        ( SELECT MAX(Account_Main_Financial_Level_Nbr) FROM cdr.Account_Main_Financial
                WHERE [refTable].Account_Main_Financial_Id =Account_Main_Financial_Id )
               
Account_Main_Financial_Id        Account_Main_Start_Code        Account_Main_End_Code        Account_Main_Financial_Level_Nbr
1        0        4999        2
2        0        9999        1
20        4000        4499        3
47        4390        4399        4


I tried to use a parameter to resolve this issue but parameters in a sub query are not permitted in the custom query?

select * from (SELECT Account_Main_Financial_Id,
       Account_Main_Start_Code,
       Account_Main_End_Code,
       Account_Main_Financial_Level_Nbr
FROM   cdr.Account_Main_Financial
WHERE  Account_Main_Financial_Id > 0
       AND current_usage_ind = 1
       ) [refTable]
WHERE ? BETWEEN [refTable].[Account_Main_Start_Code] AND [refTable].[Account_Main_End_Code]
AND [refTable].Account_Main_Financial_Level_Nbr =
        ( SELECT MAX(Account_Main_Financial_Level_Nbr) FROM cdr.Account_Main_Financial
                WHERE ? BETWEEN [Account_Main_Start_Code] AND [Account_Main_End_Code] )
               

Esentially, I need to return the Max(Account_Main_Financial_Level_Nbr) so that only Account_Main_Financial_Id=47 is returned.

Unfortunately, its not 1=1 and there are multiple rows being returned. Is there an easier way to do this?

Many thanks in anticipation.
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Something like this perhaps:
SELECT  a.Account_Main_Financial_Id,
        a.Account_Main_Start_Code,
        a.Account_Main_End_Code,
        d.Account_Main_Financial_Level_Nbr
FROM    cdr.Account_Main_Financial a
        INNER JOIN (SELECT  Account_Main_Financial_Id,
                            MAX(Account_Main_Financial_Level_Nbr) Account_Main_Financial_Level_Nbr
                    FROM    cdr.Account_Main_Financial
                    GROUP BY Account_Main_Financial_Id
                   ) d ON a.Account_Main_Financial_Id = d.Account_Main_Financial_Id
WHERE   a.Account_Main_Financial_Id > 0
        AND a.current_usage_ind = 1
        AND 4390 BETWEEN a.Account_Main_Start_Code AND a.Account_Main_End_Code

Open in new window

Avatar of ExpertSkills
ExpertSkills

ASKER

Thanks for your solution but when executing, still get multiple rows returned;

Account_Main_Financial_Id        Account_Main_Start_Code        Account_Main_End_Code        Account_Main_Financial_Level_Nbr
1        0        4999        2
2        0        9999        1
20        4000        4499        3
47        4390        4399        4
If all you want is a single row returned then do something like this:
;WITH MyCTE AS (
    SELECT  Account_Main_Financial_Id,
            Account_Main_Start_Code,
            Account_Main_End_Code,
            Account_Main_Financial_Level_Nbr,
            ROW_NUMBER() OVER (ORDER BY Account_Main_Financial_Level_Nbr DESC) Row
    FROM    cdr.Account_Main_Financial a
    WHERE   Account_Main_Financial_Id > 0
            AND current_usage_ind = 1
            AND 4390 BETWEEN Account_Main_Start_Code AND Account_Main_End_Code
             )
SELECT  Account_Main_Financial_Id,
        Account_Main_Start_Code,
        Account_Main_End_Code,
        Account_Main_Financial_Level_Nbr
FROM    MyCTE
WHERE   Row = 1

Open in new window

That works a treat in Query Analyser but not in the SSIS Lookup Transformation; Advanced >  Modify the SQL Statement.

Any way to incorporate this into the Lookup transformation?
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Appreciate your help! Will try to get the code integrated into the component!