Telegram Group & Telegram Channel
💡 SQL: быстрое нахождение первых или последних записей с DISTINCT ON !!!

В PostgreSQL есть полезный приём — DISTINCT ON, который позволяет взять первую строку в каждой группе по определённому полю.


SELECT DISTINCT ON (customer_id)
customer_id,
order_date,
amount
FROM orders
ORDER BY customer_id, order_date DESC;


🔎 Этот запрос вернёт последний заказ каждого клиента без лишних подзапросов или JOIN.

Работает очень быстро и удобно, если нужно найти «самый первый» или «самый последний» элемент в группе.

@sqlhub
🔥278👍7🥰1



group-telegram.com/sqlhub/2075
Create:
Last Update:

💡 SQL: быстрое нахождение первых или последних записей с DISTINCT ON !!!

В PostgreSQL есть полезный приём — DISTINCT ON, который позволяет взять первую строку в каждой группе по определённому полю.


SELECT DISTINCT ON (customer_id)
customer_id,
order_date,
amount
FROM orders
ORDER BY customer_id, order_date DESC;


🔎 Этот запрос вернёт последний заказ каждого клиента без лишних подзапросов или JOIN.

Работает очень быстро и удобно, если нужно найти «самый первый» или «самый последний» элемент в группе.

@sqlhub

BY Data Science. SQL hub


Warning: Undefined variable $i in /var/www/group-telegram/post.php on line 260

Share with your friend now:
group-telegram.com/sqlhub/2075

View MORE
Open in Telegram


Telegram | DID YOU KNOW?

Date: |

"This time we received the coordinates of enemy vehicles marked 'V' in Kyiv region," it added. "There are a lot of things that Telegram could have been doing this whole time. And they know exactly what they are and they've chosen not to do them. That's why I don't trust them," she said. And while money initially moved into stocks in the morning, capital moved out of safe-haven assets. The price of the 10-year Treasury note fell Friday, sending its yield up to 2% from a March closing low of 1.73%. The Dow Jones Industrial Average fell 230 points, or 0.7%. Meanwhile, the S&P 500 and the Nasdaq Composite dropped 1.3% and 2.2%, respectively. All three indexes began the day with gains before selling off. Messages are not fully encrypted by default. That means the company could, in theory, access the content of the messages, or be forced to hand over the data at the request of a government.
from us


Telegram Data Science. SQL hub
FROM American