2

I have an working query and not sure if its efficient or not.

Table columns:

  1. p_type: can be STRING sell, cancelsell, bank, cancelbank
  2. id
  3. fid
  4. unit

Operation to be performed:

  1. SUM(unit) for each process type sell,cancelsell,bank,cancelbank.
  2. SUM(UNIT for sell) - SUM(unit for cancelsell) , SUM(unit for bank) - SUM(unit for cancelbank)

NOTE: I am also checking for id and fid in each select though I need to check with same values for all select.

The existing query:

    select sell-cancelsell scount, bank-cancelbank bcount from

    (select sum(a.unit) as sell from table1 a where   
    a.p_type = 'Sell' and a.id=1 and a.fid=2 ),

    (select sum(c.unit) as cancelsell from table1  c where      
    c.p_type = 'CancelSell' and c.id=1 and c.fid=2),

    (select sum(b.unit) as bank from table1  b where     
    b.p_type = 'Bank' and b.id=1 and b.fid=2),

    (select sum(d.unit) as cancelbank from table1  d where  
    d.p_type = 'CancelBank' and d.id=1 and d.fid=2)

Is good enough? If anyone can suggest a way to make it more efficient that would be great.

3 Answers 3

5

You can do this

select 
sum(Case when a.p_type = 'sell' then a.unit else null end) as sellUnit, 
sum(Case when a.p_type = 'CancelSell' then a.unit else null end) as CancelSellUnit,
sum(Case when a.p_type = 'Bank' then a.unit else null end) as BankUnit ,
sum(Case when a.p_type = 'CancelBank' then a.unit else null end) as CancelBankUnit  
from table1 a where and a.id=1 and a.fid=2 
Sign up to request clarification or add additional context in comments.

1 Comment

@rs. thank you very much. I knew that I had to use Case, but was not getting it quite right.thank you again
2

try this:

SELECT 
        SUM(CASE WHEN P_TYPE = 'SELL' THEN UNIT END) - 
        SUM(CASE WHEN P_TYPE = 'CANCELSELL' THEN UNIT END) AS SCOUNT,
        SUM(CASE WHEN P_TYPE = 'BANK' THEN UNIT END) -
        SUM(CASE WHEN P_TYPE = 'CANCELBANK' THEN UNIT END) AS BCOUNT 
FROM    TABLE1  
WHERE   ID=1 AND FID=2 

1 Comment

Thank you. I actually used CASE this way ly!
0
select temp1.sumunit - temp2.sumunit,temp3.sumunit-temp4.sumunit from
(select p_type,sum(unit) as sumunit from table1
group by p_type) as temp1
inner join
(select p_type,sum(unit) as sumunit from table1
group by p_type) as temp2 on temp2.p_type = 'cancelsell' 
inner join
(select p_type,sum(unit) as sumunit from table1
group by p_type) as temp3 on temp3.p_type = 'bank'
inner join
(select p_type,sum(unit) as sumunit from table1
group by p_type) as temp4 on temp4.p_type='cancelbank'
where temp1.p_type='sell'
and temp1.id = 1 and temp1.fid = 2
and temp2.id = 1 and temp2.fid = 2
and temp3.id = 1 and temp3.fid = 2
and temp4.id = 1 and temp4.fid = 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.