Solved

SQL Command help with nested case statement logic

Posted on 2011-03-21
6
935 Views
Last Modified: 2012-05-11
Anyone have any input on how to correct my code.  I have a Crystal Report using a SQL command statement.  The SQL Command uses a SQL Command parameter to get the accounting period from the user input.  The SQL command parameter is a character string; example enter C for current, L for last or accounting Period.  If the user enters C the sql statement looks up the current period in a table called GLSYSTEM.  If the user enters L the sql statement subtracts 1 month from the current period.  However if the GLSYSTEM accounting period is 1 (January) then the sql statment needs to default to accounting period 12 (December).


SELECT
gla.COMPANY,
gla.ACCT_UNIT,
gla.ACCOUNT,
'{?Period_1}' as Param_1,
CASE '{?Period_1}'
WHEN 'C' THEN gls.ACCT_PERIOD
      WHEN 'L' THEN
                  CASE gls.ACCT_PERIOD
                        WHEN gls.ACCT_PERIOD = 1 THEN 12
                        WHEN gls.ACCT_PERIOD in (2 to 12) THEN gls.ACCT_PERIOD-1
                        END

else cast('{?Period_1}' as integer)
end as col3,
gla.FISCAL_YEAR,
gls.ACCT_PERIOD,
gls.FISCAL_YEAR
FROM GLAMOUNTS gla
LEFT OUTER JOIN GLSYSTEM gls
ON gla.COMPANY = gls.COMPANY
0
Comment
Question by:jrbledsoe001
  • 2
  • 2
6 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 35184923
try this.
SELECT gla.COMPANY, 
       gla.ACCT_UNIT, 
       gla.ACCOUNT, 
       '{?Period_1}' AS Param_1, 
       CASE '{?Period_1}' 
         WHEN 'C' THEN gls.ACCT_PERIOD 
         WHEN 'L' THEN CASE 
                         WHEN gls.ACCT_PERIOD = 1 THEN 12 
                         WHEN gls.ACCT_PERIOD BETWEEN 2 AND 12 THEN 
                         gls.ACCT_PERIOD - 1 
                       END 
         ELSE CAST('{?Period_1}' AS INTEGER) 
       END           AS col3, 
       gla.FISCAL_YEAR, 
       gls.ACCT_PERIOD, 
       gls.FISCAL_YEAR 
  FROM GLAMOUNTS gla 
       LEFT OUTER JOIN GLSYSTEM gls 
         ON gla.COMPANY = gls.COMPANY 

Open in new window

0
 

Author Comment

by:jrbledsoe001
ID: 35184932
this seems to be working

SELECT
gla.COMPANY,
gla.ACCT_UNIT,
gla.ACCOUNT,
{?Period} as Param_1,
'{?Period_1}' as Param_2,
CASE '{?Period_1}'
WHEN 'C' THEN gls.ACCT_PERIOD
      WHEN 'L' THEN
      CASE gls.ACCT_PERIOD
      WHEN 1 THEN 12
                      ELSE gls.ACCT_PERIOD-1
      END
else cast('{?Period_1}' as integer)
END as col3,
gla.FISCAL_YEAR,
gls.ACCT_PERIOD,
gls.FISCAL_YEAR
FROM GLAMOUNTS gla
LEFT OUTER JOIN GLSYSTEM gls
ON gla.COMPANY = gls.COMPANY
0
 

Author Comment

by:jrbledsoe001
ID: 35184936
Anyone have any other ideas that might be helpful to fellow learners?
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35184948
The CASE condition on gls.ACCT_PERIOD BETWEEN 2 AND 12 is not required.
SELECT gla.COMPANY, 
       gla.ACCT_UNIT, 
       gla.ACCOUNT, 
       '{?Period_1}' AS Param_1, 
       CASE '{?Period_1}' 
         WHEN 'C' THEN gls.ACCT_PERIOD 
         WHEN 'L' THEN CASE 
                         WHEN gls.ACCT_PERIOD = 1 THEN 12 
                         ELSE gls.ACCT_PERIOD - 1 
                       END 
         ELSE CAST('{?Period_1}' AS INTEGER) 
       END           AS col3, 
       gla.FISCAL_YEAR, 
       gls.ACCT_PERIOD, 
       gls.FISCAL_YEAR 
  FROM GLAMOUNTS gla 
       LEFT OUTER JOIN GLSYSTEM gls 
         ON gla.COMPANY = gls.COMPANY

Open in new window

0
 
LVL 100

Expert Comment

by:mlmcc
ID: 35473621
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question