As you can see my query, this is the working one
SELECT m.MerchandiseId, m.MerchandiseName, createdAt = MAX(i.CreatedAt) FROM Inbox i
INNER JOIN [User] u
ON i.FromUserId = u.UserId
LEFT OUTER JOIN Merchandise m
ON u.MerchandiseId = m.MerchandiseId OR i.ToMerchantId = m.MerchandiseId
WHERE i.ToCompanyId = 10 OR i.FromCompanyId = 10
GROUP BY m.MerchandiseId, m.MerchandiseName
What if i want to SELECT one more field as i.InboxMessage with the Last record of each group?
SELECT m.MerchandiseId, m.MerchandiseName, i.InboxMessage, createdAt = MAX(i.CreatedAt) FROM Inbox i
INNER JOIN [User] u
ON i.FromUserId = u.UserId
LEFT OUTER JOIN Merchandise m
ON u.MerchandiseId = m.MerchandiseId OR i.ToMerchantId = m.MerchandiseId
WHERE i.ToCompanyId = 10 OR i.FromCompanyId = 10
GROUP BY m.MerchandiseId, m.MerchandiseName
I've tried some ways that I found in stackoverflow, but I still not manage to do it. Thousand of thanks if anyone would help on this.
