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

免费学习该课程!

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

Solution: Unpivoting columns into rows using UNION

Solution: Unpivoting columns into rows using UNION - SQL教程

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

Solution: Unpivoting columns into rows using UNION

- [Instructor] So now that we have the hang of unions, let's look at this example where all of the data we need is in one table, but the data point we want to analyze is logged in multiple columns. So we have three cancellation reason columns, but we want them all in the same column so we can count them. One way to do this is to un-pivot the columns into rows using union. I've already given you the code to turn this result set into a CTE called All Cancellation Reasons. I've also already given you the code to calculate the percent of users that reported expensive as their cancellation reason, but I'm going to walk you through how it works later. First, let's create a query that combines all the cancellation reasons into one output. We're going to first erase this commented outline. You can leave it if you want, but I'm going to get it out of our way. We're going to start with select, subscriptionid and…

内容