Getting Unique Results [message #619460] |
Tue, 22 July 2014 14:26 |
|
itcoracle
Messages: 3 Registered: July 2014
|
Junior Member |
|
|
I am using the following query to get a bunch of data:
SELECT DISTINCT table2.xid, table1.date from table1 LEFT JOIN table2 ON table1.id = table2.id
ORDER BY table2.xid ASC, table1.date DESC
Results:
table2.xid table1.date
101 7/21/14
102 7/21/14
102 6/26/14
102 5/05/14
103 6/18/14
104 5/01/14
104 3/19/14
What I can't figure out how to do is limit the results to only show the rows with unique XIDs and each XID is associated with the latest dates. Meaning, if an XID has multiple dates associated with it, only show the row of data with the latest date giving the following results.
Desired Results:
table2.xid table1.date
101 7/21/14
102 7/21/14
103 6/18/14
104 5/01/14
I need to do this with a single query too. In other words, I can't use any other language to provide the logic to sort the data. I need to feed one query to the db and have it spit out the above results.
Any Ideas?
|
|
|
|
Re: Getting Unique Results [message #619473 is a reply to message #619463] |
Tue, 22 July 2014 16:42 |
|
itcoracle
Messages: 3 Registered: July 2014
|
Junior Member |
|
|
Ok so I over-simplified my query for the purposes of my question thinking it wouldn't matter but apparently it does, so let me try this again. Sorry!
The MAX and GROUP BY work just fine in my original example, however, I have a nested JOIN statement and when I add it to the mix, it breaks:
Works without the GROUP just shows me rows I don't need:
SELECT DISTINCT
table1.xid,
table2.date,
table3.description
FROM table1
LEFT JOIN (table2 LEFT JOIN table3 ON table2.pid = table3.pid)
ON table1.id = table3.id
ORDER BY table1.xid ASC, table2.date DESC
Perhaps I am putting it in the wrong place but I have tried several different ways and I'm not sure what is wrong:
With the MAX and GROUP BY but breaks: "not a GROUP BY expression"
SELECT DISTINCT
table1.xid,
MAX(table2.date),
table3.description
FROM table1
LEFT JOIN (table2 LEFT JOIN table3 ON table2.pid = table3.pid)
ON table1.id = table3.id
GROUP BY table1.xid
ORDER BY table1.xid ASC, table2.date DESC
In order for me to not get the error, I have to add all of the selected items to the GROUP BY statement (GROUP BY table1.xid, table2.date, table3.description) which makes it ineffective.
Any ideas?
|
|
|
|
|
|
|
Re: Getting Unique Results [message #619598 is a reply to message #619499] |
Wed, 23 July 2014 12:44 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Something like that:
SELECT table1.xid, table2.max_date, table3.description
FROM table1
LEFT JOIN
(select table2.pid, max(table2.date) max_date from table2 group by table2.pid) table2
ON table2.pid = table1.id
LEFT JOIN
table3
ON table3.pid = table1.id
ORDER BY table1.xid ASC, table2.max_date DESC
But, of course, as we don't know the actual tables definition and their relations (above all on these id, xid, pid, I wonder if the "table2.pid = table1.id" you posted is not "table2.pid = table1.xid"), it may not be exactly that.
[Updated on: Wed, 23 July 2014 12:45] Report message to a moderator
|
|
|
Re: Getting Unique Results [message #619623 is a reply to message #619598] |
Wed, 23 July 2014 14:24 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
In general, your solution will not return single row per table1.xid. We don't know if table1 to table2 relationship is one-to-one or one-to-many or many-to-many. We don't know if table1 to table3 relationship is one-to-one or one-to-many or many-to-many. So generic solution would be:
SELECT table1.xid,
max(table2.date) max_date,
max(table3.description) keep(dense_rank last order by table2.date) description
FROM table1
LEFT JOIN
table2
ON table2.pid = table1.id
LEFT JOIN
table3
ON table3.pid = table1.id
ORDER BY table1.xid
/
SY.
|
|
|
|
|
Re: Getting Unique Results [message #619678 is a reply to message #619623] |
Thu, 24 July 2014 06:09 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Oops, I missed GROUP BY clause:
SELECT table1.xid,
max(table2.date) max_date,
max(table3.description) keep(dense_rank last order by table2.date) description
FROM table1
LEFT JOIN
table2
ON table2.pid = table1.id
LEFT JOIN
table3
ON table3.pid = table1.id
GROUP BY table1.xid
ORDER BY table1.xid
/
SY.
|
|
|