ここ2日間くらいで、新しく知ったSQL文

SELECT * FROM records WHERE start_at = '2023-03-20 05:03:00'; start_atの時間順に表示するSQLを教えて下さい。
SELECT * FROM records ORDER BY start_at ASC;

降順(新しいものから古いものへ)に並べ替えたい場合は、ASCDESC に変更します:
SELECT * FROM records ORDER BY start_at DESC;

select user_id, start_at, end_at from records order by start_at ASC where user_id = 7; のSQLは何が間違っていますか?
SELECT user_id, start_at, end_at FROM records WHERE user_id = 7 ORDER BY start_at ASC;

moove=# select place from records where id = 2; place ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [{"lat": 34.7325, "lng": 135.4986}, {"lat": 34.7326, "lng": 135.4989}, {"lat": 34.7326, "lng": 135.4989}, {"lat": 34.7327, "lng": 135.4995}, {"lat": 34.7327, "lng": 135.5}, {"lat": 34.7328, "lng": 135.5002}, {"lat": 34.733, "lng": 135.5006}, {"lat": 34.7331, "lng": 135.501}, {"lat": 34.7334, "lng": 135.5015}, {"lat": 34.7336, "lng": 135.5018}, {"lat": 34.7336, "lng": 135.5018}, {"lat": 34.7336, "lng": 135.5019}, {"lat": 34.7337, "lng": 135.5021}, {"lat": 34.734, "lng": 135.5024}, {"lat": 34.7343, "lng": 135.5026}, {"lat": 34.7346, "lng": 135.5024}, {"lat": 34.7347, "lng": 135.5024}, {"lat": 34.7346, "lng": 135.5024}, {"lat": 34.7345, "lng": 135.5025}, {"lat": 34.7343, "lng": 135.5027}, {"lat": 34.7343, "lng": 135.5029}, {"lat": 34.7342, "lng": 135.5031}, {"lat": 34.7344, "lng": 135.5035}, {"lat": 34.7344, "lng": 135.5035}, {"lat": 34.7344, "lng": 135.5035}] (1 row) で登場する、最初の{"lat": 34.7325, "lng": 135.4986}と、最後の {"lat": 34.7344, "lng": 135.5035}だけを取り出すSQL文を書いて下さい
WITH places AS (
SELECT jsonb_array_elements(place) AS place_elements
FROM records
WHERE id = 2
)
SELECT
(SELECT place_elements FROM places LIMIT 1) AS first_place,
(SELECT place_elements FROM places ORDER BY jsonb_array_length(place) DESC LIMIT 1) AS last_place
FROM records
WHERE id = 2;

select user_id from records; で出てくる異なるuser_id(整数)をピックアップするSQL文を教えて下さい
SELECT DISTINCT user_id FROM records;

ありがとうございます。では、これを降順に並べる方法も教えてください
SELECT DISTINCT user_id FROM records ORDER BY user_id DESC;
昇順ならどうなりますか
SELECT DISTINCT user_id FROM records ORDER BY user_id ASC;

 

2023,江端さんの技術メモ

Posted by ebata