Emile Cloete
Emile Cloete

Reputation: 163

Conditionally select constant instead of column value

I am trying to select contract, vehicle and revenue values from a trips table in SQL Server without selecting the revenue for a certain contract and vehicle. For example:

    SELECT contract, vehicle, revenue FROM trips

But the revenue should show 0 where the contract is John Doe and at the same time the truck is 'Truck 1' because that trip's revenue needs to show somewhere else.

Upvotes: 0

Views: 2137

Answers (1)

Rich Benner
Rich Benner

Reputation: 8113

You need a pretty simple CASE statement to do what you're after.

SELECT
contract
,vehicle
,CASE WHEN contract = 'John Doe' THEN 0 ELSE revenue END AS revenue
FROM trips

Upvotes: 5

Related Questions