根据月份和年份订购交叉选项卡查询

Posted

技术标签:

【中文标题】根据月份和年份订购交叉选项卡查询【英文标题】:ORDERING CROSS TAB QUERY BASED ON MONTH AND YEAR 【发布时间】:2013-07-06 08:17:33 【问题描述】:

这是我的表架构

+---------------+----------+-------------------+-----------------------------+----------------------------------+-----------+-------------+------------+-------------+--------+----------+----------+
|   VERTICAL    |   LOB    |      PROCESS      |         SUB PROCESS         |             ACTIVITY             |   MNTH    | OPPORTUNITY | DEFECTS(F) | DEFECTS(NF) |  CTQ   |   TYPE   | CATEGORY |
+---------------+----------+-------------------+-----------------------------+----------------------------------+-----------+-------------+------------+-------------+--------+----------+----------+
| Bill Delivery | Mobility | Service Complaint | Original eBill not Received | Original eBill not received      | 31-May-13 |         895 |          0 |          20 | Repeat | Customer | D        |
| Bill Delivery | Mobility | Service Complaint | MNP- Bill Delivery          | Bill Delivery Issue              | 30-Apr-13 |          12 |          0 |           0 | CAR    | Customer | C        |
| Bill Delivery | Mobility | Service Complaint | MNP- Bill Delivery          | Bill Delivery Issue              | 30-Apr-13 |         179 |          0 |           0 | CAR    | Customer | C        |
| Bill Delivery | Mobility | Service Complaint | Original Bill not Received  | Original Paper Bill not received | 30-Apr-13 |        1157 |          0 |           0 | CAR    | Customer | D        |
| Bill Delivery | Mobility | Service Complaint | Original Bill not Received  | Original Paper Bill not received | 30-Apr-13 |       16381 |          0 |          38 | CAR    | Customer | D        |
| Bill Delivery | Mobility | Service Complaint | Original eBill not Received | Original eBill not received      | 30-Apr-13 |          75 |          0 |           1 | CAR    | Customer | D        |
| Bill Delivery | Mobility | Service Complaint | Original eBill not Received | Original eBill not received      | 30-Apr-13 |         913 |          0 |          10 | CAR    | Customer | D        |
| Bill Delivery | Mobility | Service Request   | Duplicate Bill to be sent   | Duplicate Paper Bill to be sent  | 30-Apr-13 |        1022 |          0 |           1 | CAR    | Customer | D        |
| Bill Delivery | Mobility | Service Request   | Duplicate Bill to be sent   | Duplicate Paper Bill to be sent  | 30-Apr-13 |       14278 |          0 |          25 | CAR    | Customer | D        |
| Bill Delivery | Mobility | Service Request   | Other Delivery related      | Statement of Account to be sent  | 30-Apr-13 |          16 |          0 |           0 | CAR    | Customer | D        |
| Bill Delivery | Mobility | Service Request   | Other Delivery related      | Statement of Account to be sent  | 30-Apr-13 |         627 |          0 |          17 | CAR    | Customer | D        |
| Billing       | Mobility | Service Complaint | Billing Discrepancy         | 2G -  Usage & Rental Issue       | 30-Apr-13 |         849 |          0 |          40 | CAR    | Customer | C        |
| Billing       | Mobility | Service Complaint | Billing Discrepancy         | 2G -  Usage & Rental Issue       | 30-Apr-13 |        7834 |          0 |         569 | CAR    | Customer | C        |
| Billing       | Mobility | Service Complaint | Billing Discrepancy         | 3G -  Usage & Rental Issue       | 30-Apr-13 |         529 |          0 |          31 | CAR    | Customer | C        |
| Billing       | Mobility | Service Complaint | Billing Discrepancy         | 3G -  Usage & Rental Issue       | 30-Apr-13 |        3456 |          0 |         233 | CAR    | Customer | C        |
| Billing       | Mobility | Service Complaint | Billing Discrepancy         | 4G -  Usage & Rental Issue       | 30-Apr-13 |          17 |          0 |           0 | CAR    | Customer | C        |
+---------------+----------+-------------------+-----------------------------+----------------------------------+-----------+-------------+------------+-------------+--------+----------+----------+

我已使用以下交叉表查询来获得所需格式的输出

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

    SELECT @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(DATENAME(MONTH, MNTH)+''+DATENAME(YEAR, MNTH)) 
                    FROM CAPABILITY
                  WHERE DATENAME(YEAR,MNTH)>2012
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
SET @query = 'SELECT VERTICAL, PROCESS, [SUB PROCESS], ACTIVITY, CTQ, TYPE,' 

