mercredi 15 juin 2016

Case when a value is different to other value, SQL Server

I have this table structure for table prices:

CREATE TABLE prices
(
     id int, 
     priceFrom int, 
     priceUp int
);

INSERT INTO prices (id, priceFrom, priceUp)
VALUES (1, 23, 23), (2, 0, 0), (3, 12, 13),
       (4, 40, 40), (5, 15, 15), (6, 0, 0);

This is the result:

enter image description here

I have this query:

select 
    pricefrom, priceup,
    case
        when pricefrom = 0 then null
        when priceFrom <> priceUp then priceFrom + ' - ' + priceUp
        when priceFrom = priceUp then priceFrom
    end as FinalPrice
from 
    prices

what I need is to do a case when

  1. pricefrom = 0 then show null
  2. pricefrom = priceup then show the price
  3. At least if pricefrom != priceup I want to show for example this: 12(pricefrom) - 13(priceup)

but in my query in this line:

enter image description here

I try to do this with <> but in the result appears the sum for both numbers:

enter image description here

How can I fix this?

Aucun commentaire:

Enregistrer un commentaire