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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:42:02+00:00 2026-05-18T19:42:02+00:00

I have been struggling with getting this query right for hours now. I have

  • 0

I have been struggling with getting this query right for hours now. I have a huge amount of data and I want to show just the departments with IDs 10,15,18 and 25. From here, I want to subtract the profits for each dept with ID 18 from 15, i.e. 15-18.

I filter the data with the following query:

SELECT * FROM deptTable WHERE ID IN(10,15,18,25) AND date = ‘2009-01-25’

dept ———- date —————- ID —————– profit
UK ———- 2009-01-25 ———- 10 —————- 2000
Brazil ——- 2009-01-25 ———- 10 —————- 1300
Japan——- 2009-01-25 ——— 10 —————- 2500
Spain——- 2009-01-25 ———- 10 —————- 3200
UK ———- 2009-01-25 ———- 15 —————- 4000
Brazil ——- 2009-01-25 ———- 15 —————- 1700
Japan——- 2009-01-25 ———- 15 —————- 3500
Spain——– 2009-01-25 ———- 15 ————— 1200
UK ———- 2009-01-25 ———- 18 —————- 2500
Brazil ——- 2009-01-25 ———- 18 —————- 1300
Japan——- 2009-01-25 ——— 18 —————- 2120
Spain——- 2009-01-25 ———- 18 —————- 800

UK ———- 2009-01-25 ———- 25 —————- 3000
Brazil ——- 2009-01-25 ———- 25 —————- 1850
Japan——- 2009-01-25 ——— 25 —————- 1580
Spain——– 2009-01-25 ———- 25 ————— 1070

What I basically want is to then subtract each row with ID 18 from rows with ID 15. Taking the UK as an example:

4000 – 2500 = 1500, doing the rest of the dept regions will give the desired result which is:

dept ———- date —————- ID —————– profit
UK ———- 2009-01-25 ———- 10 —————- 2000
Brazil ——- 2009-01-25 ———- 10 —————- 1300
Japan——- 2009-01-25 ——— 10 —————– 2500
Spain——- 2009-01-25 ———- 10 —————- 3200
UK ———- 2009-01-25 ———- 15-18 ———— 1500
Brazil ——- 2009-01-25 ———- 15-18 ———— 400
Japan——- 2009-01-25 ———- 15-18 ———– 1380
Spain——– 2009-01-25 ———- 15-18 ———– 400

UK ———- 2009-01-25 ———- 25 —————- 3000
Brazil ——- 2009-01-25 ———- 25 —————- 1850
Japan——- 2009-01-25 ———- 25 —————- 1580
Spain——– 2009-01-25 ———- 25 ————— 1070

2 points:
1. The calculated rows ID column doesn’t have to read ’15-18′, I’ve just typed ’15-18′ to help explain the issue
2. The italics/bold are the only calculated rows, all other rows remain the same

Surely something like this is possible?

Thanks,

  • 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-05-18T19:42:02+00:00Added an answer on May 18, 2026 at 7:42 pm

    I think that something like this will work…

    SELECT a.dept, a.date, IF(a.id=15,'15-18',a.id) AS id, IF(b.profit IS NULL,a.profit,a.profit-b.profit) AS profit
    FROM deptTable a 
    LEFT JOIN deptTable b ON a.ID=15 AND b.ID=18 AND a.dept=b.dept
    WHERE a.ID IN(10,15,25) AND a.date = '2009-01-25' 
    

    Tested result:

    +--------+------------+-------+--------+
    | dept   | date       | id    | profit |
    +--------+------------+-------+--------+
    | UK     | 2009-01-25 | 10    |   2000 |
    | Brazil | 2009-01-25 | 10    |   1300 |
    | JAPAN  | 2009-01-25 | 10    |   2500 |
    | SPAIN  | 2009-01-25 | 10    |   3200 |
    | UK     | 2009-01-25 | 15-18 |   1500 |
    | Brazil | 2009-01-25 | 15-18 |    400 |
    | JAPAN  | 2009-01-25 | 15-18 |   1380 |
    | SPAIN  | 2009-01-25 | 15-18 |    400 |
    | UK     | 2009-01-25 | 25    |   3000 |
    | Brazil | 2009-01-25 | 25    |   1850 |
    | JAPAN  | 2009-01-25 | 25    |   1580 |
    | SPAIN  | 2009-01-25 | 25    |   1070 |
    +--------+------------+-------+--------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling with this for a while now. given a set of
I have been struggling with Core Data sigh so I decided to work this
I have been struggling with this error for more than two hours: error: aggregate
I have been struggling with this question for awhile now, and I haven't reached
I've been struggling with this problem for 5 hours and I have a feeling
I have been struggling with this problem for a few days now and I
I have been struggling with getting this to work for a number of days
i have been struggling getting this to work for 2 weeks, I am trying
Really been struggling with this one for some time now, i have many text
I have been struggling for almost three days now on this task, and I

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.