0

I am trying to calculate the query using if condition, I need to show two values as buying and selling.

SELECT
    n.Date as Date,
    n.dp_id as DP_ID,
    cast(n.emp_no as nchar) as emp_no,
    n.holder as HOLDER,
    n.add1 as ADD1,
    n.add2 as ADD2,
    n.add3 as ADD3,
    n.add4 as ADD4,
    n.pin as PIN,
    'emp_table' as rec_type,
    (
    select s.position from emp_table as s
    where s.emp_no = n.emp_no
    and s.dp_id = n.dp_id
    and s.Date < n.Date
    order by s.emp_no,s.Date desc
    limit 0,1
    ) as OPEN_BAL,
    n.position as CLOS_BAL
    FROM emp_table as n
    where true
    and n.Date >= '2013-02-01'
    and n.Date <= '2013-02-8'

----up to this it was working if condition part not working----- 
(select
    open_bal,
    if(open_bal<clos_bal,clos_bal-open_bal,0) as buying,
    if(open_bal>clos_bal,open_bal-clos_bal,0) as selling,
    order by buying desc from emp_table
)
Stacked
  • 6,892
  • 7
  • 57
  • 73

1 Answers1

0

You can use CASE in place of the if-condition

SELECT   ...,
         CASE WHEN open_bal < clos_bal THEN ID ELSE open_bal END AS ColumnName,
         ...
FROM     emp_table
Stacked
  • 6,892
  • 7
  • 57
  • 73
Dharmendra
  • 127
  • 12
  • select n.holder,( CASE WHEN open_balclos_bal,open_bal-clos_bal THEN "Selling" END)) AS status from emp_table still not getting help me to get output? – Grace Peter Jun 23 '16 at 14:05