SQLHUB Telegram 2040
💡 SQL: условные агрегаты через CASE !!!

Хотите посчитать сумму только по условию прямо внутри агрегата?
Для этого не нужен отдельный WHERE — используйте CASE WHEN.


SELECT
customer_id,
SUM(CASE WHEN status = 'completed' THEN amount ELSE 0 END) AS completed_sum,
SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) AS pending_sum
FROM orders
GROUP BY customer_id;


🔎 В одном запросе можно посчитать суммы по разным статусам — и не делать несколько JOIN или подзапросов.
Работает также с COUNT(), AVG() и другими агрегатами.

@sqlhub
👍237🥰4



tgoop.com/sqlhub/2040
Create:
Last Update:

💡 SQL: условные агрегаты через CASE !!!

Хотите посчитать сумму только по условию прямо внутри агрегата?
Для этого не нужен отдельный WHERE — используйте CASE WHEN.


SELECT
customer_id,
SUM(CASE WHEN status = 'completed' THEN amount ELSE 0 END) AS completed_sum,
SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) AS pending_sum
FROM orders
GROUP BY customer_id;


🔎 В одном запросе можно посчитать суммы по разным статусам — и не делать несколько JOIN или подзапросов.
Работает также с COUNT(), AVG() и другими агрегатами.

@sqlhub

BY Data Science. SQL hub


Share with your friend now:
tgoop.com/sqlhub/2040

View MORE
Open in Telegram


Telegram News

Date: |

2How to set up a Telegram channel? (A step-by-step tutorial) On Tuesday, some local media outlets included Sing Tao Daily cited sources as saying the Hong Kong government was considering restricting access to Telegram. Privacy Commissioner for Personal Data Ada Chung told to the Legislative Council on Monday that government officials, police and lawmakers remain the targets of “doxxing” despite a privacy law amendment last year that criminalised the malicious disclosure of personal information. On June 7, Perekopsky met with Brazilian President Jair Bolsonaro, an avid user of the platform. According to the firm's VP, the main subject of the meeting was "freedom of expression." Ng was convicted in April for conspiracy to incite a riot, public nuisance, arson, criminal damage, manufacturing of explosives, administering poison and wounding with intent to do grievous bodily harm between October 2019 and June 2020. With Bitcoin down 30% in the past week, some crypto traders have taken to Telegram to “voice” their feelings.
from us


Telegram Data Science. SQL hub
FROM American