我正在尝试使用if条件计算查询,我需要显示两个值作为买卖。
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
)
答案 0 :(得分:0)
您可以使用CASE
代替if-condition
SELECT ...,
CASE WHEN open_bal < clos_bal THEN ID ELSE open_bal END AS ColumnName,
...
FROM emp_table