From: gamesguru Date: Tue, 29 Sep 2020 20:14:24 +0000 (-0400) Subject: better function X-Git-Tag: 0.0.0~15 X-Git-Url: https://git.nutra.tk/v1?a=commitdiff_plain;h=f426ff9fe3d7f08465b6b1483c966194dc6c7b0f;p=nutratech%2Fnt-sqlite.git better function --- diff --git a/sql/data/bio_log_entry.csv b/sql/data/bio_log_entry.csv index 5bd12c6..4961acf 100644 --- a/sql/data/bio_log_entry.csv +++ b/sql/data/bio_log_entry.csv @@ -26,7 +26,10 @@ log_id,biometric_id,value 9,22,62 9,23,108 9,24,65 +10,1,177 10,2,67 +10,3,17.2 +10,4,21.6 10,5, 10,6, 10,7, @@ -123,3 +126,6 @@ log_id,biometric_id,value 37,23,103 37,24,63 38,2,65.2 +39,3,17.5 +39,4,21.4 +40,1,178 diff --git a/sql/data/biometric_log.csv b/sql/data/biometric_log.csv index fae6cfe..23c94ec 100644 --- a/sql/data/biometric_log.csv +++ b/sql/data/biometric_log.csv @@ -33,7 +33,9 @@ id,guid,user_id,date,tags,notes 32,f4cf78ea8e2c7a03892e4b0268481b59,2,Thu 24 Sep 2020 05:05:51 PM EDT,illness, 33,63239cac4d86f57a69cf9a9c7799a13c,1,Thu 24 Sep 2020 05:05:51 PM EDT,, 34,0ebe7d5f9214f2e62c2952696caaadd9,2,Thu 24 Sep 2020,, -35,7a4692cf15f960c1384210fb477367a9,1,Fri 25 Sep 2020,,gym scale +35,7a4692cf15f960c1384210fb477367a9,1,Fri 25 Sep 2020,gym scale, 36,67e1a2d244afb9924d1999d792264109,2,Sat 26 Sep 2020,, 37,6f61f3010e4cae1e4d46cc85302ab4d6,2,Sat 26 Sep 2020 11:54:17 AM EDT,, -38,180eed8bc3094f634895c13331cef858,1,Tue 29 Sep 2020 03:40:37 PM EDT,,no clothes +38,180eed8bc3094f634895c13331cef858,1,Tue 29 Sep 2020 03:40:37 PM EDT,nude, +39,356726361e8910c5228cb659dc8fec02,1,Tue 29 Sep 2020 03:40:37 PM EDT,test, +40,2fba6de7d11ca3337efff83b376380aa,1,Tue 29 Sep 2020 03:40:37 PM EDT,test, diff --git a/sql/format.sh b/sql/format.sh new file mode 100755 index 0000000..0c90d08 --- /dev/null +++ b/sql/format.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +cd "$(dirname "$0")" + +pg_format -s 2 tables.sql -o tables.sql +pg_format -s 2 functions.sql -o functions.sql diff --git a/sql/functions.sql b/sql/functions.sql index e040587..a503c28 100644 --- a/sql/functions.sql +++ b/sql/functions.sql @@ -5,14 +5,7 @@ SELECT users.name, date, - ( - SELECT - ROUND(value * 2.204, 1) - FROM - bio_log_entry - WHERE - biometric_id = 2 - AND log_id = biometric_log.id) AS weight, + ROUND(bio_log_entry.value * 2.204, 1) AS weight, tags, notes FROM @@ -32,39 +25,48 @@ SELECT DISTINCT users.name, tags, notes, - ( - SELECT - CAST(value AS int) - FROM - bio_log_entry - WHERE - biometric_id = 23 - AND log_id = biometric_log.id) || '/' || ( - SELECT - CAST(value AS int) - FROM - bio_log_entry - WHERE - biometric_id = 24 - AND log_id = biometric_log.id) AS pressure, - ( - SELECT - CAST(value AS int) - FROM - bio_log_entry - WHERE - biometric_id = 22 - AND log_id = biometric_log.id) AS pulse + CAST(sys.value AS int) || '/' || CAST(dia.value AS int) AS pressure, + CAST(pulse.value AS int) AS pulse FROM biometric_log INNER JOIN users ON user_id = users.id - INNER JOIN bio_log_entry ON biometric_id IN (22, - 23, - 24) - AND log_id = biometric_log.id + INNER JOIN bio_log_entry pulse ON pulse.biometric_id = 22 + AND pulse.log_id = biometric_log.id + INNER JOIN bio_log_entry sys ON sys.biometric_id = 23 + AND sys.log_id = biometric_log.id + INNER JOIN bio_log_entry dia ON dia.biometric_id = 24 + AND dia.log_id = biometric_log.id WHERE users.name = 'Mark'; +-------------------------------- +-- Meas, skinfolds, statics +-------------------------------- + +SELECT + date, + users.name, + height.value AS height, + wrist.value AS wrist, + ankle.value AS ankle +FROM + biometric_log + INNER JOIN users ON user_id = users.id + LEFT JOIN bio_log_entry height ON height.biometric_id = 1 + AND height.log_id = biometric_log.id + LEFT JOIN bio_log_entry wrist ON wrist.biometric_id = 3 + AND wrist.log_id = biometric_log.id + LEFT JOIN bio_log_entry ankle ON ankle.biometric_id = 4 + AND ankle.log_id = biometric_log.id +WHERE + height.value + OR wrist.value + OR ankle.value; + +-------------------------------- +-- OLD: pulse/bp +-------------------------------- + SELECT date, users.name,