diff --git a/problem1-512-game-play-analysis2.sql b/problem1-512-game-play-analysis2.sql new file mode 100644 index 0000000..61c3162 --- /dev/null +++ b/problem1-512-game-play-analysis2.sql @@ -0,0 +1,18 @@ +with row_num_based_user_activity as ( + select + player_id, + device_id, + row_number() over ( + partition by player_id + order by event_date + ) as rnk + from activity +) + +select + player_id, + device_id +from + row_num_based_user_activity +where + rnk = 1 \ No newline at end of file diff --git a/problem2-534-game-play-analysis3.sql b/problem2-534-game-play-analysis3.sql new file mode 100644 index 0000000..e743b0a --- /dev/null +++ b/problem2-534-game-play-analysis3.sql @@ -0,0 +1,12 @@ +select + player_id, + event_date, + sum(games_played) + over ( + partition by player_id + order by event_date + ) + as games_played_so_far +from + activity +order by player_id, event_date \ No newline at end of file diff --git a/problem3-612-shortestt-distance-plane.sql b/problem3-612-shortestt-distance-plane.sql new file mode 100644 index 0000000..f688592 --- /dev/null +++ b/problem3-612-shortestt-distance-plane.sql @@ -0,0 +1,8 @@ +select + round(min(sqrt(power(p2.x - p1.x, 2) + power(p2.y - p1.y, 2))), 2) + as shortest +from + point2d as p1 +inner join + point2d as p2 + on (p1.x, p1.y) < (p2.x, p2.y) \ No newline at end of file diff --git a/problem4-175-combine-two-tables.sql b/problem4-175-combine-two-tables.sql new file mode 100644 index 0000000..1b3e4fa --- /dev/null +++ b/problem4-175-combine-two-tables.sql @@ -0,0 +1,11 @@ +select + FIRSTNAME, + LASTNAME, + CITY, + STATE +from + PERSON +left join + ADDRESS + on + PERSON.PERSONID = ADDRESS.PERSONID \ No newline at end of file diff --git a/problem5-2474-customers-with-strictly-increase-purchase.sql b/problem5-2474-customers-with-strictly-increase-purchase.sql new file mode 100644 index 0000000..c91df3a --- /dev/null +++ b/problem5-2474-customers-with-strictly-increase-purchase.sql @@ -0,0 +1,25 @@ +with cte_customer_yoy_purchase as ( + select + customer_id, + YEAR(order_date) as order_year, + SUM(price) as total_price + from + orders + group by + customer_id, + YEAR(order_date) + order by + customer_id +) + +select cyoy.customer_id +from + cte_customer_yoy_purchase as cyoy +left join + cte_customer_yoy_purchase as cyoy1 + on + cyoy.customer_id = cyoy1.customer_id + and cyoy.order_year + 1 = cyoy1.order_year + and cyoy.total_price < cyoy1.total_price +group by cyoy.customer_id +having COUNT(*) - COUNT(cyoy1.customer_id) = 1