Solved

Nested CASE statement  in T-sQL

Posted on 2014-11-10
8
41 Views
Last Modified: 2016-06-15
I have a crystal report with A financial detail table that has 13 accounting period columns in it and based on the accounting period sent by a parameter  between 1 to 13  it should display the corresponding amount for the corresponding account for that specific accounting period. then the amount is further manipulated to show negative or positive values.

DECLARE @FY AS INT ,@COMPANY as varchar(100),@ACCTPERIOD AS VARCHAR(26)

SELECT  
COMPANY, ACCOUNT,

CASE
      WHEN @ACCTPERIOD='1'  THEN DB_AMOUNT_01
      WHEN @ACCTPERIOD='2'  THEN DB_AMOUNT_02
      WHEN @ACCTPERIOD='3'  THEN DB_AMOUNT_03
      WHEN @ACCTPERIOD='4'  THEN DB_AMOUNT_04
    WHEN @ACCTPERIOD='5'  THEN DB_AMOUNT_05
    WHEN @ACCTPERIOD='6'  THEN DB_AMOUNT_06
    WHEN @ACCTPERIOD='7'  THEN DB_AMOUNT_07
      WHEN @ACCTPERIOD='8'  THEN DB_AMOUNT_08
      WHEN @ACCTPERIOD='9'  THEN DB_AMOUNT_09
      WHEN @ACCTPERIOD='10' THEN DB_AMOUNT_10
      WHEN @ACCTPERIOD='11' THEN DB_AMOUNT_11
      WHEN @ACCTPERIOD='12' THEN DB_AMOUNT_12
    WHEN @ACCTPERIOD='13' THEN DB_AMOUNT_13

 ELSE ''
 END AS DBAMOUNT,
 


CASE       
WHEN ((SUBSTRING((CAST (PROD.dbo.FBDETAIL.ACCOUNT AS VARCHAR(10))),1,1))='4') AND (DB_AMOUNT_01 < 0) THEN  (ABS(DB_AMOUNT_01))
WHEN ((SUBSTRING((CAST (PROD.dbo.FBDETAIL.ACCOUNT AS VARCHAR(10))),1,1))='4') AND (DB_AMOUNT_01 > 0) THEN  (-1*(DB_AMOUNT_01))
WHEN ((SUBSTRING((CAST (PROD.dbo.FBDETAIL.ACCOUNT AS VARCHAR(10))),1,1))='5') AND (DB_AMOUNT_01 < 0) THEN  (ABS(DB_AMOUNT_01))
WHEN ((SUBSTRING((CAST (PROD.dbo.FBDETAIL.ACCOUNT AS VARCHAR(10))),1,1))='5') AND (DB_AMOUNT_01 > 0) THEN  (-1*(DB_AMOUNT_01))
ELSE DB_AMOUNT_01
END AS DOLLARS,



FISCAL_YEAR,  0 AS PERIOD,'BUDGET' "TIME CLASS", DB_UNITS_01 AS UNITS
FROM PROD.dbo.FBDETAIL  

WHERE      (FISCAL_YEAR = @FY)
AND COMPANY='100'



Now i need to take the DBAMOUNT column and further manipulated it  for accounts starting with 4 and 5 as shown in second case statement,  but i would like to reference the  DBAMOUNT rather than the first DB_AMOUNT_01  i am currently reffering in the first case statement.

i know i can create a table valued function and put the first case statement init but i am having trouble retrieving the function in the stored procedure as it returns multiple values.


any ideas how can i  reference the column for first case statment into second case statment.
0
Comment
Question by:malikomran
8 Comments
 
LVL 18

Expert Comment

by:vasto
Comment Utility
use a subquery:

select DBAMOUNT 
FROM 
(
.... your current query
) t

Open in new window

0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
You know that
CASE       
WHEN ((SUBSTRING((CAST (PROD.dbo.FBDETAIL.ACCOUNT AS VARCHAR(10))),1,1))='4') AND (DB_AMOUNT_01 < 0) THEN  (ABS(DB_AMOUNT_01)) 
WHEN ((SUBSTRING((CAST (PROD.dbo.FBDETAIL.ACCOUNT AS VARCHAR(10))),1,1))='4') AND (DB_AMOUNT_01 > 0) THEN  (-1*(DB_AMOUNT_01)) 
WHEN ((SUBSTRING((CAST (PROD.dbo.FBDETAIL.ACCOUNT AS VARCHAR(10))),1,1))='5') AND (DB_AMOUNT_01 < 0) THEN  (ABS(DB_AMOUNT_01)) 
WHEN ((SUBSTRING((CAST (PROD.dbo.FBDETAIL.ACCOUNT AS VARCHAR(10))),1,1))='5') AND (DB_AMOUNT_01 > 0) THEN  (-1*(DB_AMOUNT_01)) 
ELSE DB_AMOUNT_01
END AS DOLLARS, 

Open in new window

is the same as
CASE       
WHEN ((SUBSTRING((CAST (PROD.dbo.FBDETAIL.ACCOUNT AS VARCHAR(10))),1,1)) in ('4','5') THEN  -DB_AMOUNT_01 
ELSE DB_AMOUNT_01
END AS DOLLARS, 

Open in new window

?
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
It appears that when the db_amount is negative, you use abs(db_amount) to make it positive and when it's positive you multiply by -1 to make it negative whether account = '4' or '5'. Why not just multiply by -1 for every case? Then you can make your first case statement -
CASE
      WHEN @ACCTPERIOD='1'  THEN DB_AMOUNT_01*(-1)
      WHEN @ACCTPERIOD='2'  THEN DB_AMOUNT_02*(-1)
      WHEN @ACCTPERIOD='3'  THEN DB_AMOUNT_03*(-1)
      WHEN @ACCTPERIOD='4'  THEN DB_AMOUNT_04*(-1)
    WHEN @ACCTPERIOD='5'  THEN DB_AMOUNT_05*(-1)
    WHEN @ACCTPERIOD='6'  THEN DB_AMOUNT_06*(-1)
    WHEN @ACCTPERIOD='7'  THEN DB_AMOUNT_07*(-1)
      WHEN @ACCTPERIOD='8'  THEN DB_AMOUNT_08*(-1)
      WHEN @ACCTPERIOD='9'  THEN DB_AMOUNT_09*(-1)
      WHEN @ACCTPERIOD='10' THEN DB_AMOUNT_10*(-1)
      WHEN @ACCTPERIOD='11' THEN DB_AMOUNT_11*(-1)
      WHEN @ACCTPERIOD='12' THEN DB_AMOUNT_12*(-1)
    WHEN @ACCTPERIOD='13' THEN DB_AMOUNT_13*(-1)
 ELSE ''
 END AS DBDOLLARAMOUNT,
...
WHERE SUBSTRING(CAST (PROD.dbo.FBDETAIL.ACCOUNT AS VARCHAR(10)),1,1) IN ('4','5')
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
malikomran - How's it going?  Looks like you have three good comments to work with..
0
 

Author Comment

by:malikomran
Comment Utility
Qlemo, this is a mistake, i want to grant you 400 points. i have sent a request to the moderator
0
 
LVL 22

Expert Comment

by:Steve Wales
Comment Utility
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Qlemo (https:#a40433907)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

sjwales
Experts-Exchange Cleanup Volunteer
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Data lost in SQL server 23 94
Excel to create SQL insert 7 34
query question 4 26
SQL Help joining two tables 7 33
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

6 Experts available now in Live!

Get 1:1 Help Now