views:

41

answers:

3

I have a table svn1:

id | date | startdate

23 2002-12-04 2000-11-11
23 2004-08-19 2005-09-10
23 2002-09-09 2004-08-23

select id,startdate from svn1 where startdate>=(select max(date) from svn1 where id=svn1.id);

Now the problem is how do I let know the subquery to match id with the id in the outer query. Obviously id=svn1.id wont work. Thanks!

If you have the time to read more:

This really is a simplified version of asking what I really am trying to do here. my actual query is something like this

select id,count(distinct archdetails.compname) from svn1,svn3,archdetails where svn1.name='ant' and svn3.name='ant' and archdetails.name='ant' and type='Bug' and svn1.revno=svn3.revno and svn3.compname=archdetails.compname and ( (startdate>=sdate and startdate<=edate) or (sdate<=(select max(date) from svn1 where type='Bug' and id=svn1.id) and edate>=(select max(date) from svn1 where type='Bug' and id=svn1.id)) or (sdate>=startdate and edate<=(select max(date) from svn1 where type='Bug' and id=svn1.id)) ) group by id LIMIT 0,40;

As you notice select max(date) from svn1 where type='Bug' and id=svn1.id has to be calculated many times.

Can I just calculate this once and store it using AS and then use that variable later. Main problem is to correct id=svn1.id so as to correctly equate it to the id in the outer table.

A: 

Try using alias

Something like that should work

select s.id,s.startdate from svn1.s where s.startdate>=(select max(date) from svn1.s2 where s.id=s2.id);

Yasen Zhelev
A: 

I'm not sure you can eliminate the repetition of the subquery, but the subquery can reference the main query if you use a table alias, as in the following:

select id,
       count(distinct archdetails.compname)
from svn1 s1,
     svn3 s3,
     archdetails a
where s1.name='ant' and
      s3.name='ant' and
      a.name='ant' and
      type='Bug' and
      s1.revno=s3.revno and
      s3.compname = a.compname and
      ( (startdate >= sdate and startdate<=edate) or
        (sdate <= (select max(date)
                     from svn1
                     where type='Bug' and
                           id=s1.id and
         edate>=(select max(date)
                   from svn1
                   where type='Bug' and
                   id=s1.id)) or
        (sdate >= startdate and edate<=(select max(date)
                                          from svn1
                                          where type='Bug' and
                                          id=s1.id)) )
group by id LIMIT 0,40;

Share and enjoy.

Bob Jarvis
Thanks a lot. This is exactly what I was looking for. But this has really increased the execution time now. I don't have an index on table though. What would be the best columns to create index on just by looking at the query? or using any other method?
Gaurav
@Gaurav: please add the definitions of these tables to your original question. It's hard to make recommendations without understanding what the tables look like. Thanks.
Bob Jarvis
A: 

You should be able to left join to a sub-select so you only run the query once. Then you can do a join condition to pull out the maximum for the ID on each record as shown below:

SELECT id,
       COUNT(DISTINCT archdetails.compname)
FROM   svn1,
       svn3,
       archdetails
LEFT JOIN (
        SELECT id, MAX(date) AS MaximumDate
          FROM   svn1
          WHERE  TYPE = 'Bug'
          GROUP BY id
       ) AS MaxDate ON MaxDate.id = svn1.id
WHERE  svn1.name = 'ant'
       AND svn3.name = 'ant'
       AND archdetails.name = 'ant'
       AND TYPE = 'Bug'
       AND svn1.revno = svn3.revno
       AND svn3.compname = archdetails.compname
       AND (
               (startdate >= sdate AND startdate <= edate)
               OR (
                      sdate <= MaxDate.MaximumDate
                      AND edate >= MaxDate.MaximumDate
                  )
               OR (
                      sdate >= startdate
                      AND edate <= MaxDate.MaximumDate
                  )
           )
GROUP BY
       id LIMIT 0,
       40;
orvado
It is unable to recognize svn1.id in `MaxDate.id = svn1.id`. I tried changing it to `FROM svn1 t` and `MaxDate.id = t.id` but that doesnot seem to work either.>Error:Unknown column 't.id' in 'on clause'
Gaurav