创建SQL报告的最高值查询

时间:2018-02-08 19:29:20

标签: sql sql-server

我在创建SQL报告的顶级值查询时遇到了问题。

我有一个表(同类群组),在我的数据库中,我有以下列

CUML_DEBT_P90 CUML_DEBT_P75 CUML_DEBT_P25 CUML_DEBT_P10

我需要创建正确的sql代码来执行以下操作并遇到问题。我的问题是我对SQL不太熟悉,并且在语法和参数方面遇到了麻烦。任何帮助将不胜感激。

  • 10%完成贷款债务的10所最高和最低学校

  • 完成25%学位的10所最高和最低学校的贷款债务

  • 完成75%学位的10所最高和最低学校的贷款债务

  • 完成90%学位的10所最高和最低学校的贷款债务

我通过管理工作室

使用 SQL Server 2012

SQL语句从CUML_DEBT_P10 desc

中选择Cohorts顺序排名前10 *

结果14177 11250 10000 9500 9299 8544 8350 8250 8125 8108

然而,这并未显示该列的整个数据集的前十个最高值,/此列的hi0ghest值为2172

表中的数据类型是

CREATE TABLE [dbo].[Cohorts](
[UNITID] [float] NULL,
[INSTNM] [nvarchar](255) NULL,
[CUML_DEBT_N] [float] NULL,
[CUML_DEBT_P90] [float] NULL,
[CUML_DEBT_P75] [float] NULL,
[CUML_DEBT_P25] [float] NULL,
[CUML_DEBT_P10] [float] NULL,
[C150_4_POOLED_SUPP] [float] NULL

以下是导入到SQL Server的数据的链接,它是一个excel文件。使用中的表格结构相同。

以下是一些示例数据

UNITID INSTNM CUML_DEBT_N CUML_DEBT_P90 CUML_DEBT_P75 CUML_DEBT_P25 CUML_DEBT_P10 C150_4_POOLED_SUPP

100654 Alabama A& M大学279701 57500 34308 5860 2172 0.819

100663阿拉巴马大学伯明翰分校279701 57500 34308 5860 2172 0.710515789

100706阿拉巴马大学亨茨维尔分校279701 57500 34308 5860 2172 0.250007692

100724阿拉巴马州立大学279701 57500 34308 5860 2172 0.268253659

100751阿拉巴马大学279701 57499 34308 5860 2172 0.44262623

100830蒙哥马利奥本大学279701 57490.5 34308 5860 2172 0.627122034

100858 Auburn University 279701 57490.5 34308 5860 2172 0.9674

100937伯明翰南方学院279701 57490.5 34308 5860 2172 0.557726923

101073 Concordia College Alabama 279701 57490.5 34308 5860 2172 0.705856303

以下是所需结果的示例,但是我需要显示的不仅仅是第90个百分位数,而是需要显示所有提到的百分位数

机构名称累积贷款债务第90百分位

AOMA中西医结合研究生院$ 131,335.00

国际设计与技术学院 - 芝加哥$ 57,500.00

国际设计与科技学院 - 特洛伊$ 57,500.00

国际设计与技术学院 - 纳什维尔$ 57,500.00

柯林斯学院$ 57,500.00

Beulah Heights University $ 57,499.00

南海岸学院$ 57,496.00

Westwood College-Denver South $ 57,490.50

Westwood College-Anaheim $ 57,490.50

Westwood College-Inland Empire $ 57,490.50

这显示了第90个中最高的还需要最低和其他列,如CUML_DEBT_P10等

2 个答案:

答案 0 :(得分:1)

分别运行其中的每一项,您应该拥有所需的数据。

SELECT TOP 10 * FROM COHORTS ORDER BY CUML_DEBT_P90 ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CUML_DEBT_P90 DESC
SELECT TOP 10 * FROM COHORTS ORDER BY CUML_DEBT_P75 ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CUML_DEBT_P75 DESC
SELECT TOP 10 * FROM COHORTS ORDER BY CUML_DEBT_P25 ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CUML_DEBT_P25 DESC
SELECT TOP 10 * FROM COHORTS ORDER BY CUML_DEBT_P10 ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CUML_DEBT_P10 DESC

由于值是FLOAT数据类型,您可以使用STR函数进行转换。

见下文,这里的值按7位数字排列到两位小数。您可以选择您订购的号码的最大长度(此处为7位数字)和该点后的位数(此处,该号码中的(最多)7位数中的2位始终为在点之后),然后按此顺序(如下所示)并且应该解决问题。

SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(DECIMAL,STR(CUML_DEBT_P90,7,2))  ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(DECIMAL,STR(CUML_DEBT_P90,7,2))  DESC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(DECIMAL,STR(CUML_DEBT_P75,7,2))  ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(DECIMAL,STR(CUML_DEBT_P75,7,2))  DESC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(DECIMAL,STR(CUML_DEBT_P25,7,2))  ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(DECIMAL,STR(CUML_DEBT_P25,7,2))  DESC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(DECIMAL,STR(CUML_DEBT_P10,7,2))  ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(DECIMAL,STR(CUML_DEBT_P10,7,2)) DESC

或者作为金钱:

SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P90,8,2))  ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P90,8,2))  DESC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P75,8,2))  ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P75,8,2))  DESC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P25,8,2))  ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P25,8,2))  DESC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P10,8,2))  ASC
SELECT TOP 10 * FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P10,8,2)) DESC

选择UNITID,INSTNM&百分位列,同时保留ORDER BY参数:

