| Author |
Topic |
|
imbrod
Starting Member
10 Posts |
Posted - 2006-09-28 : 04:35:02
|
| Can someone help me out on this:I have a table 'articles'ID --- date --- authorID1 --- 20060901 --- 12 --- 20060902 --- 13 --- 20060903 --- 34 --- 20060904 --- 35 --- 20060905 --- 2I need a query (ID, date, authorID) that will produce the records that equals maximum date for each author (distinct authors), ordered by date descending.For example, output should be in this case:5 --- 20060905 --- 24 --- 20060904 --- 32 --- 20060902 --- 1Hope that you understand what I mean...Please help!P.S. Don't worry about using the reserved word in the field name 'date', it has another original name... |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-28 : 04:49:38
|
| select a.* from articles a inner join(select authorid, max(date) md from articles group by authorid) z on z.authorid = a.authorid and z.md = a.datePeter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-28 : 04:50:53
|
select id, t2.date, authoridfrom tbl t1join(select authorid, max(date) as datefrom tblgroup by authorid) as t2on t1.date = t2.date andt1.authorid = t1.authorid Not again, Peter !!!Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-28 : 05:01:38
|
| No worry! Next posting from you will be half K!Peter LarssonHelsingborg, Sweden |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-28 : 08:19:27
|
Peso is always faster MadhivananFailing to plan is Planning to fail |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-28 : 09:06:51
|
quote: Originally posted by madhivanan Peso is always faster MadhivananFailing to plan is Planning to fail
I couldn't agree more and still always has nice solution !!Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
imbrod
Starting Member
10 Posts |
Posted - 2006-09-28 : 09:25:38
|
| Wow - that was fast replies!Thank you all! I'll check it out and let you know. |
 |
|
|
imbrod
Starting Member
10 Posts |
Posted - 2006-09-28 : 15:33:37
|
quote: Originally posted by Peso select a.* from articles a inner join(select authorid, max(date) md from articles group by authorid) z on z.authorid = a.authorid and z.md = a.datePeter LarssonHelsingborg, Sweden
It works like a clock!!!(I just added ORDER BY a.date DESC to finish my goal)Thanks again!(I should put 'SOLVED' on this post but don't know how) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-28 : 21:08:30
|
| Edit your question and Change the subjectMadhivananFailing to plan is Planning to fail |
 |
|
|
imbrod
Starting Member
10 Posts |
Posted - 2006-09-30 : 09:41:35
|
| Would it be too much trouble if I ask you to refine the query so I can get authorName as well from joining with another table?authorsauthorID --- authorName1 --- AAAA2 --- BBBB3 --- CCCCAlong with the fields I mentioned (articles.ID, articles.date, articles.authorID), I need also authors.authorName? |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-30 : 09:52:44
|
You just have join to Authors table as well:select t1.id, t2.date, t2.authorid, au.AuthorNamefrom tbl t1join(select authorid, max(date) as datefrom tblgroup by authorid) as t2on t1.date = t2.date andt1.authorid = t1.authoridjoin Authors auon t2.authorid = au.authorid Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
imbrod
Starting Member
10 Posts |
Posted - 2006-09-30 : 10:09:51
|
| Sorry, it doesn't work...Could you rewrite the query with the field names I provided, please?Maybe this line is wrong:t1.authorid = t1.authorid?articles:ID --- date --- authorID --- title --- text1 --- 20060901 --- 1 --- aaa --- aaa12 --- 20060902 --- 1 --- bbb --- aaa13 --- 20060903 --- 3 --- ccc --- aaa14 --- 20060904 --- 3 --- ddd --- aaa15 --- 20060905 --- 2 --- eee --- aaa1authors:authorID --- authorName1 --- aaaaaa2 --- bbbbbb3 --- ccccccSo basically I need:- articles.ID, articles.date, articles.title, articles.text, authors.authorName- show only records of each author that has newest (highest) date for that author (1 record for 1 author)- order all the records by date descendingThanks in advance! |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-30 : 10:28:58
|
may be this will solve your problem:select t1.id, t2.date, t2.authorid, t1.text, au.AuthorNamefrom Articles t1join(select authorid, max(date) as datefrom Articlesgroup by authorid) as t2on t1.date = t2.date andt1.authorid = t2.authoridjoin Authors auon t2.authorid = au.authoridorder by t2.date Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
imbrod
Starting Member
10 Posts |
Posted - 2006-09-30 : 10:45:52
|
| Sorry, doesn't work.Maybe it's because I use MS Access and it probably can't put 3 JOINs together...I made a solution with 2 recordsets (2 queries) finally.Thanks again! |
 |
|
|
|