I have a simple ‘Action’ table I use to record user actions that looks like this:
{
label (varchar),
timestamp
}
A record is saved for every action a user completes.
I now need to output a report that looks something like this:
Date, Label1Count, Label2Count, Label3Count, ...
dd/mm/yy, 34, 44, 55, ...
dd/mm/yy, 34, 44, 55, ...
dd/mm/yy, 34, 44, 55, ...
Where Date is the day the action took place, and each label header is one of the labels that I record.
I can output something similar no probs:
label, date, count
label, date, count
...
And order it so that labels are grouped together and then dates.
I would then need to copy and paste it in Excel to match the desired view.
Any ideas how I would get the first view using pure SQL? I could do it with a combo of python and SQL, but I’m sure SQL alone could do it?
Thanks
You can use
crosstabin PostGreSQL, but I don’t like it.So I use this pattern instead…
Obviously, this only works when you know all the labels that you are pivotting.
If you don’t knw all the labels, and you want a result set with a dynamic number of columns, you need dynamic SQL (code that writes SQL, specifically the new lines of code for the different labels). There isn’t a single static query that can do that for you.
Because you need dynamic SQL when you don’t know the different label values at design time, the normalised data-set is often actually the best methodology in your data layer. Your client code could then transform that normalised data set into flat file dataset in a model layer. Or similar.
For this reason, I often recommend not pivotting the data in a SQL environment.