SELECT TOP 10 UNITID,INSTNM,CUML_DEBT_P90 FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P90,8,2))  ASC
SELECT TOP 10 UNITID,INSTNM,CUML_DEBT_P90 FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P90,8,2))  DESC
SELECT TOP 10 UNITID,INSTNM,CUML_DEBT_P75 FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P75,8,2))  ASC
SELECT TOP 10 UNITID,INSTNM,CUML_DEBT_P75 FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P75,8,2))  DESC
SELECT TOP 10 UNITID,INSTNM,CUML_DEBT_P25 FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P25,8,2))  ASC
SELECT TOP 10 UNITID,INSTNM,CUML_DEBT_P25 FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P25,8,2))  DESC
SELECT TOP 10 UNITID,INSTNM,CUML_DEBT_P10 FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P10,8,2))  ASC
SELECT TOP 10 UNITID,INSTNM,CUML_DEBT_P10 FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P10,8,2))  DESC

您可以转换选择中的百分位列,以便它们显示(或移动到另一个表)作为您按行排序的数据类型,以达到前10位:

SELECT TOP 10 UNITID,INSTNM,CONVERT(MONEY,STR(CUML_DEBT_P90,8,2)) FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P90,8,2))  ASC
SELECT TOP 10 UNITID,INSTNM,CONVERT(MONEY,STR(CUML_DEBT_P90,8,2)) FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P90,8,2))  DESC
SELECT TOP 10 UNITID,INSTNM,CONVERT(MONEY,STR(CUML_DEBT_P75,8,2)) FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P75,8,2))  ASC
SELECT TOP 10 UNITID,INSTNM,CONVERT(MONEY,STR(CUML_DEBT_P75,8,2)) FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P75,8,2))  DESC
SELECT TOP 10 UNITID,INSTNM,CONVERT(MONEY,STR(CUML_DEBT_P25,8,2)) FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P25,8,2))  ASC
SELECT TOP 10 UNITID,INSTNM,CONVERT(MONEY,STR(CUML_DEBT_P25,8,2)) FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P25,8,2))  DESC
SELECT TOP 10 UNITID,INSTNM,CONVERT(MONEY,STR(CUML_DEBT_P10,8,2)) FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P10,8,2))  ASC
SELECT TOP 10 UNITID,INSTNM,CONVERT(MONEY,STR(CUML_DEBT_P10,8,2)) FROM COHORTS ORDER BY CONVERT(MONEY,STR(CUML_DEBT_P10,8,2))  DESC

答案 1 :(得分:1)

未经测试,

                    SELECT  TOP 10
                            ROW_NUMBER() OVER (ORDER BY CUML_DEBT_P10 ASC) ctr,
                            UNITID,INSTNM,
                            CUML_DEBT_P10,'CUML_DEBT_P10_ASC' Label
                    FROM    [dbo].[Cohorts]


                    SELECT  TOP 10
                            ROW_NUMBER() OVER (ORDER BY CUML_DEBT_P10 DESC) ctr,
                            UNITID,INSTNM,
                            CUML_DEBT_P10,'CUML_DEBT_P10_DESC' Label
                    FROM    [dbo].[Cohorts]


                    SELECT  TOP 10
                            ROW_NUMBER() OVER (ORDER BY CUML_DEBT_P25 ASC) ctr,
                            UNITID,INSTNM,
                            CUML_DEBT_P25,'CUML_DEBT_P25_ASC' Label
                    FROM    [dbo].[Cohorts]


                    SELECT  TOP 10
                            ROW_NUMBER() OVER (ORDER BY CUML_DEBT_P25 ASC) ctr,
                            UNITID,INSTNM,
                            CUML_DEBT_P25,'CUML_DEBT_P25_DESC' Label
                    FROM    [dbo].[Cohorts]


                    SELECT  TOP 10
                            ROW_NUMBER() OVER (ORDER BY CUML_DEBT_P25 ASC) ctr,
                            UNITID,INSTNM,
                            CUML_DEBT_P25,'CUML_DEBT_P25_ASC' Label
                    FROM    [dbo].[Cohorts]


                    SELECT  TOP 10
                            ROW_NUMBER() OVER (ORDER BY CUML_DEBT_P25 ASC) ctr,
                            UNITID,INSTNM,
                            CUML_DEBT_P25,'CUML_DEBT_P25_DESC' Label
                    FROM    [dbo].[Cohorts]



                    SELECT  TOP 10
                            ROW_NUMBER() OVER (ORDER BY CUML_DEBT_P75 ASC) ctr,
                            UNITID,INSTNM,
                            CUML_DEBT_P75,'CUML_DEBT_P75_ASC' Label
                    FROM    [dbo].[Cohorts]

                    SELECT  TOP 10
                            ROW_NUMBER() OVER (ORDER BY CUML_DEBT_P75 ASC) ctr,
                            UNITID,INSTNM,
                            CUML_DEBT_P75,'CUML_DEBT_P75_DESC' Label
                    FROM    [dbo].[Cohorts]



                    SELECT  TOP 10
                            ROW_NUMBER() OVER (ORDER BY CUML_DEBT_P90 ASC) ctr,
                            UNITID,INSTNM,
                            CUML_DEBT_P90,'CUML_DEBT_P90_ASC' Label
                    FROM    [dbo].[Cohorts]


                    SELECT  TOP 10
                            ROW_NUMBER() OVER (ORDER BY CUML_DEBT_P90 ASC) ctr,
                            UNITID,INSTNM,
                            CUML_DEBT_P90,'CUML_DEBT_P90_DESC' Label
                    FROM    [dbo].[Cohorts]