+@cols+ 
'FROM  

(
SELECT VERTICAL, PROCESS,TYPE,CTQ,[SUB PROCESS], ACTIVITY,
    CASE
WHEN
(SUM([DEFECTS(F)])+SUM([DEFECTS(NF)]))=0
THEN
6
WHEN
(SUM([DEFECTS(F)])+SUM([DEFECTS(NF)]))= SUM(OPPORTUNITY)
THEN
0
WHEN
(SUM([DEFECTS(F)])+SUM([DEFECTS(NF)]))> SUM(OPPORTUNITY)
THEN 
''ERROR'' 
ELSE
DBO.NORMSINV((1-(SUM([DEFECTS(F)])+SUM([DEFECTS(NF)]))/SUM(OPPORTUNITY)))+1.5
END AS Sigma    

    ,DATENAME(MONTH, MNTH)+''''+DATENAME(YEAR, MNTH)
 AS MONTHS
    FROM CAPABILITY

        GROUP BY VERTICAL, PROCESS, DATENAME(MONTH, MNTH)+''''+DATENAME(YEAR, MNTH),TYPE,CTQ,[SUB PROCESS],ACTIVITY





)X

PIVOT 
            (
                MIN(SIGMA)
                FOR MONTHS in (' + @cols + ')
            ) X '

            --//PRINT (@QUERY)
EXECUTE (@QUERY)

这给出了以下输出

+--------------+--------------+-------------------------------------+-------------------------------------+-----+---------------+--------+--------+--------+--------+--------+
|   VERTICAL   |   PROCESS    |             SUB PROCESS             |              ACTIVITY               | CTQ |     TYPE      | Feb-13 | Apr-13 | May-13 | Mar-13 | Jan-13 |
+--------------+--------------+-------------------------------------+-------------------------------------+-----+---------------+--------+--------+--------+--------+--------+
| Airtel Money | Airtel Money | 10% cashback                        | 10% cashback                        | SQ  | Transactional | NULL   | 6      | NULL   | NULL   | NULL   |
| Airtel Money | Airtel Money | 5 % Cashback                        | 5 % Cashback                        | SQ  | Transactional | NULL   | 6      | NULL   | NULL   | NULL   |
| Airtel Money | Airtel Money | 5 % Cashback Post paid Self payment | 5 % Cashback Post paid Self payment | SQ  | Transactional | NULL   | NULL   | 6      | NULL   | NULL   |
| Airtel Money | Airtel money | APEF                                | APEF                                | SQ  | Transactional | NULL   | NULL   | NULL   | NULL   | NULL   |
| Airtel Money | Airtel money | Bank File                           | BANK FILE                           | SQ  | Transactional | NULL   | NULL   | NULL   | NULL   | NULL   |
+--------------+--------------+-------------------------------------+-------------------------------------+-----+---------------+--------+--------+--------+--------+--------+

我面临的唯一限制是我无法根据月份和年份以降序(或升序)顺序对详细信息进行排序。因此,我希望以正确的顺序(1 月 13 日至 5 月 13 日)代替“FEB -13”、“APRIL - 13”、“MAY -13”、“MARCH - 13”、“JAN -13”。有人可以在这里帮助我吗?

【问题讨论】:

我没有尝试,但是如果将 ORDER BY MNTH 添加到 SELECT for XML 和 PIVOT 本身会怎样? 【参考方案1】:

试试这个

   SELECT @cols = ISNULL(@cols+',','') + QUOTENAME(DATENAME(MONTH, MNTH)+''+DATENAME(YEAR, MNTH)) 
    FROM CAPABILITY
    WHERE MNTH>='20120101'
    GROUP BY DATENAME(MONTH, MNTH), DATENAME(YEAR, MNTH), DATEPART(MONTH,mnth)
    ORDER BY DATENAME(YEAR, MNTH), DATEPART(MONTH,mnth)

我也换了

WHERE DATENAME(YEAR,MNTH)>2012

WHERE MNTH>='20120101'

因为第一个保证 Index Scan 不是人们想要的。

【讨论】:

以上是关于根据月份和年份订购交叉选项卡查询的主要内容,如果未能解决你的问题,请参考以下文章

根据搜索查询查找结果

Android:为什么在创建选项卡时会调用onTabSelected?

chrome调试器:单击控制台中的异常链接会在新选项卡中打开文件,而不是交叉链接到源文件

引导、导航选项卡、下拉菜单;如何根据 URI 设置活动选项卡

AWS Redshift CTAS 查询在集群查询选项卡中完成,但仍从客户端 sql 工作台/j 运行。该表也未创建

在 Pivot SQL 表中排序月份