orlio
07-09-2009, 05:40 AM
Hey, I need some help with the following SQL-query:
SELECT DISTINCT
SharingRule.Created,
Organization.Id
FROM
SharingRule
LEFT JOIN
Organization ON Organization.OrganizationId = SharingRule.OrganizationId
WHERE
SharingRule.ChannelId = 1
GROUP BY
SharingRule.OrganizationId
ORDER BY
SharingRule.Created DESC
This returns SharingRule.Created and Organization.Id which is grouped by Organization.Id. So far so good. BUT there can be many SharingRule:s for an Organization and I just want to pick the row with the latest one (the one with highest value in SharingRule.Created). In other words, I want each group to contain the SharingRule with the highest SharingRule.Created. How do I put such a condition on the GROUP BY?
SELECT DISTINCT
SharingRule.Created,
Organization.Id
FROM
SharingRule
LEFT JOIN
Organization ON Organization.OrganizationId = SharingRule.OrganizationId
WHERE
SharingRule.ChannelId = 1
GROUP BY
SharingRule.OrganizationId
ORDER BY
SharingRule.Created DESC
This returns SharingRule.Created and Organization.Id which is grouped by Organization.Id. So far so good. BUT there can be many SharingRule:s for an Organization and I just want to pick the row with the latest one (the one with highest value in SharingRule.Created). In other words, I want each group to contain the SharingRule with the highest SharingRule.Created. How do I put such a condition on the GROUP BY?