Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CASE STATEMENT

Status
Not open for further replies.

wexas45

MIS
Joined
Mar 11, 2010
Messages
9
Location
GB
Hello All, can you please help. I am trying to display margin as 0.00 if [GrossProfit] = 0 if not then do the calculation. thanks in advance


SELECT *, CASE WHERE [GrossProfit] <> 0 THEN (CONVERT(DECIMAL(17,2),([GrossProfit] / NULLIF(Price,0) * 100.0))) AS Margin
ELSE CONVERT(DECIMAL(17,2),0.0) END

FROM
(SELECT
[Booking Product],
YEAR([Booking Conf Date]) AS 'BookingYear',
MONTH([Booking Conf Date]) AS 'BookingMonth',
COUNT(DISTINCT [Booking Ref]) AS TotalBookings,
CONVERT (DECIMAL(17,2),(SUM(GBPCost))) AS Cost,
CONVERT (DECIMAL(17,2),(SUM(price))) AS Price,
CONVERT (DECIMAL(17,2),(SUM([Price]-[GBPCost]))) AS 'GrossProfit',
([Booked Adults]+[Booked Children]+[Booked Infants])AS 'PAX'

FROM
[Travelink-Test2].[dbo].[view_EUR_Accounts_Bookings_On_Components]

WHERE
BookingStatus = 'CONFIRMED'
AND [Booking Product]= 'LEIS'

GROUP BY
[GBPCost],[Price],[Booking Product],[Booking Conf Date],
[Booked Adults],[Booked Children],[Booked Infants]) X;
 
the [AS] clause should be after the END of the CASE statement, and the statement is CASE WHEN not CASE WHERE:
Code:
SELECT *,
      CASE [COLOR=red]WHEN[/color] [GrossProfit] <> 0
           THEN (CONVERT(DECIMAL(17,2),([GrossProfit] / NULLIF(Price,0) * 100.0)))
      ELSE  CONVERT(DECIMAL(17,2),0.0) END [COLOR=red]AS Margin[/color]
....

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top