I have 3 table named Sale
, SaleStatus
, SaleStatusType
. The table structure is like below
Sale Status:
SaleStatusID
SaleStatusTypeID
CreateDate
SaleID
SaleStatusType:
SaleStatusTypeID
SaleStatusTypeName
Sale:
SaleID
<other sale info>
There can be multiple SaleStatus for one SaleID.
Now if I want to select each saleID
and its latest SaleStatus
information added to my database based on CreateDate
(DATETIME
type), what should I do?
I've written something like the following which results in error
SELECT SST.SaleStatusType, SS.SaleStatusTypeID, SS.SaleID
FROM SaleStatusType AS SST
INNER JOIN SaleStatus AS SS
ON SS.SaleStatusTypeID = SST.SaleStatusTypeID
GROUP BY S.SaleID
HAVING SS.CreateDate = MAX(SS.CreateDate)