The table I have right now is this:
Is there a way in MySQL to manipulate and spread the table to:
This is called records to columns conversion
or pivot
.
The general query structure is, done with a GROUP BY
, CASE END
and some aggregate function like MAX
, MIN
or SUM
. In MySQL/MariaDB or anny database system which does not have a PIVOT()
kind of function
Where MAX()
and MIN()
can pivot every datatype, SUM()
is only suitable for numeric datatypes.
SELECT
Name
, SUM(CASE WHEN Cycle = 'weekly' THEN 1 ELSE 0 END) AS weekly
..
..
FROM
t
GROUP BY
Name
Note because SQL tables when stored and selections off SQL tables are by SQL definition orderless so the result off this query is nondeterministic (random), there wasn't anny column in your data example which could be used to make a deterministic (fixed) order by