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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:52:48+00:00 2026-05-31T10:52:48+00:00

I have this complex (to me) requirement and would appreciate a SQL query for

  • 0

I have this complex (to me) requirement and would appreciate a SQL query for it. Here’s the deal:

  • There’s this table named “Expenses” in an Access file.
  • The table contains fields as “Category,” “Amount,” “Payer,” and “IsShare.”
  • Category can be Gas, Groceries, Home, etc…
  • Amount is the dollar amount for the expense.
  • Payer can either be me or my brother (“Me” or “Bro”).
  • IsShare can either be true or false. Basically if IsShare is true, then the expense is divided in two. Otherwise, the payer pays for it all.

I need a SQL query to calculate all my expenses for a particular Category. It should be calculated as such:

  • 100% of all non-shared items that I’ve paid (Payer = ‘Me’ AND IsShare = false),
  • 50% of all shared items that I’ve paid (Payer = ‘Me’ AND IsShare = true), and
  • 50% of all shared items that my brother’s paid (Payer = ‘Bro’ AND IsShare = true).

The query should return the sum of all these three items.

So far I have a preliminary query, but it returns incorrect results. Could someone correct it for me?

Thank you very much.

return @"SELECT Sum(Amount) AS TotalAmount " +
        "FROM Expenses " +
        "WHERE Category = 'Groceries' " +
        "AND Payer = 'Me' " +
        "AND Share = false " +
        "GROUP BY Category " +
        "UNION " +
        "SELECT 0.5 * Sum(Amount) AS TotalAmount " +
        "FROM Expenses " +
        "WHERE Category = 'Groceries' " +
        "AND Payer = 'Me' " +
        "AND Share = true " +
        "GROUP BY Category " +
        "UNION " +
        "SELECT 0.5 * Sum(Amount) AS TotalAmount " +
        "FROM Expenses " +
        "WHERE Category = 'Groceries' " +
        "AND Payer = 'Bro' " +
        "AND Share = true ";
        "GROUP BY Category";
  • 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-31T10:52:50+00:00Added an answer on May 31, 2026 at 10:52 am

    I don’t think the UNION is needed for what you are trying to do. This will pass over the data once and put the values in individual columns. You may need to cast or convert the 0.5 to a specific type, I didn’t have a chance to run this.

    SELECT 
         Category,
         SUM(CASE WHEN Payer='me' AND NOT IsShare THEN Amount ELSE 0 END) as IPaid,
         SUM(CASE WHEN Payer='me' AND IsShare THEN Amount * 0.5 ELSE 0 END) as SharedPay,
         SUM(CASE WHEN Payer='bro' AND IsShare THEN Amount * 0.5 ELSE 0 END) as BrotherPay
    FROM Eexpenses
    WHERE Category = 'Groceries'
    GROUP BY Category
    

    For MS ACCESS:

    SELECT 
         Category,
         SUM(IIF(Payer='me' AND NOT IsShare, Amount,0)) as IPaid,
         SUM(IIF(Payer='me' AND IsShare, Amount * 0.5, 0)) as SharedPay,
         SUM(IIF(Payer='bro' AND IsShare, Amount * 0.5, 0)) as BrotherPay
    FROM Eexpenses
    WHERE Category = 'Groceries'
    GROUP BY Category
    

    Add the three columns together:

    SELECT Category, IPaid, SharedPay, BrotherPay, IPay + SharedPay + BotherPay as Total 
    FROM (
        SELECT 
             Category,
             SUM(IIF(Payer='me' AND NOT IsShare, Amount,0)) as IPaid,
             SUM(IIF(Payer='me' AND IsShare, Amount * 0.5, 0)) as SharedPay,
             SUM(IIF(Payer='bro' AND IsShare, Amount * 0.5, 0)) as BrotherPay
        FROM Eexpenses
        WHERE Category = 'Groceries'
        GROUP BY Category
    ) as T1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully this is less complex than I think. I have one table of companies,
I have a complex query that gets executed like this: if ($stmt = $dbi->prepare($pt_query))
I am trying to do rather complex SQL query to produce a report. This
I have this piece of code: var myObj = function () { this.complex =
I have this long and complex source code that uses a RNG with a
I have a relatively complex .htaccess file to control page requests, this currently redirects
If you have a complex requirement set with many users(&servers) how will your websocket
I have a complex Clojure data structure that I would like to serialize -
We have a requirement to be able to access resources on a mobile device.
I have this code in jQuery, that I want to reimplement with the prototype

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.