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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:12:15+00:00 2026-06-17T14:12:15+00:00

I need a query that subtracts or adds according to a condition for SQLite

  • 0

I need a query that subtracts or adds according to a condition for SQLite using the CASE statement. This is my query at the moment:

SELECT s.companyName, CASE ia.type 
                      WHEN ia.type='add' THEN SUM(ia.quantity) 
                      WHEN ia.type='subtract' THEN SUM(-ia.type) 
                      ELSE SUM(0) END AS total
FROM stocktake s 
LEFT JOIN stocktake_adjustment sa ON s.stocktakeId = sa.stocktakeId
LEFT JOIN adjustment a ON a.adjustmentId = sa.adjustmentId
LEFT JOIN inventory_adjustment ia ON ia.adjustmentId = a.adjustmentId
LEFT JOIN inventory i ON i.inventoryId = ia.inventoryId 
LEFT JOIN supplier s ON s.supplierId = i.supplierId 
WHERE s.supplierId = '4da99b63-fcb9-9b9f-8415-4896caeb920c'; 

Basically I want to add if the condition is ‘add’ or subtract if condition is subtract, from the total.

Thank you in advance guys.

  • 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-17T14:12:16+00:00Added an answer on June 17, 2026 at 2:12 pm

    Move your CASE into SUM:

    SELECT s.companyName, SUM(CASE ia.type 
                              WHEN ia.type='add' THEN ia.quantity
                              WHEN ia.type='subtract' THEN -ia.quantity 
                              ELSE 0 
                              END)
                          AS total
    FROM stocktake s 
    LEFT JOIN stocktake_adjustment sa ON s.stocktakeId = sa.stocktakeId
    LEFT JOIN adjustment a ON a.adjustmentId = sa.adjustmentId
    LEFT JOIN inventory_adjustment ia ON ia.adjustmentId = a.adjustmentId
    LEFT JOIN inventory i ON i.inventoryId = ia.inventoryId 
    LEFT JOIN supplier s ON s.supplierId = i.supplierId 
    WHERE s.supplierId = '4da99b63-fcb9-9b9f-8415-4896caeb920c';
    

    (I assumed that -ia.type in the original question was a typo, really meaning -ia.quantity)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a SQLite query that searches 1 field only using LIKE. Basic example:
Database has tables Photos and PhotoAlbums. I need a query that will select all
I need to setup a SELECT query that will return rows based on matching
I'm having a problem with my MySQL statement. I need a query that counts
I need a query that can sum like this (previous row with current row)
Trying to sort this out... I need a query that will combine both tables
Ideally I need a query that is equivalent to select * from customer where
I need a query that will return a table where each column is the
Need help with a query that I wrote: I have three tables Company id
I need to write a query that returns all object less that or equal

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.