INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Can addition or sustraction be performemed on two case statments

Can addition or sustraction be performemed on two case statments

Can addition or sustraction be performemed on two case statments

(OP)
Hello,

I have a report that get the data from sql statements,
it is looks something like this

CODE

SELECT
I9ADR7,
CUSNM,
SUM((case when yr = YEAR( CURRENT DATE)-4 then sales end)) AS "2013",
SUM((case when yr = YEAR( CURRENT DATE)-3 then sales end)) AS "2014",
SUM((case when yr = YEAR( CURRENT DATE)-2 then sales end)) AS "2015",
SUM((case when yr = YEAR( CURRENT DATE)-1 then sales end)) AS "2016",
SUM((case when yr = YEAR( CURRENT DATE) then sales end)) AS "2017",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'01' THEN SALES END)) AS "January",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'02' THEN SALES END)) AS "February",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'03' THEN SALES END)) AS "March",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'04' THEN SALES END)) AS "April",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'05' THEN SALES END)) AS "May",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'06' THEN SALES END)) AS "June",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'07' THEN SALES END)) AS "July",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'08' THEN SALES END)) AS "August",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'09'THEN SALES END)) AS "September",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'10' THEN SALES END)) AS "October",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'11' THEN SALES END)) AS "November",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'12' THEN SALES END)) AS "December"
FROM DAVIESF.BOOKPRLINK
WHERE (CUSNM ='CENTURY TOOLS & MACHINERY LTD      ')
GROUP BY I9ADR7, CUSNM 

this works well, but I need to subtract 2016 form 2017 I was wondering if I can do something like this

CODE

SUM((case when yr = YEAR( CURRENT DATE) then sales end)-(case when yr = YEAR( CURRENT DATE)-1 then sales end)) AS "2017", 


I tried the above code but I get an empty field

any help is much welcomed!!!

thanks!!

RE: Can addition or sustraction be performemed on two case statments

Why not just perform a query on your query

Select .....
From
(SELECT
I9ADR7,
CUSNM,
SUM((case when yr = YEAR( CURRENT DATE)-4 then sales end)) AS "2013",
SUM((case when yr = YEAR( CURRENT DATE)-3 then sales end)) AS "2014",
SUM((case when yr = YEAR( CURRENT DATE)-2 then sales end)) AS "2015",
SUM((case when yr = YEAR( CURRENT DATE)-1 then sales end)) AS "2016",
SUM((case when yr = YEAR( CURRENT DATE) then sales end)) AS "2017",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'01' THEN SALES END)) AS "January",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'02' THEN SALES END)) AS "February",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'03' THEN SALES END)) AS "March",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'04' THEN SALES END)) AS "April",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'05' THEN SALES END)) AS "May",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'06' THEN SALES END)) AS "June",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'07' THEN SALES END)) AS "July",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'08' THEN SALES END)) AS "August",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'09'THEN SALES END)) AS "September",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'10' THEN SALES END)) AS "October",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'11' THEN SALES END)) AS "November",
SUM((CASE WHEN YRMO = YEAR( CURRENT DATE)||'12' THEN SALES END)) AS "December"
FROM DAVIESF.BOOKPRLINK
WHERE (CUSNM ='CENTURY TOOLS & MACHINERY LTD ')
GROUP BY I9ADR7, CUSNM) A

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close