tags:

views:

74

answers:

2
SELECT * FROM a 
JOIN (SELECT * FROM b WHERE b.aId = a.Id) AS c ON c.aId = a.Id

It says does not recognize: a.Id in the Where Clause.

I know its probably cause im using a temp table and a.Id cannot be passed through but is there any way we can do that?

Because here is what actually happens

SELECT * 
  FROM a 
  JOIN (SELECT * FROM b 
         WHERE b.aId = a.Id 
      ORDER BY b.dateReg DESC
         LIMIT 1) AS c ON c.aId = a.Id

I need the ORDER BY b.dateReg DESC LIMIT 1 as it returns me the last row that assosiates with the a Table.. If you require i can post the Create Query

+2  A: 

-- find last rows on b

select * from b x 
where exists(
     select id 
     from b y 
     where y.id = b.id 
     having max(y.dateReg) = x.dateReg
     group by id
     )

-- then join that b to a, this is the final query:

select * from a
join 
(
     select * from b x 
     where exists(
         select id 
         from b y 
         where y.id = b.id 
         having max(y.dateReg) = x.dateReg
         group by id
         )

) as last_rows on last_rows.id = a.id

-- simpler:

select * 
from a join b x on a.id = x.id
where exists(
    select id
    from b y
    where y.id = b.id
    having max(y.dateReg) = x.dateReg
    group by id)

-- or if you will use postgres:

select DISTINCT ON (a.id) * 
from a join b x on a.id = x.id
order by a.id, b.dateReg DESC
-- look ma! no group by!    

-- nothing beats postgresql's simplicity :-)
Michael Buen
You see i did have the query working and it was returning a screw load of rows... i wish we could do it so it only returns the last.. and we can limit it
Shahmir Javaid
Il accept yours if there are no others
Shahmir Javaid
yeah i see the first code is not working, i'm in a hurry to have lunch. i edited it now, i think the corrected code is correct
Michael Buen
Ty Ty :D.... Accepted
Shahmir Javaid
Ahhh crap... acidently removed the vote.. will try later :(.. Something about vote too old to change
Shahmir Javaid
i pseudo-edit my answer, answer become non-old, perhaps you can upvote it again :-)
Michael Buen
I voted. I read that there might still be a bug with reversing a downvote...
OMG Ponies
Done Mr Micheal :D... Sry didnt mean to do it to begin with
Shahmir Javaid
@rexum: With all great systems come Bugs... :D it wont be great if it didnt have bugs...lol... The morale i suppose is.. if its perfect its crap...Needs to Have Bugs
Shahmir Javaid
+1  A: 

Try:

SELECT DISTINCT *
  FROM A
  JOIN B b ON b.aid = a.id
  JOIN (SELECT b.aid,
               MAX(b.datereg) 'max_datereg'
          FROM B b
      GROUP BY b.aid) md ON md.aid = b.aid
                        AND md.max_datereg = b.datereg

If you do want the first record associated with the associate, use:

SELECT DISTINCT *
  FROM A
  JOIN B b ON b.aid = a.id
  JOIN (SELECT b.aid,
               MIN(b.datereg) 'min_datereg'
          FROM B b
      GROUP BY b.aid) md ON md.aid = b.aid
                        AND md.min_datereg = b.datereg
OMG Ponies