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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:42:39+00:00 2026-05-11T17:42:39+00:00

I am using SQLite in an application that I am developing. I am trying

  • 0

I am using SQLite in an application that I am developing.
I am trying to run a pretty complex query (complex for me!) and I have gotten the basic results I need but I am stuck on getting over the last hurdle.

I presently have this query that does what i need it to do…

SELECT SUM(activity)
FROM activities
WHERE activity_id IN(SELECT name_id FROM foods GROUP BY name_id HAVING SUM(points) > 20);  

I need to add another part to this query but this is where it has gotten a little complicated for me. There are three tables….dates, foods, activities and i need to find the sum of a result set which contains the minimum number between two values from two different tables as long as a certain statement is true.

Basically..

SELECT SUM(total) FROM (SELECT MIN(value from table1 which is determined by a value in table2, value from table3) AS total
FROM table3
WHERE value from table3 is contained in a result set from table1);

The below query is something I came up with that would work if there was no syntax whatsoever (lol!). This doesn’t work but i just wanted to show it to better understand what I’m trying to do.

SELECT SUM(activity_amount) FROM (SELECT min((SELECT SUM(points) - 20 FROM foods WHERE name_id IN(SELECT pk FROM dates WHERE weekly=1) GROUP BY name_id), activity) AS activity_amount
FROM activities
WHERE activity_id IN(SELECT name_id FROM foods GROUP BY name_id HAVING SUM(points) > 20));

The problem is with the first value in the MIN()….

SELECT SUM(points) - 20 FROM food WHERE name_id IN(SELECT pk FROM dates WHERE weekly=1) GROUP BY name_id

That statement yields more than one value but even though I do need those values to compare against others in the MIN(), I only need them one at a time…not as a whole set.

How can I get something like the above query I created to work?

EDIT…some example tables to better help. Thanks jellomonkey and hainstech

Table#1(dates)  

CREATE TABLE dates (pk INTEGER PRIMARY KEY, date INTEGER, weekly INTEGER)
pk  date       weekly
1   05062009    1  
2   05072009    1  
3   05082009    2 

Table #2(foods)  

CREATE TABLE foods (pk INTEGER PRIMARY KEY, food VARCHAR(64), points DOUBLE, name_id INTEGER)
pk   food   points   name_id  
 1   food1    12.0     1  
 2   food2    9.0      1  
 3   food3    5.0      1  
 4   food4    15.0     2
 5   food5    14.0     2  
 6   food6    12.0     3  

Table#3(activities)  

CREATE TABLE activities (pk INTEGER PRIMARY KEY, activity DOUBLE, activity_id INTEGER)
pk   activity   activity_id
 1     5.0           1  
 2     4.0           1  
 3     2.0           2  
 4     4.0           3  

With this ex and query from my original post (one that doesn’t work), I would be looking for a result set containing one value..8.0

MIN(26.0-20, 9.0) = 6.0
MIN(29.0-20, 2.0) = 2.0
6.0 + 2.0 = 8.0

I hope this helps!

  • 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-11T17:42:40+00:00Added an answer on May 11, 2026 at 5:42 pm

    After much head-scratching and divining I suspect what you want might be:

    SELECT SUM(MIN(fp-20, ap)) FROM
      (SELECT dates.pk AS fd, SUM(points) AS fp
      FROM dates
      JOIN foods ON name_id = fd
      GROUP BY fd
      HAVING fp >= 20)
        JOIN
      (SELECT dates.pk AS ad, SUM(activity) AS ap
      FROM dates
      JOIN activities ON activity_id = ad
      GROUP BY ad)
        ON fd = ad
    

    The column names appear to have zero connection to their meaning but, hey, at least this does give 8.0 and the subselects give the other numbers you mention!-)

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't necessarily need their permission. Install svn on your… May 11, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer The solution here is so simple, it's annoying that dev's… May 11, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer As simple as this: if($.browser.msie && $.browser.version=="6.0") alert("Im the annoying… May 11, 2026 at 11:51 pm

Related Questions

I am building application that required some data from iPhone's Call log(read only). The
What is the correct way to work with SQLite in WPF and ASP.NET MVC?
In short: How do you specify a BIGINT in Django models? In a current
I have a reference application that I use to work through DDD issues, and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.