Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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 Microsoft Sql Server使用Pivot创建多个聚合函数_Sql Server_Pivot - Fatal编程技术网

Sql server Microsoft Sql Server使用Pivot创建多个聚合函数

Sql server Microsoft Sql Server使用Pivot创建多个聚合函数,sql-server,pivot,Sql Server,Pivot,我有这样的数据 select * from age; Payor Description ID Amount --------------------------------- Medical S1 101 200 Medical s1 102 100 Medical S2 201 400 Medical S2 202 450 Medical S3 301

我有这样的数据

select * from age;

Payor   Description  ID    Amount
---------------------------------
Medical    S1        101    200
Medical    s1        102    100
Medical    S2        201    400
Medical    S2        202    450
Medical    S3        301    500
我需要以下格式的数据,前3列给出总量,后3列给出Id的不同计数

Payor   S1  S2  S3  S1_Count S2_Cnt S3_cnt 
-------------------------------------------
Medical 300 850 500   2        2      1
或者我可以在不同的多行中显示相同的数据

Payor   s1  s2  s3
--------------------
Medical 300 850 500
Medical  2   2   1
一个聚合函数给出数量之和,另一个聚合函数给出id的不同计数

我曾尝试使用Pivot,但我无法理解如何在一个SQL命令中使用两个聚合函数。当我使用
count(distinct ID)
时,它立即抛出一个错误,不确定我犯了什么错误

使用下面的代码,我可以得到前3列s1、s2、s3,它们是特定付款人的金额总和

SELECT 
    [payor], [s1], [s2], [s3]            
FROM   
    (SELECT 
         [payor], des, amount 
     FROM age) AS age1 ---- from age table
PIVOT 
    (SUM(amount) FOR des IN ([s1], [s2], [s3] ) AS t2; --- sum     
有人能帮我找到我想要的正确格式吗?

试试UNION:

Select payor, S1, S2, S3
FROM
(
Select payor, Description, count(ID) CountVal, 1 AS Orderby from table
group by payor, Description) z
PIVOT
(
MAX(CountVal) FOR Description IN ([S1], [S2], [S3])
) x

UNION

Select payor, S1, S2, S3
FROM
(
Select payor, Description, SUM(Amount) SumVal, 0 AS Orderby from table
group by payor, Description) z
PIVOT
(
MAX(SumVal) FOR Description IN ([S1], [S2], [S3])
) x
输出:

payor       S1      S2      S3
-------------------------------
Medical     2       2       1
Medical     300     850     500
Payor   S1      S2      S3  S1_Cnt      S2_Cnt      S3_Cnt
-----------------------------------------------------------
Medical 300     850     500    2         2            1
在以下情况下使用简单案例:

SELECT
Payor,
S1 = SUM (case when Description = 'S1' THEN Amount ELSE 0 END),
S2 = SUM (case when Description = 'S2' THEN Amount ELSE 0 END),
S3 = SUM (case when Description = 'S3' THEN Amount ELSE 0 END),
S1_Cnt = COUNT (case when Description = 'S1' THEN ID ELSE NULL END),
S2_Cnt = COUNT (case when Description = 'S2' THEN ID ELSE NULL END),
S3_Cnt = COUNT (case when Description = 'S3' THEN ID ELSE NULL END)
FROM table
GROUP BY Payor
输出:

payor       S1      S2      S3
-------------------------------
Medical     2       2       1
Medical     300     850     500
Payor   S1      S2      S3  S1_Cnt      S2_Cnt      S3_Cnt
-----------------------------------------------------------
Medical 300     850     500    2         2            1
尝试使用UNION:

Select payor, S1, S2, S3
FROM
(
Select payor, Description, count(ID) CountVal, 1 AS Orderby from table
group by payor, Description) z
PIVOT
(
MAX(CountVal) FOR Description IN ([S1], [S2], [S3])
) x

UNION

Select payor, S1, S2, S3
FROM
(
Select payor, Description, SUM(Amount) SumVal, 0 AS Orderby from table
group by payor, Description) z
PIVOT
(
MAX(SumVal) FOR Description IN ([S1], [S2], [S3])
) x
输出:

payor       S1      S2      S3
-------------------------------
Medical     2       2       1
Medical     300     850     500
Payor   S1      S2      S3  S1_Cnt      S2_Cnt      S3_Cnt
-----------------------------------------------------------
Medical 300     850     500    2         2            1
在以下情况下使用简单案例:

SELECT
Payor,
S1 = SUM (case when Description = 'S1' THEN Amount ELSE 0 END),
S2 = SUM (case when Description = 'S2' THEN Amount ELSE 0 END),
S3 = SUM (case when Description = 'S3' THEN Amount ELSE 0 END),
S1_Cnt = COUNT (case when Description = 'S1' THEN ID ELSE NULL END),
S2_Cnt = COUNT (case when Description = 'S2' THEN ID ELSE NULL END),
S3_Cnt = COUNT (case when Description = 'S3' THEN ID ELSE NULL END)
FROM table
GROUP BY Payor
输出:

payor       S1      S2      S3
-------------------------------
Medical     2       2       1
Medical     300     850     500
Payor   S1      S2      S3  S1_Cnt      S2_Cnt      S3_Cnt
-----------------------------------------------------------
Medical 300     850     500    2         2            1

非常感谢,先生,我尝试过使用pivot union,执行时间增加了一倍,但案例陈述对我很有效。非常感谢,先生,我尝试过使用pivot union,执行时间增加了一倍,但案例陈述对我很有效。