views:

1966

answers:

2

HI,
New to StackOverflow...
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 tho 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?

Thanks for any help you might be able to provide

EDIT...some ex 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  A: 

Agree with jellomonkey - I can't understand exactly what you want unless you show more info. Simple table definitions, a few rows of example data, and expected output would help.

Here is a simple example for getting a min value from a table based on a range whose bounds are defined in two other tables:

create table t1(t1id int primary key, value int)
create table t2(t1id int, t2id int primary key, value int)
create table t3(t2id int, t3id int primary key, value int)

select
    MIN(t1.value)
from table1 as t1 
    join table2 as t2 on t2.t1id = t1.t1id
    join table3 as t3 on t3.t2id = t2.t2id
where t1.value between t2.value and t3.value

This uses t2.value and t3.value as the bounds of the range to find the MIN() in.

EDIT - thanks for posting the sample tables, that helps a ton. You could use some convoluted CASE statements to compare the two values, but I would go with bringing them into one rowset and doing a normal MIN aggregation. This will be more readable and will also allow you to more easily add an additional set of info to include in your MIN inputs if need be. So here is a quick whack at what I would do:

select
    SUM(activity)
from (
    select
     min(activity) as activity
     ,activity_id
    from (
     select
      SUM(activity) as activity, activity_id
     from activities 
     group by activity_id
     union all
     select
      SUM(points)-20
      ,name_id
     from foods 
     group by name_id
        having SUM(points) > 20
    ) as activitySum
    where activity_id in (select pk from dates where weekly=1)
    group by activity_id
) as activityLesserOf

Edit #2 - added HAVING to above query per the needs clarification

hey thanks...i was already in middle if adding the example tables when i saw your post so i just finished it anyway. I will try and implement your suggestion. Anyway, I edited my OP to include an example
freddyD
this worked as the query is designed but actually i needed something a little different and i guess thats my fault for not explaining it better. In my application where i am gonna implement this, I only care about the name_id's where the points is greater than 20. Taking the sum(points) and subtracting 20 would only be done where points is greater than 20. I really appreciate it because this looked great and i wish i was good enough with sql to know where to add a part to make this happen...any thoughts?
freddyD
@freddyD - if I understand right, you want to filter out all aggregations from the foods table where the SUM() is less than 20, right? You filter on post-aggregation values with HAVING. It is basically the same thing as WHERE, except WHERE is applied before the aggregation, and HAVING is applied after it. I added it to the above query, let us know if this is what you are looking for.
thanks for the effort you put into this. Your query gave the right final result...8.0 but it didn't give the same results of the subsets, which is something i needed also. Im not sure where the query goes wrong in what i need but it has been helpful regardless...I have learned a little more about sql studying this and Alex's query below. I dont want to waste any more of your time because i ended up using Alex's query anyway. Thanks so much for your time!!!
freddyD
+2  A: 

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!-)

Alex Martelli
Alex, thanks alot!! this worked awesome. The only other thing I would ask of you is this query takes into account all the pk's from dates. How would I only retrieve the pk's of dates where "weekly=1"?? I guess where you have "...FROM dates JOIN foods ON name_id....", I can make that "....FROM dates WHERE weekly=1 JOIN foods ON name_id....". Will that work?
freddyD
Alex, I'm thinking about it some more and i don't think that will work what i suggested, right? I think that will cause an error with JOIN?
freddyD
syntactically, `WHERE dates.weekly = 1` goes _after_ the JOIN and you need it in both inner SELECTs.
Alex Martelli
Thanks alot!!! Works perfect!!
freddyD