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: |

In view of this, the regulator has cautioned investors not to rely on such investment tips / advice received through social media platforms. It has also said investors should exercise utmost caution while taking investment decisions while dealing in the securities market. Telegram, which does little policing of its content, has also became a hub for Russian propaganda and misinformation. Many pro-Kremlin channels have become popular, alongside accounts of journalists and other independent observers. These entities are reportedly operating nine Telegram channels with more than five million subscribers to whom they were making recommendations on selected listed scrips. Such recommendations induced the investors to deal in the said scrips, thereby creating artificial volume and price rise. So, uh, whenever I hear about Telegram, it’s always in relation to something bad. What gives? The perpetrators use various names to carry out the investment scams. They may also impersonate or clone licensed capital market intermediaries by using the names, logos, credentials, websites and other details of the legitimate entities to promote the illegal schemes.
from vn


Telegram Data Science. SQL hub
FROM American