Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:19:41+00:00 2026-06-12T00:19:41+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:19:43+00:00Added an answer on June 12, 2026 at 12:19 am

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query working but it is not returning back exactly what I
Using the following query, I can't figure out why my update is not working.
I have this working query that outputs URL and click counts from database. <?php
I have this working query: $q = $this->db->query('SELECT u.name FROM users u JOIN user_group
i have been working on query which uses compute by clause for avg and
I have below a working query that needs to be simplified. The reason is
I've got a rather lengthy query i have been working with that is throwing
I am having trouble writing a query. I have been working with UNION on
I am working on a query, and have found minimal documentation from Microsoft on
I have the following query which is working fine, but I also want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.