Re-Write SQL Query to a View

  • Hi to all. I need to rewrite the code below ideally as a View as calling application is unable to pass a parameter.

    The requirement:

    When the view is executed it should return data for the current 'AccountingYear' + previous 'AccountingPeriod' (values defined in table AccountingCalendar).

    IF 'AccountingPeriod' = 1 then it should return data from previous 'AccountingYear' + last 'AccountingPeriod' i.e. 12.

    I hope that makes some sense. The SQL code is below. I have attached script to create tables, insert test data + run the query (all done in SQL 2014).

    --Query

    DECLARE @Year int

    DECLARE @Period int

    SET @Year = 2015 --(SELECT AccountingYear FROM AccountingCalendar WHERE AccountingDate = convert(varchar(20),getdate(),106))

    SET @Period = 12 --(SELECT AccountingPeriod FROM AccountingCalendar WHERE AccountingDate = convert(varchar(20),getdate(),106))

    SELECT

    MAX(GLChartOfAccounts.GLChartOfAccountId) AS GLChartOfAccountId,

    SUM(DTbl.OpenBalance) AS OpenBalance,

    SUM(DTbl.ThisPeriodDebits) AS ThisPeriodDebits,

    SUM(DTbl.ThisPeriodCredits) AS ThisPeriodCredits,

    SUM(DTbl.GLItemValue) AS ClosingBalance

    FROM

    (

    SELECT

    GLItems.GLChartOfAccount,

    SUM(GLItems.GLItemValue) AS GLItemValue,

    SUM(GLItems.GLItemValue) AS OpenBalance,

    NULL AS ThisPeriodCredits,

    NULL AS ThisPeriodDebits

    FROM GLItems

    LEFT OUTER JOIN GLChartOfAccounts ON GLItems.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    WHERE GLItems.GLYearPeriod < ((@Year*100)+@Period)

    AND NOT(GLItems.SourceType = 'YC' AND round(GLItems.GLYearPeriod,-2) / 100 = @Year)

    GROUP BY

    GLItems.GLChartOfAccount

    UNION ALL

    SELECT

    GLItems.GLChartOfAccount,

    SUM(GLItems.GLItemValue),

    SUM(CASE WHEN GLItems.SourceType = 'YO' THEN GLItems.GLItemValue ELSE NULL END),

    SUM(CASE

    WHEN GLItems.SourceType = 'YO' THEN NULL

    WHEN GLItems.GLItemValue < 0 THEN GLItems.GLItemValue

    ELSE NULL

    END),

    SUM(CASE

    WHEN GLItems.SourceType = 'YO' THEN NULL

    WHEN GLItems.GLItemValue >= 0 THEN GLItems.GLItemValue

    ELSE NULL

    END)

    FROM GLItems

    LEFT OUTER JOIN GLChartOfAccounts ON GLItems.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    WHERE GLItems.GLYearPeriod = ((@Year*100)+@Period)

    AND NOT(GLItems.SourceType = 'YC' AND round(GLItems.GLYearPeriod,-2) / 100 = @Year)

    GROUP BY

    GLItems.GLChartOfAccount

    ) AS DTbl

    LEFT OUTER JOIN GLChartOfAccounts ON DTbl.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    GROUP BY

    DTbl.GLChartOfAccount

    HAVING NOT (ISNULL(SUM(DTbl.ThisPeriodCredits),0) = 0 AND ISNULL(SUM(DTbl.ThisPeriodDebits),0) = 0 AND ISNULL(SUM(DTbl.OpenBalance),0) = 0)

    ORDER BY

    GLChartOfAccountId

    Kind Regards,

    Phil.

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

  • If you want to pass parameters, you don't need a view. You need an inline table-valued function (iTVF).

    CREATE FUNCTION SomeFunctionName(

    @Year int

    ,@Period int

    ) RETURNS TABLE AS

    RETURN

    SELECT

    MAX(GLChartOfAccounts.GLChartOfAccountId) AS GLChartOfAccountId,

    SUM(DTbl.OpenBalance) AS OpenBalance,

    SUM(DTbl.ThisPeriodDebits) AS ThisPeriodDebits,

    SUM(DTbl.ThisPeriodCredits) AS ThisPeriodCredits,

    SUM(DTbl.GLItemValue) AS ClosingBalance

    FROM

    (

    SELECT

    GLItems.GLChartOfAccount,

    SUM(GLItems.GLItemValue) AS GLItemValue,

    SUM(GLItems.GLItemValue) AS OpenBalance,

    NULL AS ThisPeriodCredits,

    NULL AS ThisPeriodDebits

    FROM GLItems

    LEFT OUTER JOIN GLChartOfAccounts ON GLItems.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    WHERE GLItems.GLYearPeriod < ((@Year*100)+@Period)

    AND NOT(GLItems.SourceType = 'YC' AND round(GLItems.GLYearPeriod,-2) / 100 = @Year)

    GROUP BY

    GLItems.GLChartOfAccount

    UNION ALL

    SELECT

    GLItems.GLChartOfAccount,

    SUM(GLItems.GLItemValue),

    SUM(CASE WHEN GLItems.SourceType = 'YO' THEN GLItems.GLItemValue ELSE NULL END),

    SUM(CASE

    WHEN GLItems.SourceType = 'YO' THEN NULL

    WHEN GLItems.GLItemValue < 0 THEN GLItems.GLItemValue

    ELSE NULL

    END),

    SUM(CASE

    WHEN GLItems.SourceType = 'YO' THEN NULL

    WHEN GLItems.GLItemValue >= 0 THEN GLItems.GLItemValue

    ELSE NULL

    END)

    FROM GLItems

    LEFT OUTER JOIN GLChartOfAccounts ON GLItems.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    WHERE GLItems.GLYearPeriod = ((@Year*100)+@Period)

    AND NOT(GLItems.SourceType = 'YC' AND round(GLItems.GLYearPeriod,-2) / 100 = @Year)

    GROUP BY

    GLItems.GLChartOfAccount

    ) AS DTbl

    LEFT OUTER JOIN GLChartOfAccounts ON DTbl.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    GROUP BY

    DTbl.GLChartOfAccount

    HAVING NOT (ISNULL(SUM(DTbl.ThisPeriodCredits),0) = 0 AND ISNULL(SUM(DTbl.ThisPeriodDebits),0) = 0 AND ISNULL(SUM(DTbl.OpenBalance),0) = 0);

    GO

    --Query

    DECLARE @Year int

    DECLARE @Period int

    SET @Year = 2015 --(SELECT AccountingYear FROM AccountingCalendar WHERE AccountingDate = convert(varchar(20),getdate(),106))

    SET @Period = 12 --(SELECT AccountingPeriod FROM AccountingCalendar WHERE AccountingDate = convert(varchar(20),getdate(),106))

    SELECT *

    FROM dbo.SomeFunctionName(@Year, @Period) f

    ORDER BY GLChartOfAccountId;

    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
  • Not really sure if you need parameters or not. You mention them but also have a commented query using the current date. If you need parameters than use the fine solution that Luis posted. If you want to have your query change based on the current system time then you can do something like this.

    SELECT

    MAX(GLChartOfAccounts.GLChartOfAccountId) AS GLChartOfAccountId,

    SUM(DTbl.OpenBalance) AS OpenBalance,

    SUM(DTbl.ThisPeriodDebits) AS ThisPeriodDebits,

    SUM(DTbl.ThisPeriodCredits) AS ThisPeriodCredits,

    SUM(DTbl.GLItemValue) AS ClosingBalance

    FROM

    (

    SELECT

    GLItems.GLChartOfAccount

    , SUM(GLItems.GLItemValue) AS GLItemValue

    , SUM(GLItems.GLItemValue) AS OpenBalance

    , NULL AS ThisPeriodCredits

    , NULL AS ThisPeriodDebits

    FROM GLItems

    LEFT OUTER JOIN GLChartOfAccounts ON GLItems.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    cross apply (SELECT AccountingYear, AccountingPeriod FROM AccountingCalendar WHERE AccountingDate = convert(varchar(20),getdate(),106)) MyCal

    WHERE GLItems.GLYearPeriod < ((MyCal.AccountingYear * 100) + MyCal.AccountingPeriod)

    AND NOT(GLItems.SourceType = 'YC' AND round(GLItems.GLYearPeriod,-2) / 100 = MyCal.AccountingYear)

    GROUP BY GLItems.GLChartOfAccount

    UNION ALL

    SELECT

    GLItems.GLChartOfAccount

    , SUM(GLItems.GLItemValue)

    , SUM(CASE WHEN GLItems.SourceType = 'YO' THEN GLItems.GLItemValue ELSE NULL END)

    , SUM(CASE

    WHEN GLItems.SourceType = 'YO' THEN NULL

    WHEN GLItems.GLItemValue < 0 THEN GLItems.GLItemValue

    ELSE NULL

    END)

    , SUM(CASE

    WHEN GLItems.SourceType = 'YO' THEN NULL

    WHEN GLItems.GLItemValue >= 0 THEN GLItems.GLItemValue

    ELSE NULL

    END)

    FROM GLItems

    LEFT OUTER JOIN GLChartOfAccounts ON GLItems.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    cross apply (SELECT AccountingYear, AccountingPeriod FROM AccountingCalendar WHERE AccountingDate = convert(varchar(20),getdate(),106)) MyCal

    WHERE GLItems.GLYearPeriod = ((MyCal.AccountingYear * 100) + MyCal.AccountingPeriod)

    AND NOT(GLItems.SourceType = 'YC' AND round(GLItems.GLYearPeriod,-2) / 100 = MyCal.AccountingYear)

    GROUP BY GLItems.GLChartOfAccount

    ) AS DTbl

    LEFT OUTER JOIN GLChartOfAccounts ON DTbl.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    GROUP BY DTbl.GLChartOfAccount

    HAVING NOT (ISNULL(SUM(DTbl.ThisPeriodCredits),0) = 0 AND ISNULL(SUM(DTbl.ThisPeriodDebits),0) = 0 AND ISNULL(SUM(DTbl.OpenBalance),0) = 0)

    ORDER BY GLChartOfAccountId

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Hi Luis/Sean. I confirm I DO NOT wantt to use the parameters hence the re-write to enable VIEW creation.

    Luis I did re-write as inline table function but I was still having to pass @Year + @Period parameters.

    Sean your code is nearly there although it returns the current accounting period. I can remedy this with a slight change adding -1 to MyCal.AccountingPeriod. This however only serves to highlight my coding conundrum.

    ((MyCal.AccountingYear * 100) + MyCal.AccountingPeriod -1) will work but not at year end

    New Accounting Year 2017

    Period 1

    The code would run and try to return data for AccountingYear = 2017 AccountingPeriod 0 rather than the required AccountingYear = 2016 AccountingPeriod 12

    Basically the accounting calendar is divided in 12 periods. When the code runs it should return data for previous closed period (this has a PeriodClosed flag set to 1 in table AccountingCalendar). If AccountingPeriod is 1 it should return data from previous AccountingYear (2015) last closed AccountingPeriod (12).

    Confused? I nearly am πŸ™‚

    WHERE GLItems.GLYearPeriod = ((MyCal.AccountingYear * 100) + MyCal.AccountingPeriod -1)

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

  • Those two UNIONed queries would probably be more efficient run as one query. also, is that left-joined table actually necessary except for the outer SELECT? This is what I mean:

    SELECT

    GLItems.GLChartOfAccount,

    GLItemValue = SUM(GLItems.GLItemValue),

    OpenBalance = SUM(CASE WHEN x.[Period] = 'PREVIOUS' THEN GLItems.GLItemValue ELSE (CASE

    WHEN GLItems.SourceType = 'YO' THEN GLItems.GLItemValue ELSE NULL END) END),

    ThisPeriodCredits = SUM(CASE WHEN x.[Period] = 'PREVIOUS' THEN NULL ELSE (CASE

    WHEN GLItems.SourceType = 'YO' THEN NULL

    WHEN GLItems.GLItemValue < 0 THEN GLItems.GLItemValue

    ELSE NULL

    END) END),

    ThisPeriodDebits = SUM(CASE WHEN x.[Period] = 'PREVIOUS' THEN NULL ELSE (CASE

    WHEN GLItems.SourceType = 'YO' THEN NULL

    WHEN GLItems.GLItemValue >= 0 THEN GLItems.GLItemValue

    ELSE NULL

    END) END)

    FROM GLItems

    --LEFT OUTER JOIN GLChartOfAccounts

    --ON GLItems.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    CROSS APPLY (

    SELECT [Period] = CASE

    WHEN GLItems.GLYearPeriod = (@Year*100)+@Period THEN 'CURRENT'

    WHEN GLItems.GLYearPeriod < ((@Year*100)+@Period) THEN 'PREVIOUS' END

    ) x

    WHERE x.[Period] IS NOT NULL

    AND NOT(GLItems.SourceType = 'YC' AND round(GLItems.GLYearPeriod,-2) / 100 = @Year)

    GROUP BY

    GLItems.GLChartOfAccount

    β€œ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

  • 2Tall (4/13/2016)


    Hi Luis/Sean. I confirm I DO NOT wantt to use the parameters hence the re-write to enable VIEW creation.

    Luis I did re-write as inline table function but I was still having to pass @Year + @Period parameters.

    Sean your code is nearly there although it returns the current accounting period. I can remedy this with a slight change adding -1 to MyCal.AccountingPeriod. This however only serves to highlight my coding conundrum.

    ((MyCal.AccountingYear * 100) + MyCal.AccountingPeriod -1) will work but not at year end

    New Accounting Year 2017

    Period 1

    The code would run and try to return data for AccountingYear = 2017 AccountingPeriod 0 rather than the required AccountingYear = 2016 AccountingPeriod 12

    Basically the accounting calendar is divided in 12 periods. When the code runs it should return data for previous closed period (this has a PeriodClosed flag set to 1 in table AccountingCalendar). If AccountingPeriod is 1 it should return data from previous AccountingYear (2015) last closed AccountingPeriod (12).

    Confused? I nearly am πŸ™‚

    WHERE GLItems.GLYearPeriod = ((MyCal.AccountingYear * 100) + MyCal.AccountingPeriod -1)

    Sounds to me like a view is not what you need because you need the ability to pass in parameters. Or figure out some rules to determine which period you want to use.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Hi Chris. Thanks for the re-write. Your code still requires a parameter to be passed at time of execution.

    Many Thanks,

    Phil.

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

  • Hi Sean.

    Sounds to me like a view is not what you need because you need the ability to pass in parameters. Or figure out some rules to determine which period you want to use.

    I am leaning towards 'figure out some rules to determine which period you want to use.....'

    Many Thanks,

    Phil.

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

  • Hi. I have tried adding CASE to WHERE clause.

    WHERE

    GLItems.GLYearPeriod =

    CASE

    WHEN MyCal.AccountingPeriod = 1 THEN ((MyCal.AccountingYear * 100 -1) + MyCal.AccountingPeriod + 11)

    ELSE GLItems.GLYearPeriod = (MyCal.AccountingYear * 100) + MyCal.AccountingPeriod-1)

    I may be barking up the wrong tree even if I could get the syntax correct!

    Many Thanks,

    Phil.

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

  • 2Tall (4/13/2016)


    Hi. I have tried adding CASE to WHERE clause.

    WHERE

    GLItems.GLYearPeriod =

    CASE

    WHEN MyCal.AccountingPeriod = 1 THEN ((MyCal.AccountingYear * 100 -1) + MyCal.AccountingPeriod + 11)

    ELSE GLItems.GLYearPeriod = (MyCal.AccountingYear * 100) + MyCal.AccountingPeriod-1)

    I may be barking up the wrong tree even if I could get the syntax correct!

    Many Thanks,

    Phil.

    You just need an END to end your case expression and it will be syntactically correct. I don't know about the logic part...I leave that to you. πŸ˜€

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Helow again. SQL reports incorrect syntax near second WHEN.

    WHERE GLItems.GLYearPeriod =

    CASE

    WHEN MyCal.AccountingPeriod = 1 THEN ((MyCal.AccountingYear * 100 -1) + (MyCal.AccountingPeriod + 11)

    WHEN GLItems.GLYearPeriod <> 1 THEN ((MyCal.AccountingYear * 100) + (MyCal.AccountingPeriod-1)

    END

    GROUP BY GLItems.GLChartOfAccount

    Full code:

    SELECT

    MAX(GLChartOfAccounts.GLChartOfAccountId) AS GLChartOfAccountId,

    SUM(DTbl.OpenBalance) AS OpenBalance,

    SUM(DTbl.ThisPeriodDebits) AS ThisPeriodDebits,

    SUM(DTbl.ThisPeriodCredits) AS ThisPeriodCredits,

    SUM(DTbl.GLItemValue) AS ClosingBalance

    FROM

    (

    SELECT

    GLItems.GLChartOfAccount

    , SUM(GLItems.GLItemValue) AS GLItemValue

    , SUM(GLItems.GLItemValue) AS OpenBalance

    , NULL AS ThisPeriodCredits

    , NULL AS ThisPeriodDebits

    FROM GLItems

    LEFT OUTER JOIN GLChartOfAccounts ON GLItems.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    cross apply (SELECT AccountingYear, AccountingPeriod FROM AccountingCalendar WHERE AccountingDate = convert(varchar(20),getdate(),106)) MyCal

    --WHERE GLItems.GLYearPeriod < ((MyCal.AccountingYear * 100) + MyCal.AccountingPeriod)

    --AND NOT(GLItems.SourceType = 'YC' AND round(GLItems.GLYearPeriod,-2) / 100 = MyCal.AccountingYear)

    WHERE GLItems.GLYearPeriod =

    CASE

    WHEN MyCal.AccountingPeriod = 1 THEN ((MyCal.AccountingYear * 100 -1) + (MyCal.AccountingPeriod + 11)

    WHEN GLItems.GLYearPeriod <> 1 THEN ((MyCal.AccountingYear * 100) + (MyCal.AccountingPeriod-1)

    END

    GROUP BY GLItems.GLChartOfAccount

    UNION ALL

    SELECT

    GLItems.GLChartOfAccount

    , SUM(GLItems.GLItemValue)

    , SUM(CASE WHEN GLItems.SourceType = 'YO' THEN GLItems.GLItemValue ELSE NULL END)

    , SUM(CASE

    WHEN GLItems.SourceType = 'YO' THEN NULL

    WHEN GLItems.GLItemValue < 0 THEN GLItems.GLItemValue

    ELSE NULL

    END)

    , SUM(CASE

    WHEN GLItems.SourceType = 'YO' THEN NULL

    WHEN GLItems.GLItemValue >= 0 THEN GLItems.GLItemValue

    ELSE NULL

    END)

    FROM GLItems

    LEFT OUTER JOIN GLChartOfAccounts ON GLItems.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    cross apply (SELECT AccountingYear, AccountingPeriod FROM AccountingCalendar WHERE AccountingDate = convert(varchar(20),getdate(),106)) MyCal

    --WHERE GLItems.GLYearPeriod = ((MyCal.AccountingYear * 100) + MyCal.AccountingPeriod)

    --AND NOT(GLItems.SourceType = 'YC' AND round(GLItems.GLYearPeriod,-2) / 100 = MyCal.AccountingYear)

    WHERE GLItems.GLYearPeriod =

    CASE

    WHEN MyCal.AccountingPeriod = 1 THEN ((MyCal.AccountingYear * 100 -1) + (MyCal.AccountingPeriod + 11)

    WHEN GLItems.GLYearPeriod <> 1 THEN ((MyCal.AccountingYear * 100) + (MyCal.AccountingPeriod-1)

    END

    GROUP BY GLItems.GLChartOfAccount

    ) AS DTbl

    LEFT OUTER JOIN GLChartOfAccounts ON DTbl.GLChartOfAccount = GLChartOfAccounts.GLChartOfAccount

    GROUP BY DTbl.GLChartOfAccount

    HAVING NOT (ISNULL(SUM(DTbl.ThisPeriodCredits),0) = 0 AND ISNULL(SUM(DTbl.ThisPeriodDebits),0) = 0 AND ISNULL(SUM(DTbl.OpenBalance),0) = 0)

    ORDER BY GLChartOfAccountId

    Many Thanks,

    Phil.

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

  • Phil

    I'd say you're missing a ")" at the end of both of those WHEN lines.

    Edit - either that, or get rid of the first "(" on each line - I don't think you need it.

    John

  • Good spot John. The query now runs. Time to test the logic πŸ™‚

    Many thanks to everyone who has taken time to view this post.

    Phil.

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

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

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