如何在 SQL 服务器中使用多列进行数据透视?

How to pivot using multiple columns in SQL Server?

我在创建遵循下述架构的枢轴 table 时遇到问题。 例如,使用以下 table(此处未显示所有记录):

Route Bus_Fare_Payment_Method Total_Annual_Household_Income
Route 1 10-Ride Pass K To K
Route 1 10-Ride Pass K To K
Route 1 10-Ride Pass K To K
Route 1 10-Ride Pass K Or More
Route 1 Regular Cash Fare Under K
Route 1 Regular Cash Fare Under K
Route 1 Regular Cash Fare Under K
Route 1 Regular Cash Fare Under K
Route 1 Regular Cash Fare Under K
Route 1 Regular Cash Fare Under K
Route 1 Regular Cash Fare Under K
Route 1 Regular Cash Fare Under K
Route 10 10-Ride Pass K To K
Route 10 31-Day Adult K To K
Route 10 31-Day Adult K To K
Route 10 31-Day Adult K To K
Route 10 31-Day Adult K To K
Route 10 31-Day Adult K To K
Route 10 31-Day Adult K To K
Route 10 31-Day Adult K To K
Route 10 31-Day Adult K To K
Route 10 31-Day Adult K To K
Route 101 All Day Pass Reduced Under K
Route 101 Other Under K
Route 101 Reduced Fare K To K
Route 101 Reduced Fare K To K
Route 101 Reduced Fare K To K
Route 101 Reduced Fare K To K
Route 101 Reduced Fare K Or More
Route 101 Reduced Fare K Or More
Route 101 Reduced Fare K Or More
Route 101 Reduced Fare Under K
Route 101 Reduced Fare Under K
Route 101 Reduced Fare Under K
Route 101 Regular Cash Fare K To K
Route 101 Regular Cash Fare K To K
Route 101 Regular Cash Fare K To K
Route 101 Regular Cash Fare K To K

我想制作以下内容table:

Route Bus_Fare_Payment_Method K To K K To K K To K K To K K To K K To K K Or More Under K
Route 1 10-Ride Pass 2 1 1
Route 1 31-Day Adult
Route 1 All Day Pass Reduced
Route 1 Other
Route 1 Reduced Fare
Route 1 Regular Cash Fare 8
Route 10 10-Ride Pass 1
Route 10 31-Day Adult 4 1 4
Route 10 All Day Pass Reduced
Route 10 Other
Route 10 Reduced Fare
Route 10 Regular Cash Fare
Route 101 10-Ride Pass
Route 101 31-Day Adult
Route 101 All Day Pass Reduced 1
Route 101 Other 1
Route 101 Reduced Fare 1 1 1 1 3 3
Route 101 Regular Cash Fare 4

我可以使用下面包含的查询创建以下内容 table,但我缺少需要作为输出一部分的 Route 字段(如上所示)。

SELECT [Bus_Fare_Payment_Method] "Bus Fare Payment Method", [Under k] 'Under k', [K to K] 'K to K',[k to k] 'k to k', [k to k] 'k to k', [k to k] 'k to k', [k to k] 'k to k', [k to k] 'k to k', [k to k] 'k to k', [k or more] 'k or more'
FROM   
(SELECT [Route], [Total_Annual_Household_Income], [Bus_Fare_Payment_Method]  
FROM [BCT_TDP_SURVEY_2018] where [Bus_Fare_Payment_Method] != '' ) p  
PIVOT  
(  
COUNT ([Route])  
FOR [Total_Annual_Household_Income] IN  
( [Under k], [K to K],[k to k], [k to k], [k to k], [k to k], [k to k], [k to k], [k or more] )  
) AS pvt  
ORDER BY pvt.[Bus_Fare_Payment_Method]
Bus_Fare_Payment_Method K To K K To K K To K K To K K To K K To K K Or More Under K
10-Ride Pass 2 1 1 1
31-Day Adult 4 1 4
All Day Pass Reduced 1
Other 1
Reduced Fare 1 1 1 1 3 3
Regular Cash Fare 4 8

正如我在评论中提到的,与限制性 PIVOT 运算符相比,使用条件聚合要好得多:

SELECT Route,
       Bus_Fare_Payment_Method,
       COUNT(CASE Total_Annual_Household_Income WHEN 'K To K' THEN 1 END) AS [K To K],
       COUNT(CASE Total_Annual_Household_Income WHEN 'K To K' THEN 1 END) AS [K To K],
       ...
FROM dbo.YourTable
GROUP BY Route,
         Bus_Fare_Payment_Method;