Solved

SQL Command help with nested case statement logic

Posted on 2011-03-21
6
931 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 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Anyone have any other ideas that might be helpful to fellow learners?
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now