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 8275177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:59:08+00:00 2026-06-08T07:59:08+00:00

select content_type_code_id , ABS(price) AS price , SUM(case when price >= 0 THEN 1

  • 0
select content_type_code_id 
    , ABS(price) AS price
    , SUM(case when price >= 0 THEN 1 ELSE 0 END) AS debits
    , SUM(case when price < 0 THEN 1 ELSE 0 END) AS credits
from dbo.transaction_unrated  
where transaction_date >= '2012/05/01'
      and transaction_date < '2012/06/01'
      and content_provider_code_id in (1)
group by content_type_code_id, ABS(price) 
ORDER BY ABS(price) ASC  

The above query produces the following output:

content_type_code_id    price   debits  credits
1                      0.00      317    0
1                      0.99      178    1
1                      1.99      786    1

But I want something like this:

content_type_code_id    price   debits  credits NetCount
    1                      0.00      317    0       317 
    1                      0.99      178    1       177 
    1                      1.99      786    1       785

Where NetCount = (debits – credits)

When I try to create another column for that I get an error.

  • 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-08T07:59:11+00:00Added an answer on June 8, 2026 at 7:59 am

    Just add:

    SUM(case when price >= 0 THEN 1 ELSE 0 END) - 
        SUM(case when price < 0 THEN 1 ELSE 0 END) AS NetCount
    

    as your last statement, so you’d end up with this:

    select content_type_code_id 
        , ABS(price) AS price
        , SUM(case when price >= 0 THEN 1 ELSE 0 END) AS debits
        , SUM(case when price < 0 THEN 1 ELSE 0 END) AS credits
        , SUM(case when price >= 0 THEN 1 ELSE 0 END) - 
            SUM(case when price < 0 THEN 1 ELSE 0 END) AS NetCount
    from dbo.transaction_unrated  
    where transaction_date >= '2012/05/01'
          and transaction_date < '2012/06/01'
          and content_provider_code_id in (1)
    group by content_type_code_id, ABS(price) 
    ORDER BY ABS(price) ASC  
    

    Derived table version for Lamak:

    You can also use a derived table to make the code a little cleaner:

    select content_type_code_id,
        price, debits, credits, (debits - credits) as NetCount
    from (
        select content_type_code_id 
            , ABS(price) AS price
            , SUM(case when price >= 0 THEN 1 ELSE 0 END) AS debits
            , SUM(case when price < 0 THEN 1 ELSE 0 END) AS credits
        from dbo.transaction_unrated  
        where transaction_date >= '2012/05/01'
              and transaction_date < '2012/06/01'
              and content_provider_code_id in (1)
        group by content_type_code_id, ABS(price) 
    ) YourDerivedTable
    ORDER BY price ASC  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have queried a table with the following query select content_type_code_id , price ,
TOPIC_COUNT_SQL = SELECT COUNT(*) FROM topics_topic WHERE topics_topic.object_id = maps_map.id AND topics_topic.content_type_id = %s
I have this SQL query from the logs select content = 5%id%201=1 from pages
I have two mysql queries: $sql = SELECT * FROM content WHERE threadName LIKE
What am I doing wrong with this: $sql = SELECT * FROM content WHERE
After running this query statement with my database: select * from articles where content
i have this code $md_query = SELECT * FROM table ORDER BY id ASC;
Look at this code: $sql = $this->getAdapter()->select() ->from(array('mab' => 'module_adv_banner')) ->joinLeft(array('mafb' => 'module_adv_filebanner'),'mab.id =
Using: select name, id from TEST where id :2 AND name :1 order by
content_type = ContentType.objects.get_for_model(Map) maps = maps.extra(select=SortedDict([ ('member_count', MEMBER_COUNT_SQL), ('topic_count', TOPIC_COUNT_SQL), ]), select_params=(content_type.id,)) and the

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.