dynamic sql query

  • hi
    i am trying to create dynamic query for below code
    select  *  from ac_mast where opendate between '01-jan-2019' and '30-jan-2019'

  • hi
    please help 
    i am trying to use pivot clause in a query
    declare @sql nvarchar(max);
    declare @frdate datetime =;01-jan-2019',@todate datetime ='31-jan-2019' 
    set @sql= select  entrydate,id,amt from xtab where entrydate between @frdate and @todate;

    error is
    Conversion failed when converting date and/or time from character string.

  • There's a few problems with that SQL you've posted. Firstly you have:
    declare @frdate datetime =;01-jan-2019',@todate datetime ='31-jan-2019'
    You have a wayward semicolon before your first date's value, and (depending on your language) those literal strings might fail to convert. I would suggest using:
    DECLARE @frdate date = '20190101', @todate date = '20190131';
    The dates above use the ISO format, which is an unambiguous format. Regardless of the datatype (as datetime and date/datetime2/datetimeoffset behave differently) and language, the value will always be correctly interpreted.

    Next, there's actually nothing dynamic about your SQL. The statement below will just work:
    SELECT entrydate,
           id,
           amt
    FROM xtab
    WHERE entrydate BETWEEN @frdate AND @todate;

    If you have dynamic SQL, that would imply you have dynamic objects; do we have the full picture here? If not, could you share the rest of it please?

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • hi
    thanks 
    here is my whole code
    i have masked table names  on security grounds
    --------------------------

    declare @query nvarchar(max),@col nvarchar(max);
    declare @frdate datetime='01-jan-2019',@todate datetime ='31-jan-2019';
    select @col=

    STUFF((SELECT ',' + QUOTENAME(descr)
           from (
             SELECT A.TransctionAccountId AS ACCOUNTID,
                    a.FcyTransactionAmount AS aMT,
                    a.balanceflowtype,
                    postdate,
                    b.branchcode,
                    b.productcode,
                    b.accountnumber,
                    sum(case when a.drcr=2 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) credit,
                    sum(case when a.drcr=1 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) debit,
                    CASE
                    WHEN A.BALANCEFLOWTYPE=    cast(31 as varchar(2) )    THEN    'Disbursement Debit'
                    WHEN A.BALANCEFLOWTYPE=    cast(32    as varchar(2) ) THEN    'Principal Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(33    as varchar(2) ) THEN    'Charges Debit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(34    as varchar(2) ) THEN    'Charges Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(35    as varchar(2) ) THEN    'Interest Debit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(36    as varchar(2) ) THEN    'Interest Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(37    as varchar(2) ) THEN    'Penal Interest Debit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(38    as varchar(2) ) THEN    'Penal Interest Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(39    as varchar(2) ) THEN    'NPA Interest Debit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(40    as varchar(2) ) THEN    'NPA Interest Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(53    as varchar(2) ) THEN    'AWARD Interest Debit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(54    as varchar(2) ) THEN    'AWARD Interest Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(48    as varchar(2) ) THEN    'Write Off    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    41    as varchar(2) ) THEN    'Principal Credit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    42    as varchar(2) ) THEN    'Charges Credit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    47    as varchar(2) ) THEN    'Interest Debit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    43    as varchar(2) ) THEN    'Interest Credit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    44    as varchar(2) ) THEN    'Penal Interest Credit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    46    as varchar(2) ) THEN    'NPA Interest Debit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    45    as varchar(2) ) THEN    'NPA Interest Credit Reversal    '
                    else cast(a.BalanceFlowType as varchar(2) )
                    END as descr,a.drcr
                    FROM xxxx90061 a INNER JOIN xxxx90041 B ON A.TransctionAccountId =b.accountid
                    and b.moduletype=18
                    and a.CanceledFlag= 0 and a.PostDate between '01-jan-2019' and '31-jan-2019'
                    and a.balanceflowtype in (31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,53,54,48)
                    and b.branchcode in (2)

           )kar
           group by descr
           order by descr
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)')
       ,1,1,'')
      
      
    set @query='select * from (
      
        SELECT A.TransctionAccountId AS ACCOUNTID,
        a.FcyTransactionAmount AS aMT,
        a.balanceflowtype,
        postdate,
        b.branchcode,
        b.productcode,
        b.accountnumber,
        sum(case when a.drcr=2 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) credit,
        sum(case when a.drcr=1 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) debit,
        CASE
        WHEN A.BALANCEFLOWTYPE=    cast(31 as varchar(2) )    THEN    "Disbursement Debit"
        WHEN A.BALANCEFLOWTYPE=    cast(32    as varchar(2) ) THEN    "Principal Credit    "
        WHEN A.BALANCEFLOWTYPE=    cast(33    as varchar(2) ) THEN    "Charges Debit    "
        WHEN A.BALANCEFLOWTYPE=    cast(34    as varchar(2) ) THEN    "Charges Credit    "
        WHEN A.BALANCEFLOWTYPE=    cast(35    as varchar(2) ) THEN    "Interest Debit    "
        WHEN A.BALANCEFLOWTYPE=    cast(36    as varchar(2) ) THEN    "Interest Credit"
        WHEN A.BALANCEFLOWTYPE=    cast(37    as varchar(2) ) THEN    "Penal Interest Debit "
        WHEN A.BALANCEFLOWTYPE=    cast(38    as varchar(2) ) THEN    "Penal Interest Credit"
        WHEN A.BALANCEFLOWTYPE=    cast(39    as varchar(2) ) THEN    "NPA Interest Debit    "
        WHEN A.BALANCEFLOWTYPE=    cast(40    as varchar(2) ) THEN    "NPA Interest Credit    "
        WHEN A.BALANCEFLOWTYPE=    cast(53    as varchar(2) ) THEN    "AWARD Interest Debit    "
        WHEN A.BALANCEFLOWTYPE=    cast(54    as varchar(2) ) THEN    "AWARD Interest Credit    "
        WHEN A.BALANCEFLOWTYPE=    cast(48    as varchar(2) ) THEN    "Write Off    "
        WHEN A.BALANCEFLOWTYPE=    cast(    41    as varchar(2) ) THEN    "Principal Credit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    42    as varchar(2) ) THEN    "Charges Credit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    47    as varchar(2) ) THEN    "Interest Debit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    43    as varchar(2) ) THEN    "Interest Credit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    44    as varchar(2) ) THEN    "Penal Interest Credit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    46    as varchar(2) ) THEN    "NPA Interest Debit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    45    as varchar(2) ) THEN    "NPA Interest Credit Reversal    "
        else cast(a.BalanceFlowType as varchar(2) )
        END as descr,a.drcr
        FROM xxxx90061 a INNER JOIN xxxx90041 B ON A.TransctionAccountId =b.accountid
        and b.moduletype=18
        and a.CanceledFlag= 0 and a.PostDate between'+ @frdate+' and '+@todate +
        +'and a.balanceflowtype in (31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,53,54,48)
        and b.branchcode in (2)
    ) x
    pivot (
    sum(debit) as ebit,sum(credit) as credit
    for descR in ('+@col+'))hr';
    execute(@query);
    --------------------------------

  • OK, that explains a lot more. We'll go with the simplified version again, but you need to parametrise your dynamic SQL, rather than passing the dates as literal strings. So, with the above SQL:
    DECLARE @frdate date = '20190101', @todate date = '20190131';
    DECLARE @sql nvarchar(MAX);
    SET @sql = N'
    SELECT entrydate,
           id,
           amt
    FROM xtab
    WHERE entrydate BETWEEN @frdate AND @todate;';
    EXEC sp_executesql @sql, N'@frdate date, @todate date', @frdate = @frdate, @todate = @todate;

    Notice I use sp_executesql instead, and parametrise the statement (like you would in an application).

    On a totally different note, just bceause the SQL is dynamic doesn't mean you should forgo using any kind of formatting in it; infact it's just as important to do so especially when you ned to debug it (you'll see why if you use PRINT or SELECT @sql). Debugging unformatted code is a nightmare, and it's very difficult to read.

    I also suggest uses good aliases. Aaron Bertrand did a great article on this called Bad habits to kick : using table aliases like (a, b, c) or (t1, t2, t3).

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • sir
    excuse my shabbiness
    i am  a newbe ro sql
    one   more question ... may be very absurd

    what  is xtab .........?
    please explain

  • sorry 
    please ignore my silly question

    sorry again

  • sir
    i have tried query after modifying same as below
    --------query
    declare @query nvarchar(max),@col nvarchar(max);
    --declare @frdate datetime='01-jan-2019',@todate datetime ='31-jan-2019';
    DECLARE @frdate date = '20190101', @todate date = '20190131';
    select @col=

    STUFF((SELECT ',' + QUOTENAME(descr)
           from (
             SELECT A.TransctionAccountId AS ACCOUNTID,
                    a.FcyTransactionAmount AS aMT,
                    a.balanceflowtype,
                    postdate,
                    b.branchcode,
                    b.productcode,
                    b.accountnumber,
                    sum(case when a.drcr=2 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) credit,
                    sum(case when a.drcr=1 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) debit,
                    CASE
                    WHEN A.BALANCEFLOWTYPE=    cast(31 as varchar(2) )    THEN    'Disbursement Debit'
                    WHEN A.BALANCEFLOWTYPE=    cast(32    as varchar(2) ) THEN    'Principal Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(33    as varchar(2) ) THEN    'Charges Debit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(34    as varchar(2) ) THEN    'Charges Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(35    as varchar(2) ) THEN    'Interest Debit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(36    as varchar(2) ) THEN    'Interest Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(37    as varchar(2) ) THEN    'Penal Interest Debit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(38    as varchar(2) ) THEN    'Penal Interest Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(39    as varchar(2) ) THEN    'NPA Interest Debit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(40    as varchar(2) ) THEN    'NPA Interest Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(53    as varchar(2) ) THEN    'AWARD Interest Debit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(54    as varchar(2) ) THEN    'AWARD Interest Credit    '
                    WHEN A.BALANCEFLOWTYPE=    cast(48    as varchar(2) ) THEN    'Write Off    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    41    as varchar(2) ) THEN    'Principal Credit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    42    as varchar(2) ) THEN    'Charges Credit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    47    as varchar(2) ) THEN    'Interest Debit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    43    as varchar(2) ) THEN    'Interest Credit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    44    as varchar(2) ) THEN    'Penal Interest Credit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    46    as varchar(2) ) THEN    'NPA Interest Debit Reversal    '
                    WHEN A.BALANCEFLOWTYPE=    cast(    45    as varchar(2) ) THEN    'NPA Interest Credit Reversal    '
                    else cast(a.BalanceFlowType as varchar(2) )
                    END as descr,a.drcr
                    FROM xxxx90061 a INNER JOIN xxxx90041 B ON A.TransctionAccountId =b.accountid
                    and b.moduletype=18
                    and a.CanceledFlag= 0 and a.PostDate between @frdate and @todate
                    and a.balanceflowtype in (31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,53,54,48)
                    and b.branchcode in (2)

           )kar
           group by descr
           order by descr
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)')
       ,1,1,'')
      
      
    set @query='select * from (
      
        SELECT A.TransctionAccountId AS ACCOUNTID,
        a.FcyTransactionAmount AS aMT,
        a.balanceflowtype,
        postdate,
        b.branchcode,
        b.productcode,
        b.accountnumber,
        sum(case when a.drcr=2 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) credit,
        sum(case when a.drcr=1 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) debit,
        CASE
        WHEN A.BALANCEFLOWTYPE=    cast(31 as varchar(2) )    THEN    "Disbursement Debit"
        WHEN A.BALANCEFLOWTYPE=    cast(32    as varchar(2) ) THEN    "Principal Credit    "
        WHEN A.BALANCEFLOWTYPE=    cast(33    as varchar(2) ) THEN    "Charges Debit    "
        WHEN A.BALANCEFLOWTYPE=    cast(34    as varchar(2) ) THEN    "Charges Credit    "
        WHEN A.BALANCEFLOWTYPE=    cast(35    as varchar(2) ) THEN    "Interest Debit    "
        WHEN A.BALANCEFLOWTYPE=    cast(36    as varchar(2) ) THEN    "Interest Credit"
        WHEN A.BALANCEFLOWTYPE=    cast(37    as varchar(2) ) THEN    "Penal Interest Debit "
        WHEN A.BALANCEFLOWTYPE=    cast(38    as varchar(2) ) THEN    "Penal Interest Credit"
        WHEN A.BALANCEFLOWTYPE=    cast(39    as varchar(2) ) THEN    "NPA Interest Debit    "
        WHEN A.BALANCEFLOWTYPE=    cast(40    as varchar(2) ) THEN    "NPA Interest Credit    "
        WHEN A.BALANCEFLOWTYPE=    cast(53    as varchar(2) ) THEN    "AWARD Interest Debit    "
        WHEN A.BALANCEFLOWTYPE=    cast(54    as varchar(2) ) THEN    "AWARD Interest Credit    "
        WHEN A.BALANCEFLOWTYPE=    cast(48    as varchar(2) ) THEN    "Write Off    "
        WHEN A.BALANCEFLOWTYPE=    cast(    41    as varchar(2) ) THEN    "Principal Credit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    42    as varchar(2) ) THEN    "Charges Credit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    47    as varchar(2) ) THEN    "Interest Debit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    43    as varchar(2) ) THEN    "Interest Credit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    44    as varchar(2) ) THEN    "Penal Interest Credit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    46    as varchar(2) ) THEN    "NPA Interest Debit Reversal    "
        WHEN A.BALANCEFLOWTYPE=    cast(    45    as varchar(2) ) THEN    "NPA Interest Credit Reversal    "
        else cast(a.BalanceFlowType as varchar(2) )
        END as descr,a.drcr
        FROM xxxx90061 a INNER JOIN xxxx90041 B ON A.TransctionAccountId =b.accountid
        and b.moduletype=18
        and a.CanceledFlag= 0 and a.PostDate between @frdate and @todate
        and a.balanceflowtype in (31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,53,54,48)
        and b.branchcode in (2)
    ) x
    pivot (
    sum(debit) as ebit,sum(credit) as credit
    for descR in ('+@col+'))hr';
    execute(@query);

    ----------------------
    error is 
    Msg 2812, Level 16, State 62, Line 1
    Could not find stored procedure 'frdate'.

  • You're still using execute(@query);. Like I showed above, you need to use parametrised SQL with sp_executesql.

    P.s. I still really suggest getting some formatting and aliasing in that query too.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • yes 
    sir
    will try to format for sure
    thanks a million

    regards
    hemu

  • I don't see any need on doing a dynamic pivot in here. Your balanceflowtype column has limited values available, so it would be simpler to just code them. The pivot won't work as you can't pivot 2 operations at once, you need to use 2 pivot operators. I don't see the need to separate the amount into debit and credit because your balanceflowtype would take care of that. You also might want to remove columns to actually pivot the data correctly. Here's something that might be simpler.

    WITH CTE AS(
      SELECT A.TransctionAccountId AS ACCOUNTID,
        a.FcyTransactionAmount AS aMT,
        a.balanceflowtype,
        postdate,
        b.branchcode,
        b.productcode,
        b.accountnumber,
        --sum(case when a.drcr=2 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) credit,
        --sum(case when a.drcr=1 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) debit,
        CASE
          WHEN A.BALANCEFLOWTYPE= '31' THEN 'Disbursement Debit'
          WHEN A.BALANCEFLOWTYPE= '32' THEN 'Principal Credit'
          WHEN A.BALANCEFLOWTYPE= '33' THEN 'Charges Debit'
          WHEN A.BALANCEFLOWTYPE= '34' THEN 'Charges Credit'
          WHEN A.BALANCEFLOWTYPE= '35' THEN 'Interest Debit'
          WHEN A.BALANCEFLOWTYPE= '36' THEN 'Interest Credit'
          WHEN A.BALANCEFLOWTYPE= '37' THEN 'Penal Interest Debit'
          WHEN A.BALANCEFLOWTYPE= '38' THEN 'Penal Interest Credit'
          WHEN A.BALANCEFLOWTYPE= '39' THEN 'NPA Interest Debit'
          WHEN A.BALANCEFLOWTYPE= '40' THEN 'NPA Interest Credit'
          WHEN A.BALANCEFLOWTYPE= '53' THEN 'AWARD Interest Debit'
          WHEN A.BALANCEFLOWTYPE= '54' THEN 'AWARD Interest Credit'
          WHEN A.BALANCEFLOWTYPE= '48' THEN 'Write Off'
          WHEN A.BALANCEFLOWTYPE= '41' THEN 'Principal Credit Reversal'
          WHEN A.BALANCEFLOWTYPE= '42' THEN 'Charges Credit Reversal'
          WHEN A.BALANCEFLOWTYPE= '47' THEN 'Interest Debit Reversal'
          WHEN A.BALANCEFLOWTYPE= '43' THEN 'Interest Credit Reversal'
          WHEN A.BALANCEFLOWTYPE= '44' THEN 'Penal Interest Credit Reversal'
          WHEN A.BALANCEFLOWTYPE= '46' THEN 'NPA Interest Debit Reversal'
          WHEN A.BALANCEFLOWTYPE= '45' THEN 'NPA Interest Credit Reversal'
        END as descr,
        a.drcr
      FROM xxxx90061 a
      INNER JOIN xxxx90041 B ON A.TransctionAccountId =b.accountid
      AND b.moduletype=18
      AND a.CanceledFlag= 0 AND a.PostDate BETWEEN @frdate AND @todate
      AND a.balanceflowtype IN (31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,53,54,48)
      AND b.branchcode in (2)
    )
    SELECT *
    FROM CTE
    PIVOT (
    SUM(aMT)
    FOR descR in ( [Disbursement Debit]
        ,[Principal Credit]
        ,[Charges Debit]
        ,[Charges Credit]
        ,[Interest Debit]
        ,[Interest Credit]
        ,[Penal Interest Debit]
        ,[Penal Interest Credit]
        ,[NPA Interest Debit]
        ,[NPA Interest Credit]
        ,[AWARD Interest Debit]
        ,[AWARD Interest Credit]
        ,[Write Off]
        ,[Principal Credit Reversal]
        ,[Charges Credit Reversal]
        ,[Interest Debit Reversal]
        ,[Interest Credit Reversal]
        ,[Penal Interest Credit Reversal]
        ,[NPA Interest Debit Reversal]
        ,[NPA Interest Credit Reversal]))hr;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares - Friday, February 8, 2019 7:42 AM

    I don't see any need on doing a dynamic pivot in here. Your balanceflowtype column has limited values available, so it would be simpler to just code them. The pivot won't work as you can't pivot 2 operations at once, you need to use 2 pivot operators. I don't see the need to separate the amount into debit and credit because your balanceflowtype would take care of that. You also might want to remove columns to actually pivot the data correctly. Here's something that might be simpler.

    +1 from me here Luis. I hadn't actually noted that the OP wasn't using something like the sys objects to create the value @query (i'd just assumed it was). Nice catch.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Your code can be made much simpler by putting all those lookups into a table, like this:

    SELECT *
    INTO #BalanceFlowTypes
    FROM (VALUES
     ('31', 'Disbursement Debit'),
     ('32', 'Principal Credit'),
     ('33', 'Charges Debit'),
     ('34', 'Charges Credit'),
     ('35', 'Interest Debit'),
     ('36', 'Interest Credit'),
     ('37', 'Penal Interest Debit'),
     ('38', 'Penal Interest Credit'),
     ('39', 'NPA Interest Debit'),
     ('40', 'NPA Interest Credit'),
     ('41', 'Principal Credit Reversal'),
     ('42', 'Charges Credit Reversal'),
     ('43', 'Interest Credit Reversal'),
     ('44', 'Penal Interest Credit Reversal'),
     ('45', 'NPA Interest Credit Reversal'),
     ('46', 'NPA Interest Debit Reversal'),
     ('47', 'Interest Debit Reversal'),
     ('48', 'Write Off'),
     ('53', 'AWARD Interest Debit'),
     ('54', 'AWARD Interest Credit')
    ) a (BalanceFlowType, descr)
    WHERE a.balanceflowtype in (31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,53,54)

    SELECT
     A.TransctionAccountId AS ACCOUNTID,
     a.FcyTransactionAmount AS aMT,
     a.balanceflowtype,
     postdate,
     b.branchcode,
     b.productcode,
     b.accountnumber,
     sum(case when a.drcr = 2 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) credit,
     sum(case when a.drcr = 1 then a.FcyTransactionAmount else 0 end ) over (partition by A.TransctionAccountId ,a.balanceflowtype,a.postdate order by a.balanceflowtype ) debit,
     ISNULL(t.descr, cast(a.BalanceFlowType as varchar(2)) as descr,
     a.drcr
    FROM xxxx90061 a
    INNER JOIN xxxx90041 B
     ON A.TransctionAccountId =b.accountid
    LEFT JOIN #BalanceFlowTypes t
     ON t.BalanceFlowType = a.BalanceFlowType
    WHERE b.moduletype = 18
    and a.CanceledFlag = 0 and a.PostDate between ' + @frdate + ' and ' + @todate +
    +'
    and b.branchcode in (2)
    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply