The following query when I that execute
SELECT St.FirstName,St.LastName,
CASE
WHEN St.ISPackage='N' THEN Min(St.VisitingDate)
WHEN St.ISPackage='Y' THEN St.VisitingDate
END AS VisitingDate
FROM SalesTransaction As St
(...inner join and where clause)
GROUP BY St.FirstName,St.LastName, St.VisitingDate
If I use St.VisitingDate in Group By
the result is duplicated.
If not used St.VisitingDate in Group By
show error
invalid in the select list because it is not contained in either an aggregate function
How can I solve this problem?
I suspect that you want to group by name and get the visit date when
ISPackage
is "Y", and fallback on the earliest visit date if there is no date whereISPackage
is "Y".If so, you can do: