を見つけるために、私はそうのようなクエリがあると表示します。Postgresは、ネストされたSQLクエリまたはどのように最後のINET
のように見えますSELECT DISTINCT(orders.email), uuid_nil() AS customer_id, 'Order' AS customer_type, orders.first_name, orders.last_name, MAX(orders.paid_at) AS last_order_at, 1 AS order_count, SUM(orders.total_price_cents) AS total_spent_pennies
FROM orders
WHERE orders.state = 'paid' AND orders.customer_id IS null
GROUP BY orders.email, customer_id, orders.first_name, orders.last_name
UNION
SELECT DISTINCT(customers.email), customers.id AS customer_id, 'Customer' AS customer_type, customers.first_name, customers.last_name, MAX(orders.paid_at) AS last_order_at, COUNT(orders.*) AS order_count, SUM(orders.total_price_cents) AS total_spent_pennies
FROM customers
JOIN orders ON customers.id = orders.customer_id
GROUP BY customers.email, customers.id, customers.first_name, customers.last_name
:私の質問は、私は彼らの最後を含めることができる方法である
+-------------------------------+--------------------------------------+---------------+------------+--------------+-------------------------+-------------+---------------------+
| email | customer_id | customer_type | first_name | last_name | last_order_at | order_count | total_spent_pennies |
+-------------------------------+--------------------------------------+---------------+------------+--------------+-------------------------+-------------+---------------------+
| [email protected] | 00000000-0000-0000-0000-000000000000 | Order | Richard | Doe | 2015-12-18 14:45:22 UTC | 1 | 2000 |
| [email protected] | 00000000-0000-0000-0000-000000000000 | Order | Paul | Doe | 2016-04-05 09:04:57 UTC | 1 | 5000 |
+-------------------------------+--------------------------------------+---------------+------------+--------------+-------------------------+-------------+---------------------+
IPアドレス(INET列)。ある日、私は単純にMAX集約関数を使うことができますが、IPアドレスは明らかに1つ持っていません。
は基本的に私はこのクエリを組み合わせることができますどのように上記のようなものを私に彼らのlast_ipアドレスを持つ新しい列を与えるために:
SELECT browser_ip FROM orders
WHERE email = '[email protected]'
ORDER BY paid_at DESC
LIMIT 1