Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
FinaleNFJNDFJKSN
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
--PostgreSQL 9.6 --'\\' is a delimiter CREATE SCHEMA erp; CREATE TABLE erp.orders( "userId" uuid, "paidAt" timestamp DEFAULT current_timestamp ); CREATE SCHEMA prod; CREATE TABLE prod.referral_order_delivered( user_id text ); CREATE TABLE erp.users( id uuid, firstname varchar(40), lastname varchar(40), phone varchar(40) , "companyId" integer ) ; CREATE TABLE erp.companies( id integer, name varchar(40), zoneId integer ) ; CREATE TABLE prod.referral_code_used( godfather_id text, id integer, timestamp timestamp ); CREATE TABLE prod.referral_invite_sent( user_id text, id integer, timestamp timestamp ); INSERT INTO prod.referral_code_used (godfather_id, id, timestamp) VALUES ('dfcfa623-0f68-492a-9a05-24c41de81674', '323232', '2017-03-04') ; INSERT INTO prod.referral_invite_sent VALUES ('dfcfa623-0f68-492a-9a05-24c41de81674', '323232', '2017-03-04') ; INSERT INTO erp.orders VALUES ('b0b87982-b551-4be7-98ee-8e7bb7ea0788','2017-01-01' ), ('5ae688e3-662f-4768-bd42-f4ff4179474d','2017-01-01' ), ('dfcfa623-0f68-492a-9a05-24c41de81674','2017-06-30' ), ('dfcfa623-0f68-492a-9a05-24c41de81674','2017-04-01' ), ('5ae688e3-662f-4768-bd42-f4ff4179474d','2017-06-07' ), ('8a5f052b-2125-46bc-ae00-f40610221283','2017-06-07' ), ('5ae688e3-662f-4768-bd42-f4ff4179474d','2017-01-01' ), ('5ae688e3-662f-4768-bd42-f4ff4179474d','2017-01-01' ) ; INSERT INTO prod.referral_order_delivered VALUES ('5ae688e3-662f-4768-bd42-f4ff4179474d'), ('dfcfa623-0f68-492a-9a05-24c41de81674' ), ('dfcfa623-0f68-492a-9a05-24c41de81674' ), ('dfcfa623-0f68-492a-9a05-24c41de81674' ), ('dfcfa623-0f68-492a-9a05-24c41de81674' ) ; INSERT INTO erp.users VALUES ('5ae688e3-662f-4768-bd42-f4ff4179474d','Anne', 'Beaujeu', '0654738293', DEFAULT ), ('dfcfa623-0f68-492a-9a05-24c41de81674','Jean', 'Dupont','0623562783', '124'), ('8a5f052b-2125-46bc-ae00-f40610221283','Alice', 'Terramine', '0673839203', DEFAULT ), ('b0b87982-b551-4be7-98ee-8e7bb7ea0788','Hugo', 'Blanchet', '06784859403', '218' ) ; INSERT INTO erp.companies VALUES ('124','Alchatech', '2'), ('218','SigmaX2', '4') ; select u.id, paid_at, rcu.timestamp code_used_at from erp.users u inner join ( select o."userId", min(o."paidAt") paid_at from erp.orders o group by o."userId" ) o on o."userId" = u.id inner join prod.referral_code_used rcu on rcu.godfather_id::uuid = o."userId" ; select u.id, paid_at, ris.timestamp invite_sent_at from erp.users u inner join ( select o."userId", min(o."paidAt") paid_at from erp.orders o group by o."userId" ) o on o."userId" = u.id inner join prod.referral_invite_sent ris on ris.user_id::uuid = o."userId" ; select i.order_month, REPLACE(avg(i.codeused)::text, '.', ',') as average from ( select u.id, lpad(extract(month from o.paid_at)::text, 2, '0') order_month, sum(case when rcu.id is not null then 1 else 0 end) codeused from erp.users u inner join ( select o."userId", min(o."paidAt") paid_at from erp.orders o where o."paidAt"::date >= '2017-01-01' group by o."userId" ) o on o."userId" = u.id inner join prod.referral_code_used rcu on rcu.godfather_id::uuid = u.id where rcu.timestamp <= o.paid_at::timestamp + interval '1 month' group by u.id, lpad(extract(month from o.paid_at)::text, 2, '0') )i group by i.order_month order by i.order_month asc
absolute service time: 0,47 sec
edit mode
|
history
|
discussion
id
paid_at
code_used_at
1
dfcfa623-0f68-492a-9a05-24c41de81674
01.04.2017 00:00:00
04.03.2017 00:00:00
id
paid_at
invite_sent_at
1
dfcfa623-0f68-492a-9a05-24c41de81674
01.04.2017 00:00:00
04.03.2017 00:00:00
order_month
average
1
04
1,00000000000000000000