코딩테스트 SQL 코드카타 (Customer Who Visited without Any Transactions)

2024. 11. 17. 22:04·Daily 코드카타/SQL

문제

Customer Who Visited but Did Not Make Any Transactions - LeetCode

 

 

Write a solution to find the IDs of the users who visited without making any transactions
and the number of times they made these types of visits.

Return the result table sorted in any order.

The result format is in the following example.

 

Example 1:

Input: 
Visits
+----------+-------------+
| visit_id | customer_id |
+----------+-------------+
| 1        | 23          |
| 2        | 9           |
| 4        | 30          |
| 5        | 54          |
| 6        | 96          |
| 7        | 54          |
| 8        | 54          |
+----------+-------------+
Transactions
+----------------+----------+--------+
| transaction_id | visit_id | amount |
+----------------+----------+--------+
| 2              | 5        | 310    |
| 3              | 5        | 300    |
| 9              | 5        | 200    |
| 12             | 1        | 910    |
| 13             | 2        | 970    |
+----------------+----------+--------+
Output: 
+-------------+----------------+
| customer_id | count_no_trans |
+-------------+----------------+
| 54          | 2              |
| 30          | 1              |
| 96          | 1              |
+-------------+----------------+
Explanation: 
Customer with id = 23 visited the mall once and made one transaction during the visit with id = 12.
Customer with id = 9 visited the mall once and made one transaction during the visit with id = 13.
Customer with id = 30 visited the mall once and did not make any transactions.
Customer with id = 54 visited the mall three times. During 2 visits they did not make any transactions, and during one visit they made 3 transactions.
Customer with id = 96 visited the mall once and did not make any transactions.
As we can see, users with IDs 30 and 96 visited the mall one time without making any transactions. Also, user 54 visited the mall twice and did not make any transactions.

 


풀이

  1. transaction_id 에 없는 customer_id 필터
  2. transaction_id 에 없는 customer_id count 하기
  3. customer_id 기준 집합

 


코드

select v.customer_id, count(v.customer_id) as count_no_trans
from Visits v
left join Transactions t
on v.visit_id = t.visit_id
where t.transaction_id is null
group by v.customer_id

 

 


링크

LeetCode/1581-customer-who-visited-but-did-not-make-any-transactions at main · K-MarkLee/LeetCode

 

LeetCode/1581-customer-who-visited-but-did-not-make-any-transactions at main · K-MarkLee/LeetCode

Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub](https://github.com/QasimWani/LeetHub) - K-MarkLee/LeetCode

github.com

 

 

 

저작자표시 비영리 변경금지 (새창열림)

'Daily 코드카타 > SQL' 카테고리의 다른 글

코딩테스트 SQL 코드카타 (Average Time of Process per Machine)  (0) 2024.11.19
코딩테스트 SQL 코드카나 (Rising Temperature)  (0) 2024.11.18
코딩테스트 SQL 코드카타 (Product Sales Analysis)  (0) 2024.11.17
코딩테스트 SQL 코드카타 (Replace Employee ID)  (1) 2024.11.15
코딩테스트 SQL 코드카타 (Invalid Tweets)  (0) 2024.11.14
'Daily 코드카타/SQL' 카테고리의 다른 글
  • 코딩테스트 SQL 코드카타 (Average Time of Process per Machine)
  • 코딩테스트 SQL 코드카나 (Rising Temperature)
  • 코딩테스트 SQL 코드카타 (Product Sales Analysis)
  • 코딩테스트 SQL 코드카타 (Replace Employee ID)
코드 유랑자 승열
코드 유랑자 승열
코드 유랑자 승열의 프로그래밍 일지를 남기는 공간입니다.
  • 코드 유랑자 승열
    승열의 프로그래밍 시네마
    코드 유랑자 승열
  • 전체
    오늘
    어제
  • 링크

    • 깃허브 보러가기
    • 링크드인 보러가기
    • 인스타그램 보러가기
    • 카테고리
      • 코딩테스트
        • BaekJoon
      • TIL and WIL
        • TIL
        • WIL
      • 주말스터디
      • 내일배움캠프
        • 사전캠프 강의 (SQL)
      • 용어정리
        • Python
        • Python-Library
        • Machine-Learning
        • Deep-Learning
        • AI 활용
        • LLM & RAG
        • Docker
        • Django
        • SQL
        • Java Script
        • etc
      • Daily 코드카타
        • SQL
        • Python 알고리즘
      • 임시저장
      • 보류
  • 태그

    View
    word2vec
    오블완
    django
    vector db
    llm
    티스토리챌린지
    langchain
    template
    RAG
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
코드 유랑자 승열
코딩테스트 SQL 코드카타 (Customer Who Visited without Any Transactions)
상단으로

티스토리툴바