课程: SQL Hands-On Practice: Solve Business Problems

免费学习该课程!

今天就开通帐号,24,700 门业界名师课程任您挑!

Solution: Comparing rows within the same table

Solution: Comparing rows within the same table - SQL教程

课程: SQL Hands-On Practice: Solve Business Problems

Solution: Comparing rows within the same table

- [Instructor] We've already looked at some examples of self joins where we're representing a hierarchical relationship but now we're going to look at the use case where you can self join to compare rows within the same table. This is a more creative and advanced use case of self joins. It's not something I use all the time on the job but it's good to know how to do so you exercise your SQL skills and grow in your understanding of how self joins work. Since we're comparing monthly revenues for this problem, we first need to create a CTE that calculates the monthly revenues from the subscriptions table. I've given you that code on lines two through eight. Let's run this to see what it looks like. When we run this, we have the total summed revenue amount for each month. Alright, now let's turn this into a CTE called "Monthly Revs" and start the next part of the query that involves the self join. We're going to scroll back up…

内容