Во втором случае ниже Latest Sale
будет NULL
, если они еще не сделали продажу.
-- if you want all salesmen who have made a sale
SELECT
SLSNO,
SLSNAME,
MAX(SALEDATE) AS [Latest Sale]
FROM
SALESMEN AS s
JOIN
SALESHEADER AS h
ON
h.SLSNO = s.SLSNO
WHERE
SALESMANNO = SLSNO
GROUP BY
SLSNO,
SLSNAME
-- if you want all salesmen even if they don't have a sale
SELECT
SLSNO,
SLSNAME,
MAX(SALEDATE) AS [Latest Sale]
FROM
SALESMEN AS s
LEFT OUTER JOIN
SALESHEADER AS h
ON
h.SLSNO = s.SLSNO
WHERE
SALESMANNO = SLSNO
GROUP BY
SLSNO,
SLSNAME