Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 공공데이터
- union
- limit
- 티스토리챌린지
- R/3
- SAP HANA
- S/4 HANA
- group by
- SQL
- %_%
- order by
- SERVICE_KEY_IS_NOT_REGISTERED_ERROR
- join
- Where
- left join
- 중복 컬럼
- spa
- ERP
- desc
- json
- between
- HAVING
- CSV
- stratascratch
- 오블완
- over()
- dense_rank()
- r/2
- asc
- DateDiff
Archives
- Today
- Total
RE:cord
[SQL] Count the number of group (GROUP BY / JOIN / LEFT JOIN / SUM) 본문
<1>
-- subQ: Only MacBookPro user's data
-- mainQ: event name, event freq -> DESC order
SELECT event_name, COUNT(event_name) AS event_freq
FROM (SELECT *
FROM playbook_events
WHERE device = 'macbook pro') AS MBP_user
GROUP BY event_name
ORDER BY event_freq DESC;
<2>
--1.left join with 'host_id'
--2.make group by 'nationality'
--3.count n_beds of each groups
--4. sort avail_beds DESC
SELECT nationality, SUM(n_beds) AS avail_beds
FROM (select AA.host_id, nationality, n_beds
from airbnb_apartments as AA
left join airbnb_hosts as AH
on AA.host_id = AH.host_id) total_airbnb
GROUP BY nationality
ORDER BY avail_beds DESC;
* When using JOIN, you have to specify the column names in SELECT query.
Otherwise, a conflict can occur in the query.
(And write in SELECT AA.host_id, AH.nationality, AA.n_beds is recommended for defining clearly. (but I didn't))
'SQL' 카테고리의 다른 글
[SQL] Search for Data Containing Keywords (WHERE LIKE '%A%') (0) | 2024.12.03 |
---|---|
[SQL] Finding the Nth Highest Values (JOIN/ORDER BY /LIMIT/DENSE_RANK () OVER) (0) | 2024.11.30 |
[SQL] Find the Nth group (DENSE_RANK / OVER / SUBQUERY) (0) | 2024.11.24 |
[SQL] AVG / GROUP BY / ORDER BY DESC (2) | 2024.11.22 |
[SQL] UNION / COUNT / BETWEEN (0) | 2024.11.22 |