diff --git a/Combine_Two_Tables.sql b/Combine_Two_Tables.sql new file mode 100644 index 0000000..7ad9a02 --- /dev/null +++ b/Combine_Two_Tables.sql @@ -0,0 +1,5 @@ +#Solution1 +select p.firstname, p.lastname, a.city , a.state from person p left join address a on p.personid = a.personid + +#Solution2 +select p.firstname, p.lastname, a.city , a.state from address a right join person p on p.personid = a.personid \ No newline at end of file diff --git a/Customers_with_Strictly_Increasing_Purchases.sql b/Customers_with_Strictly_Increasing_Purchases.sql new file mode 100644 index 0000000..39c207f --- /dev/null +++ b/Customers_with_Strictly_Increasing_Purchases.sql @@ -0,0 +1,8 @@ +with cte as ( + select customer_id, year(order_date) as 'year' , sum(price) as 'price' from orders + group by year , customer_id order by customer_id,year) + +select c1.customer_id from cte c1 +left join cte c2 on c1.customer_id = c2.customer_id +and c1.year+1 = c2.year and c2.price > c1.price +group by c1.customer_id having count(*) - count(c2.customer_id) = 1 \ No newline at end of file diff --git a/Game_Play_AnalysisII.sql b/Game_Play_AnalysisII.sql new file mode 100644 index 0000000..8a90927 --- /dev/null +++ b/Game_Play_AnalysisII.sql @@ -0,0 +1,20 @@ +#Solution1 +with cte as ( + select player_id, device_id, min(event_date) as 'event_date' from activity + group by player_id ) +select a.player_id, a.device_id from activity a + join cte c on a.player_id = c.player_id + and a.event_date = c.event_date; + + +#Solution2 +select distinct a.player_id, first_value(a.device_id) over(partition by a.player_id order by a.event_date) as device_id from activity a + +#Solution3 +select a.player_id, a.device_id from activity a where a.event_date in (select min(b.event_date) from activity b where a.player_id = b.player_id); + +#Solution4 +with cte as ( select distinct player_id, device_id, rank() over(partition by player_id order by event_date) as 'rnk' from activity)select player_id, device_id from cte where rnk = 1; + +#Solution5 +select a1.player_id, a1.device_id from activity a1 where (a1.player_id, a1.event_date) in (select a2.player_id, min(a2.event_date) from activity a2 group by a2.player_id) \ No newline at end of file diff --git a/Game_Play_Analysis_III.sql b/Game_Play_Analysis_III.sql new file mode 100644 index 0000000..04ca460 --- /dev/null +++ b/Game_Play_Analysis_III.sql @@ -0,0 +1,8 @@ +#Solution1 +select player_id, event_date, sum( games_played) over (partition by player_id order by event_date) as games_played_so_far from activity + +#Solution2 +select a1.player_id, a1.event_date, sum(a1.games_played) as games_played_so_far from activity a1 inner join activity a2 on a1.player_id = a2.player_id and a1.event_date <= a2.event_date group by a2.player_id,a2.event_date + +#Solution3 +select a1.player_id, a1.event_date, (select sum(a2.games_played) from activity a2 where a1.player_id = a2.player_id and a2.event_date <= a1.event_date) as 'games_played_so_far' from activity a1; \ No newline at end of file diff --git a/Shortest_Distance_in_a_Plane.sql b/Shortest_Distance_in_a_Plane.sql new file mode 100644 index 0000000..a57fc8d --- /dev/null +++ b/Shortest_Distance_in_a_Plane.sql @@ -0,0 +1,8 @@ +#Solution1 +select round(sqrt(min(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2))),2) as 'shortest' from point2d p1 inner join point2d p2 on p1.x != p2.x or p1.y != p2.y + +#Solution2 +select round(sqrt(min(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2))),2) as 'shortest' from point2d p1 inner join point2d p2 on +p1.x <= p2.x and p1.y < p2.y or +p1.x <= p2.x and p1.y > p2.y or +p1.x > p2.x and p1.y = p2.y \ No newline at end of file