10

I would like to have a query that uses the greater of two values/columns if a certain other value for the record is true.

I'm trying to get a report account holdings. Unfortunately the DB usually stores the value of Cash in a column called HoldingQty, while for every other type of holding (stocks, bonds, mutual funds) it stores it in a column called Qty.

The problem is that sometimes the value of the cash is stored in Qty only, and sometimes it is in both Qty and HoldingQty. Obviously sometimes it is stored only in HoldingQty as mentioned above.

Basically I want my select statement to say "if the security is cash, look at both qty and holding qty and give me the value of whatever is greater. Otherwise, if the security isn't cash just give me qty".

How would I write that in T-SQL? Here is my effort:

SELECT 
    h.account_name, h.security_name, h.security_type, h.price,
    (CASE:
         WHEN security_type = 'cash' 
            THEN (WHEN h.qty > h.holdingqty 
                     THEN h.qty 
                  ELSE h.holdingqty)
         ELSE qty) as quantity, 
     h.total_value
FROM 
    holdings h
WHERE
    ...........
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Mike O.
  • 135
  • 1
  • 2
  • 7
  • you are almost there except for the syntax. – Vamsi Prabhala Sep 30 '15 at 19:54
  • 2
    Use a compound case statement `case when security_Type = 'cash' and h.qty<=h.holdingqty then h.holdingqty else h.qty end as quantity` this basically says when type is cash and holdingqty is > or equal to qty use holdingqty, otherwise use qty. since we know when it's not cash we always use qty, i changed the order a bit to simplify the case statement. – xQbert Sep 30 '15 at 19:54
  • 2
    Be careful with if these columns are nullable. – shawnt00 Sep 30 '15 at 19:57

3 Answers3

7

Your query is correct but need few syntax arrangement, try below code

   SELECT h.account_name, h.security_name, h.security_type, h.price,
   CASE WHEN security_type = 'cash' then 
                                     CASE when h.qty > h.holdingqty then h.qty  
                                      else h.holdingqty END 
    ELSE qty END AS 'YourColumnName'
) as quantity, h.total_value
FROM holdings h
where ...........
Anuj Tripathi
  • 2,251
  • 14
  • 18
2

Almost there!

SELECT  h.account_name ,
        h.security_name ,
    h.security_type ,
    h.price ,
    CASE WHEN security_type = 'cash'
         THEN CASE WHEN h.qty > h.holdingqty THEN h.qty
                   ELSE h.holdingqty
              END
         ELSE qty
    END AS quantity ,
    h.total_value
FROM    holdings h
 WHERE ...........
LordBaconPants
  • 1,404
  • 1
  • 19
  • 22
2

You can achieve this behavior with a nested case expression:

SELECT h.account_name, h.security_name, h.security_type, h.price,
       CASE security_type 
       WHEN 'cash' THEN  CASE WHEN h.qty > h.holdingqty THEN h.qty
                                                        ELSE h.holdingqty
                         END
       ELSE h.qty
       END
FROM   holdings h
WHERE  ...........
Mureinik
  • 297,002
  • 52
  • 306
  • 350