pglass / sqlitis

Convert sql to sqlalchemy expressions
MIT License
157 stars 15 forks source link

Hard query for lib #39

Open freenetwork opened 3 years ago

freenetwork commented 3 years ago

Please add many functions.

https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=04645a18d4d4082cf4cee40545473015


    SELECT * FROM (
    SELECT
        tsrange(timepoint, lead(timepoint) OVER (ORDER BY timepoint)),
        lead(timepoint) OVER (ORDER BY timepoint)
    FROM (
        SELECT 
            unnest(ARRAY[start, "end"]) as timepoint
        FROM
            activities
        ORDER BY timepoint
    ) s
    )s  WHERE lead IS NOT NULL
)
SELECT 
    GREATEST(MAX(start), lower(tsrange)),
    LEAST(MIN("end"), upper(tsrange)),
    array_agg(id),
    EXTRACT(EPOCH FROM (LEAST(MIN("end"), upper(tsrange)) - GREATEST(MAX(start), lower(tsrange))))
FROM 
    timeslots t
JOIN activities a
ON t.tsrange && tsrange(a.start, a.end)
GROUP BY tsrange
HAVING cardinality(array_agg(id)) > 1```
freenetwork commented 3 years ago

sqlitis 'select user_id, event_range, ((lower(event_range) AT TIME ZONE 'UTC-4') ::date ):: timestamptz, ((upper(event_range) AT TIME ZONE 'UTC-4') ::date ):: timestamptz, lower(event_range), upper(event_range), lower(event_range) AT TIME ZONE 'UTC-4', upper(event_range) AT TIME ZONE 'UTC-4', EXTRACT(EPOCH FROM ((lower(event_range) AT TIME ZONE 'UTC-4') - (lower(event_range)::date ):: timestamptz) ), EXTRACT(EPOCH FROM ((upper(event_range) AT TIME ZONE 'UTC-4') - (lower(event_range)::date ):: timestamptz) ) from horizon_logs where user_id = 11261;' ERROR: Failed to convert SQL: 'select user_id, event_range, ((lower(event_range) AT TIME ZONE 'UTC-4') ::date ):: timestamptz, ((upper(event_range) AT TIME ZONE 'UTC-4') ::date ):: timestamptz, lower(event_range), upper(event_range), lower(event_range) AT TIME ZONE 'UTC-4', upper(event_range) AT TIME ZONE 'UTC-4', EXTRACT(EPOCH FROM ((lower(event_range) AT TIME ZONE 'UTC-4') - (lower(event_range)::date ):: timestamptz) ), EXTRACT(EPOCH FROM ((upper(event_range) AT TIME ZONE 'UTC-4') - (lower(event_range)::date ):: timestamptz) ) from horizon_logs where user_id = 11261;'

'Table' object has no attribute 'Table'