Created
February 22, 2017 10:30
-
-
Save savelee/85f6bfdaa0e51ea5c34ad2bc7c319fa1 to your computer and use it in GitHub Desktop.
BigQuery UDF Function
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
#standardSQL | |
CREATE TEMPORARY FUNCTION lab_TIME(x ARRAY<TIMESTAMP>) | |
RETURNS INT64 | |
LANGUAGE js AS """ | |
var total_time = 0; | |
//loop through | |
for (var i = 0; i < x.length -1; i+=2) | |
{ total_time += x[i+1] - x[i]; } | |
return total_time/1000; | |
"""; | |
SELECT | |
lab_member AS user, | |
lab_TIME(access_timestamps) AS total_lab_time | |
FROM ( | |
SELECT | |
Name AS lab_member, | |
ARRAY_AGG(LabAccessTS) AS access_timestamps | |
FROM ( | |
SELECT | |
Name, | |
LabAccessTS | |
FROM | |
`secret_lab.door_scans_20140214` | |
GROUP BY | |
Name, | |
LabAccessTS | |
ORDER BY | |
Name, | |
LabAccessTS ASC) | |
GROUP BY | |
lab_member); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment