Telegram Group & Telegram Channel
🧠 Хитрая задача по SQL: максимум без агрегатов?

У тебя есть таблица orders со следующими полями:


orders(id, customer_id, order_date, amount)


📌 Задача:
Для каждого клиента (`customer_id`) найти наиболее поздний заказ (по order_date`), **не используя `GROUP BY и `MAX()`**.

🔥 Уловка: DISTINCT ON, TOP 1 WITH TIES и RANK() нельзя — ты ограничен базовым SQL, работающим на большинстве СУБД.

💡 Подумай:
Как ты решишь эту задачу только с JOIN, WHERE и EXISTS?

📥 Ожидаемый результат:
```sql
customer_id | order_id | order_date | amount
------------|----------|------------|--------
1001 | 87 | 2024-12-01 | 320.00
1002 | 91 | 2024-12-05 | 175.00
...

```

🧩 Подсказка:
Можно использовать NOT EXISTS, чтобы выбрать заказы, у которых нет более новых у того же клиента.


SELECT o.*
FROM orders o
WHERE NOT EXISTS (
SELECT 1
FROM orders o2
WHERE o2.customer_id = o.customer_id
AND o2.order_date > o.order_date
)


📎 Такой приём полезен:
• Когда нельзя использовать оконные функции
• Когда ты работаешь на старых версиях СУБД
• Когда нужна универсальность между MySQL / Oracle / SQLite

#SQL #Задача #БазыДанных #DataEngineering #Оптимизация

@sqlhub



tg-me.com/sqlhub/1900
Create:
Last Update:

🧠 Хитрая задача по SQL: максимум без агрегатов?

У тебя есть таблица orders со следующими полями:


orders(id, customer_id, order_date, amount)


📌 Задача:
Для каждого клиента (`customer_id`) найти наиболее поздний заказ (по order_date`), **не используя `GROUP BY и `MAX()`**.

🔥 Уловка: DISTINCT ON, TOP 1 WITH TIES и RANK() нельзя — ты ограничен базовым SQL, работающим на большинстве СУБД.

💡 Подумай:
Как ты решишь эту задачу только с JOIN, WHERE и EXISTS?

📥 Ожидаемый результат:
```sql
customer_id | order_id | order_date | amount
------------|----------|------------|--------
1001 | 87 | 2024-12-01 | 320.00
1002 | 91 | 2024-12-05 | 175.00
...

```

🧩 Подсказка:
Можно использовать NOT EXISTS, чтобы выбрать заказы, у которых нет более новых у того же клиента.


SELECT o.*
FROM orders o
WHERE NOT EXISTS (
SELECT 1
FROM orders o2
WHERE o2.customer_id = o.customer_id
AND o2.order_date > o.order_date
)


📎 Такой приём полезен:
• Когда нельзя использовать оконные функции
• Когда ты работаешь на старых версиях СУБД
• Когда нужна универсальность между MySQL / Oracle / SQLite

#SQL #Задача #БазыДанных #DataEngineering #Оптимизация

@sqlhub

BY Data Science. SQL hub


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/sqlhub/1900

View MORE
Open in Telegram


Data Science SQL hub Telegram | DID YOU KNOW?

Date: |

Telegram auto-delete message, expiring invites, and more

elegram is updating its messaging app with options for auto-deleting messages, expiring invite links, and new unlimited groups, the company shared in a blog post. Much like Signal, Telegram received a burst of new users in the confusion over WhatsApp’s privacy policy and now the company is adopting features that were already part of its competitors’ apps, features which offer more security and privacy. Auto-deleting messages were already possible in Telegram’s encrypted Secret Chats, but this new update for iOS and Android adds the option to make messages disappear in any kind of chat. Auto-delete can be enabled inside of chats, and set to delete either 24 hours or seven days after messages are sent. Auto-delete won’t remove every message though; if a message was sent before the feature was turned on, it’ll stick around. Telegram’s competitors have had similar features: WhatsApp introduced a feature in 2020 and Signal has had disappearing messages since at least 2016.

What Is Bitcoin?

Bitcoin is a decentralized digital currency that you can buy, sell and exchange directly, without an intermediary like a bank. Bitcoin’s creator, Satoshi Nakamoto, originally described the need for “an electronic payment system based on cryptographic proof instead of trust.” Each and every Bitcoin transaction that’s ever been made exists on a public ledger accessible to everyone, making transactions hard to reverse and difficult to fake. That’s by design: Core to their decentralized nature, Bitcoins aren’t backed by the government or any issuing institution, and there’s nothing to guarantee their value besides the proof baked in the heart of the system. “The reason why it’s worth money is simply because we, as people, decided it has value—same as gold,” says Anton Mozgovoy, co-founder & CEO of digital financial service company Holyheld.

Data Science SQL hub from us


Telegram Data Science. SQL hub
FROM USA