SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need advice on query


Need advice on query

Author
Message
vigneshkumart50
vigneshkumart50
SSC Veteran
SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)

Group: General Forum Members
Points: 263 Visits: 184
Hi,

I have two columns like below

parsename('$'+ Convert(varchar,Convert(money,Q.[Profit Cost]-R.[Shared Dollars]),1),2) as [Pre Profit],

parsename('$'+ Convert(varchar,Convert(money,R.[New Cost+Shared Dollars]-R.[Shared Dollars]),1),2) as [Post Profit]


Formula to be applied:

When [Pre Profit] value is zero then its value should be 1. I tried the below case statement and it doesn't work.

case when Q.[Profit Cost]-R.[Shared Dollars] = 0 then 1
else ((R.[New Cost+Shared Dollars]-R.[Shared Dollars]) / (Q.[Profit Cost]-R.[Shared Dollars])) end as [Percentage]

Really appreciate any help on this.
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40354 Visits: 19452
vigneshkumart50 (8/25/2014)
Hi,

I have two columns like below

parsename('$'+ Convert(varchar,Convert(money,Q.[Profit Cost]-R.[Shared Dollars]),1),2) as [Pre Profit],

parsename('$'+ Convert(varchar,Convert(money,R.[New Cost+Shared Dollars]-R.[Shared Dollars]),1),2) as [Post Profit]


Formula to be applied:

When [Pre Profit] value is zero then its value should be 1. I tried the below case statement and it doesn't work.

case when Q.[Profit Cost]-R.[Shared Dollars] = 0 then 1
else ((R.[New Cost+Shared Dollars]-R.[Shared Dollars]) / (Q.[Profit Cost]-R.[Shared Dollars])) end as [Percentage]

Really appreciate any help on this.


Here is a quick mock-up with some pseudo data and some quick code, you can use this to better define the question as at first glance, I cannot see anything wrong in your code.
Cool

USE tempdb;
GO

DECLARE @SAMPLE_PROFIT TABLE
(
SP_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,[Profit Cost] DECIMAL(18,5) NOT NULL
);
DECLARE @SAMPLE_REVENUE TABLE
(
SR_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,[Shared Dollars] DECIMAL(18,5) NOT NULL
,[New Cost+Shared Dollars] DECIMAL(18,5) NOT NULL
);

INSERT INTO @SAMPLE_PROFIT([Profit Cost])
VALUES (100.00),(200.00),(300.00),(400.00),(500.00);

INSERT INTO @SAMPLE_REVENUE([Shared Dollars],[New Cost+Shared Dollars])
VALUES (300.00,100.00),(300.00,300.00),(300.00,400.00),(300.00,500.00),(300.00,500.00);

SELECT
SP.SP_ID
,SR.SR_ID
,SP.[Profit Cost]
,SR.[Shared Dollars]
,SR.[New Cost+Shared Dollars]
,SP.[Profit Cost] - SR.[Shared Dollars] AS [Pre Profit]
,SR.[New Cost+Shared Dollars] - SR.[Shared Dollars] AS [Post Profit]
,CASE
WHEN (SP.[Profit Cost] - SR.[Shared Dollars]) = 0 THEN 1
ELSE (SR.[New Cost+Shared Dollars] - SR.[Shared Dollars]) / (SP.[Profit Cost] - SR.[Shared Dollars])
END AS [Percentage]
FROM @SAMPLE_PROFIT SP
OUTER APPLY @SAMPLE_REVENUE SR;


MyDoggieJessie
MyDoggieJessie
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12178 Visits: 7444
What exactly isn't working? Are you getting an error? Could be that one of your values in the CASE is NULL...

______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience" ;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search