migrations.pgsql 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. CREATE TABLE producten (guid TEXT, naam TEXT, energie_kcal NUMERIC,
  2. vet_g NUMERIC,
  3. verzadigd_vet_g NUMERIC,
  4. koolhydraten_g NUMERIC,
  5. eiwit_g NUMERIC,
  6. vezels_g NUMERIC,
  7. zout_g NUMERIC,
  8. alcohol_g NUMERIC,
  9. water_g NUMERIC,
  10. natrium_mg NUMERIC,
  11. kalium_mg NUMERIC,
  12. calcium_mg NUMERIC,
  13. magnesium_mg NUMERIC,
  14. ijzer_mg NUMERIC,
  15. selenium_µg NUMERIC,
  16. zink_mg NUMERIC,
  17. vitamine_a_µg NUMERIC,
  18. vitamine_d_µg NUMERIC,
  19. vitamine_e_mg NUMERIC,
  20. vitamine_b1_g NUMERIC,
  21. vitamine_b2_mg NUMERIC,
  22. vitamine_b6_mg NUMERIC,
  23. foliumzuur_µg NUMERIC,
  24. vitamine_b12_µg NUMERIC,
  25. nicotinezuur_mg NUMERIC,
  26. vitamine_c_mg NUMERIC,
  27. jodium_µg NUMERIC,
  28. fosfor_mg NUMERIC,
  29. suikers_g NUMERIC,
  30. eiwit_plantaardig_g NUMERIC);
  31. CREATE TABLE consumpties_USERNAME (id SERIAL, datum DATE, product_guid TEXT, periode TEXT, hoeveelheid_g NUMERIC);
  32. CREATE VIEW voedingswaarden_per_dag_USERNAME AS
  33. (WITH sub AS (SELECT datum,
  34. hoeveelheid_g / 100 * energie_kcal AS energie_kcal,
  35. hoeveelheid_g / 100 * vet_g AS vet_g,
  36. hoeveelheid_g / 100 * verzadigd_vet_g AS verzadigd_vet_g,
  37. hoeveelheid_g / 100 * koolhydraten_g AS koolhydraten_g,
  38. hoeveelheid_g / 100 * eiwit_g AS eiwit_g,
  39. hoeveelheid_g / 100 * vezels_g AS vezels_g,
  40. hoeveelheid_g / 100 * zout_g AS zout_g,
  41. hoeveelheid_g / 100 * alcohol_g AS alcohol_g,
  42. hoeveelheid_g / 100 * water_g AS water_g,
  43. hoeveelheid_g / 100 * natrium_mg AS natrium_mg,
  44. hoeveelheid_g / 100 * kalium_mg AS kalium_mg,
  45. hoeveelheid_g / 100 * calcium_mg AS calcium_mg,
  46. hoeveelheid_g / 100 * magnesium_mg AS magnesium_mg,
  47. hoeveelheid_g / 100 * ijzer_mg AS ijzer_mg,
  48. hoeveelheid_g / 100 * selenium_µg AS selenium_µg,
  49. hoeveelheid_g / 100 * zink_mg AS zink_mg,
  50. hoeveelheid_g / 100 * vitamine_a_µg AS vitamine_a_µg,
  51. hoeveelheid_g / 100 * vitamine_d_µg AS vitamine_d_µg,
  52. hoeveelheid_g / 100 * vitamine_e_mg AS vitamine_e_mg,
  53. hoeveelheid_g / 100 * vitamine_b1_g AS vitamine_b1_g,
  54. hoeveelheid_g / 100 * vitamine_b2_mg AS vitamine_b2_mg,
  55. hoeveelheid_g / 100 * vitamine_b6_mg AS vitamine_b6_mg,
  56. hoeveelheid_g / 100 * foliumzuur_µg AS foliumzuur_µg,
  57. hoeveelheid_g / 100 * vitamine_b12_µg AS vitamine_b12_µg,
  58. hoeveelheid_g / 100 * nicotinezuur_mg AS nicotinezuur_mg,
  59. hoeveelheid_g / 100 * vitamine_c_mg AS vitamine_c_mg,
  60. hoeveelheid_g / 100 * jodium_µg AS jodium_µg,
  61. hoeveelheid_g / 100 * fosfor_mg AS fosfor_mg,
  62. hoeveelheid_g / 100 * suikers_g AS suikers_g,
  63. hoeveelheid_g / 100 * eiwit_plantaardig_g AS eiwit_plantaardig_g
  64. FROM consumpties_USERNAME
  65. JOIN producten
  66. ON consumpties_USERNAME.product_guid = producten.guid)
  67. SELECT datum,
  68. SUM(energie_kcal) AS energie_kcal,
  69. SUM(vet_g) AS vet_g,
  70. SUM(verzadigd_vet_g) AS verzadigd_vet_g,
  71. SUM(koolhydraten_g) AS koolhydraten_g,
  72. SUM(eiwit_g) AS eiwit_g,
  73. SUM(vezels_g) AS vezels_g,
  74. SUM(zout_g) AS zout_g,
  75. SUM(alcohol_g) AS alcohol_g,
  76. SUM(water_g) AS water_g,
  77. SUM(natrium_mg) AS natrium_mg,
  78. SUM(kalium_mg) AS kalium_mg,
  79. SUM(calcium_mg) AS calcium_mg,
  80. SUM(magnesium_mg) AS magnesium_mg,
  81. SUM(ijzer_mg) AS ijzer_mg,
  82. SUM(selenium_µg) AS selenium_µg,
  83. SUM(zink_mg) AS zink_mg,
  84. SUM(vitamine_a_µg) AS vitamine_a_µg,
  85. SUM(vitamine_d_µg) AS vitamine_d_µg,
  86. SUM(vitamine_e_mg) AS vitamine_e_mg,
  87. SUM(vitamine_b1_g) AS vitamine_b1_g,
  88. SUM(vitamine_b2_mg) AS vitamine_b2_mg,
  89. SUM(vitamine_b6_mg) AS vitamine_b6_mg,
  90. SUM(foliumzuur_µg) AS foliumzuur_µg,
  91. SUM(vitamine_b12_µg) AS vitamine_b12_µg,
  92. SUM(nicotinezuur_mg) AS nicotinezuur_mg,
  93. SUM(vitamine_c_mg) AS vitamine_c_mg,
  94. SUM(jodium_µg) AS jodium_µg,
  95. SUM(fosfor_mg) AS fosfor_mg,
  96. SUM(suikers_g) AS suikers_g,
  97. SUM(eiwit_plantaardig_g) AS eiwit_plantaardig_g
  98. FROM sub
  99. GROUP BY datum
  100. ORDER BY datum ASC);