Because in your example you've chosen EVENT
as the value to show in the PIVOT intersections (i.e. since you've specified EVENT
in the PIVOT
clause), the value must be specified with one of the permissible aggregate functions, as there are potentially multiple rows for each of the column values that you've chosen in your pivot, when grouped by the remaining columns (i.e. DATE in your case).
In Sql Server[1], MAX()
or MIN()
is commonly used when pivoting non-numeric columns, as it is able to show one of the original of the values of the column.
Any non-aggregate and non-pivoted columns will be left as-is and will be used to form the groups on which the pivot is based (in your case, column DATE
isn't either in the aggregate, or the column pivot, so it will form the row group)
Consider the case where your pivoted table contains multiple rows matching your predicate, such as this:
INSERT INTO strategy (DATE, SITA, EVENT) VALUES
('1 Jan 2018', 'ABZPD', 'Event1'),
('1 Jan 2018', 'BFSPD', 'Event2'),
('1 Jan 2018', 'BFSPD', 'Event3');
After Pivot:
DATE ABZPD BFSPD
2018-01-01T00:00:00Z Event1 Event3
i.e. During the Pivot, the BFSPD
rows for Event2
and Event3
needed to somehow be projected into a single cell - hence the need for an aggregate. This aggregate is still needed, even if there is known to be just one value (this being the case for the Event1
value for SITA ABZPD
in the above example).
Since BFSPD
has two events, you'll need to somehow resolve how to project a value into a single cell value. The use of MAX
on the VARCHAR column resolves the 'largest' value (Event3
) in the event of multiple rows in projecting into the same resulting pivot 'cell' - SqlFiddle example here
You could choose to use COUNT(Event)
to show you the number of events per row / pivot intersection - Fiddle
And you could switch the aggregate on EVENT
with DATE
- EVENT
is thus used in the column grouping.
*1 Aggregates like AVG
or STDEV
are obviously not available to strings. Other RDBMS have additional aggregates like FIRST which will arbitrarily take the first value, or GROUP_CONCAT / LIST_AGG, which can fold string values together with a delimiter. And PostGres allows you to make your own aggregate functions!. But sadly, none of this in SqlServer, hence MIN()
/ MAX() for now.