Last active
December 14, 2015 04:28
-
-
Save jrmadsen67/5028002 to your computer and use it in GitHub Desktop.
Takes data from a key:value table & combines it onto a single-row record, per item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Data starts in table: | |
ProjectId, key, value | |
1 totaltime 02:34:56 | |
1 zip.url http://www.mysite.com/download/file.zip | |
1 zip.size 12MB | |
etc | |
and a main table, Projects, with PK id | |
The 3 rows will combine onto a single row for each ProjectId, so will result in: | |
ProjectId totaltime zip_url zip_size | |
1 02:34:56 http://www.mysite.com/download/file.zip 12MB | |
*/ | |
SELECT ProjectID, MAX(summed_data.totaltime) AS totaltime, MAX(summed_data.zip_url) AS zip_url, MAX(summed_data.zip_size) AS zip_size | |
FROM projects p, | |
(SELECT | |
ProjectID, | |
(CASE WHEN `key` = 'totaltime' THEN `value` END) AS totaltime, | |
(CASE WHEN `key` = 'zip.url' THEN `value` END) AS zip_url, | |
(CASE WHEN `key` = 'zip.size' THEN `value` END) AS zip_size | |
FROM project_files) AS summed_data | |
WHERE p.id = summed_data.ProjectID | |
GROUP BY ProjectID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment