列到行

Columns to rows

我正在使用 SQL 2008 数据库 我正在处理一份报告 我想在图表中显示以供我执行此操作 我需要将我的结果从列转换为行 这是我的查询

SELECT 
COUNT(CASE WHEN (pm.mortgagestatus) = 10  then 1 end) AS Received,
SUM(case WHEN (pm.mortgagestatus) = 10 then pm.AmountRequested Else 0 END) AS [Received Amount],
COUNT(CASE WHEN (pm.mortgagestatus) = 4  then 1 end) AS Passed,
SUM(case WHEN (pm.mortgagestatus) = 4 then pm.AmountRequested Else 0 END) AS [Passed Amount],
COUNT(CASE WHEN (pm.mortgagestatus) = 5  then 1 end) AS packaged,
SUM(case WHEN (pm.mortgagestatus) = 5 then pm.AmountRequested Else 0 END) AS [Packaged Amount],
COUNT(CASE WHEN (pm.mortgagestatus) = 6  then 1 end) AS Offered,
SUM(case WHEN (pm.mortgagestatus) = 6 then pm.AmountRequested Else 0 END) AS [Offered Amount]
FROM tbl_Profile AS p
INNER JOIN tbl_Profile_Mortgage AS pm
ON p.id = pm.fk_ProfileID

这个查询 returns

我想像这样查询 return。

这将使我能够将数据放入图表中。 非常感谢对此的任何帮助。 干杯

使用聚合 case:

SELECT (CASE WHEN pm.mortgagestatus = 10  then 'Received'
             WHEN pm.mortgagestatus = 4  then 'Passed'
             WHEN pm.mortgagestatu) = 5  then 'packaged'
             WHEN pm.mortgagestatus = 6  then 'Offered'
        END) as Mortgage_Status,
       COUNT(*) as Total,
       SUM(pm.AmountRequested) as LoanAmount
FROM tbl_Profile p INNER JOIN
     tbl_Profile_Mortgage pm
     ON p.id = pm.fk_ProfileID
WHERE pm.mortgagestatus IN (4, 5, 6, 10)
GROUP BY (CASE WHEN pm.mortgagestatus = 10  then 'Received'
               WHEN pm.mortgagestatus = 4  then 'Passed'
               WHEN pm.mortgagestatu) = 5  then 'packaged'
               WHEN pm.mortgagestatus = 6  then 'Offered'
          END);