Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在两个层面上转移注意力_Sql Server_Database_Tsql_Pivot_Relational Database - Fatal编程技术网

Sql server 如何在两个层面上转移注意力

Sql server 如何在两个层面上转移注意力,sql-server,database,tsql,pivot,relational-database,Sql Server,Database,Tsql,Pivot,Relational Database,我正在使用SQL Server 2012,并试图根据下表(通过连接多个表生成)从TSQL构建一个透视表 INCIDENT ID | Department | Priority | Impact -------------------------------------------- 1 | IT | Urgent | High 2 | IT | Retrospective | Medium 3

我正在使用SQL Server 2012,并试图根据下表(通过连接多个表生成)从TSQL构建一个透视表

INCIDENT ID | Department | Priority      | Impact 
--------------------------------------------
1           | IT         | Urgent        | High
2           | IT         | Retrospective | Medium   
3           | Marketing  | Normal        | Low
4           | Marketing  | Normal        | High
5           | Marketing  | Normal        | Med
6           | Finance    | Normal        | Med
在此表中,希望以以下格式显示:

Priority     | Normal              | Urgent              | Retrospective       |
| Department | Low | Medium | High | Low | Medium | High | Low | Medium | High |
--------------------------------------------------------------------------------
| IT         |   1 |      1 |    0 |   1 |      1 |    0 |   1 |      1 |    0 |
| Finance    |   0 |      0 |    1 |   1 |      1 |    0 |   1 |      1 |    0 |
| Marketing  |   0 |      1 |    0 |   1 |      1 |    0 |   1 |      1 |    0 |
我有以下代码,它成功地围绕优先级旋转

SELECT *
FROM (
    SELECT 
        COUNT(incident.incident_id) OVER(PARTITION BY serv_dept.serv_dept_n) Total,
        serv_dept.serv_dept_n       Department,
        ImpactName.item_n           Impact,
        PriorityName.item_n         Priority    
    FROM --  ommitted for brevity
    WHERE  -- ommitted for brevity
) AS T

PIVOT (
    COUNT(Priority)
    FOR Priority IN ("Normal", "Urgent", "Retrospective")
) PIV
ORDER BY Department ASC
我如何让这个查询像我粘贴的第二个表一样在两个级别上进行透视?
任何帮助都将不胜感激。

最简单的方法可能是条件聚合:

select department,
       sum(case when priority = 'Normal' and target = 'Low' then 1 else 0 end) as Normal_low,
       sum(case when priority = 'Normal' and target = 'Med' then 1 else 0 end) as Normal_med,
       sum(case when priority = 'Normal' and target = 'High' then 1 else 0 end) as Normal_high,
       . . .
from t
group by department;

最简单的方法可能是条件聚合:

select department,
       sum(case when priority = 'Normal' and target = 'Low' then 1 else 0 end) as Normal_low,
       sum(case when priority = 'Normal' and target = 'Med' then 1 else 0 end) as Normal_med,
       sum(case when priority = 'Normal' and target = 'High' then 1 else 0 end) as Normal_high,
       . . .
from t
group by department;

我要试试看:

WITH PivotData AS
(
    SELECT
        Department
        , Priority + '_' + Impact AS PriorityImpact
        , Incident_ID
    FROM
        <table>
)
SELECT
    Department
    , Normal_Low
    , Normal_Medium
    ,...
FROM
PivotData
PIVOT (COUNT(Incident_ID FOR PriorityImpact IN (<Listing all the PriorityImpact values>) ) as P;

我要试试看:

WITH PivotData AS
(
    SELECT
        Department
        , Priority + '_' + Impact AS PriorityImpact
        , Incident_ID
    FROM
        <table>
)
SELECT
    Department
    , Normal_Low
    , Normal_Medium
    ,...
FROM
PivotData
PIVOT (COUNT(Incident_ID FOR PriorityImpact IN (<Listing all the PriorityImpact values>) ) as P;

您的示例数据没有财务部门。这些信息是从哪里来的?这只是我为了解释而输入的随机数据。你的样本数据没有财务部门。这些信息是从哪里来的?这只是我为了解释而输入的随机数据。这个方法可以移植到其他数据库吗?它的语法是针对TSQL的。我不知道你指的是哪个数据库。我认为这必须取决于供应商是否支持PIVOT以及该供应商的特定语法。感谢您提出解决方案。我假设您的WITH子句与我在OP中使用的select子查询是等价的。但是我认为我更喜欢Gordon Linoff的答案。我同意您的看法。我也喜欢戈登的答案。我添加此答案的唯一原因是因为您似乎要求提供一个pivot解决方案。此方法可移植到其他数据库吗?此方法的语法适用于TSQL。我不知道你指的是哪个数据库。我认为这必须取决于供应商是否支持PIVOT以及该供应商的特定语法。感谢您提出解决方案。我假设您的WITH子句与我在OP中使用的select子查询是等价的。但是我认为我更喜欢Gordon Linoff的答案。我同意您的看法。我也喜欢戈登的答案。我添加这个答案的唯一原因是因为您似乎要求一个枢轴解决方案。