我的T-SQL代码从表格中选择有关大学债务金额的信息。代码本身有效,但我需要打破单行。有4行,每个债务级别一个。我可能在这一点上遗漏了一些简单的东西。
USE [College Database]
GO
DECLARE @P10W FLOAT, @P25W FLOAT, @P75W FLOAT, @P90W FLOAT, @P10A FLOAT, @P25A FLOAT, @P75A FLOAT, @P90A FLOAT
SELECT @P10W = CONVERT(DECIMAL(6,0),SUM(CUML_DEBT_P10*CUML_DEBT_N)/SUM(CUML_DEBT_N)),
@P25W = CONVERT(DECIMAL(6,0),SUM(CUML_DEBT_P25*CUML_DEBT_N)/SUM(CUML_DEBT_N)),
@P75W = CONVERT(DECIMAL(6,0),SUM(CUML_DEBT_P75*CUML_DEBT_N)/SUM(CUML_DEBT_N)),
@P90W = CONVERT(DECIMAL(6,0),SUM(CUML_DEBT_P90*CUML_DEBT_N)/SUM(CUML_DEBT_N))
FROM CollegeInfo
SELECT
@P10A = CONVERT(DECIMAL(6,0),AVG(CUML_DEBT_P10)),
@P25A = CONVERT(DECIMAL(6,0),AVG(CUML_DEBT_P25)),
@P75A = CONVERT(DECIMAL(6,0),AVG(CUML_DEBT_P75)),
@P90A = CONVERT(DECIMAL(6,0),AVG(CUML_DEBT_P90))
From CollegeInfo
SELECT '10% Debt Level',
CUML_DEBT_P10 AS 'Institution',
@P10A AS 'Average',
@P10W AS ' Weighted Average' ,
'25% Debt Level',
CUML_DEBT_P25 AS 'Institution',
@P25A AS 'Average',
@P25W AS ' Weighted Average' ,
'75% Debt Level',
CUML_DEBT_P75 AS 'Institution',
@P75A AS 'Average',
@P75W AS ' Weighted Average' ,
'90% Debt Level',
CUML_DEBT_P90 AS 'Institution',
@P90A AS 'Average',
@P90W AS ' Weighted Average'
FROM CollegeInfo
WHERE INSTNM LIKE'%Alabama A & M%'
现在它显示为
10% Debt Level 4750 3530 2816 25% Debt Level 9500 6243 5990 75% Debt Level 37900 18543 27976 90% Debt Level 50114 25147 39153
但它需要读作
10% Debt Level 4750 3530 2816
25% Debt Level 9500 6243 5990
75% Debt Level 37900 18543 27976
90% Debt Level 50114 25147 39153
答案 0 :(得分:0)
像这样:
SELECT '10% Debt Level'
, CUML_DEBT_P10 AS 'Institution'
, @P10A AS 'Average'
, @P10W AS ' Weighted Average'
FROM CollegeInfo
WHERE INSTNM LIKE'%Alabama A & M%'
UNION ALL
SELECT'25% Debt Level',
, CUML_DEBT_P25 AS 'Institution'
, @P25A AS 'Average'
, @P25W AS ' Weighted Average'
FROM CollegeInfo
WHERE INSTNM LIKE'%Alabama A & M%'
UNION ALL
SELECT '75% Debt Level'
, CUML_DEBT_P75 AS 'Institution'
, @P75A AS 'Average'
, @P75W AS ' Weighted Average'
FROM CollegeInfo
WHERE INSTNM LIKE'%Alabama A & M%'
UNION ALL
SELECT '90% Debt Level'
, CUML_DEBT_P90 AS 'Institution'
, @P90A AS 'Average'
, @P90W AS ' Weighted Average'
FROM CollegeInfo
WHERE INSTNM LIKE'%Alabama A & M%'
我想你也可以这样做:
WITH CTE AS (
SELECT '10% Debt Level' debtLevel,
CUML_DEBT_P10 AS 'InstitutionA',
@P10A AS 'AverageA',
@P10W AS [Weighted AverageA] ,
'25% Debt Level',
CUML_DEBT_P25 AS 'InstitutionB',
@P25A AS 'AverageB',
@P25W AS [Weighted AverageB] ,
'75% Debt Level',
CUML_DEBT_P75 AS 'InstitutionC',
@P75A AS 'AverageC',
@P75W AS [Weighted AverageC] ,
'90% Debt Level',
CUML_DEBT_P90 AS 'InstitutionD',
@P90A AS 'AverageD',
@P90W AS [Weighted AverageD]
FROM CollegeInfo
WHERE INSTNM LIKE'%Alabama A & M%')
SELECT B.DL
, max(CASE WHEN B.DL='10% Debt Level' THEN CTE.InstitutionA
WHEN B.DL='25% Debt Level' THEN CTE.InstitutionB
WHEN B.DL='75% Debt Level' THEN CTE.InstitutionC
WHEN B.DL='90% Debt Level' THEN CTE.InstitutionD END) as Institution
, max(CASE WHEN B.DL='10% Debt Level' THEN CTE.AverageA
WHEN B.DL='25% Debt Level' THEN CTE.AverageB
WHEN B.DL='75% Debt Level' THEN CTE.AverageC
WHEN B.DL='90% Debt Level' THEN CTE.AverageD END) as Average
, max(CASE WHEN B.DL='10% Debt Level' THEN CTE.[Weighted AverageA]
WHEN B.DL='25% Debt Level' THEN CTE.[Weighted AverageB]
WHEN B.DL='75% Debt Level' THEN CTE.[Weighted AverageC]
WHEN B.DL='90% Debt Level' THEN CTE.[Weighted AverageD] END) as WeightedAverage
FROM CTE
CROSS JOIN (SELECT '10% Debt Level' DL UNION ALL
SELECT '25% Debt Level' UNION ALL
SELECT '75% Debt Level' UNION ALL
SELECT '90% Debt Level') B
GROUP BY B.DL