Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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两列数据透视表_Sql Server_Tsql_Pivot - Fatal编程技术网

Sql server SQL Server两列数据透视表

Sql server SQL Server两列数据透视表,sql-server,tsql,pivot,Sql Server,Tsql,Pivot,我正在尝试创建一个PIVOT TSQL statment,它按日期和州/省汇总产品,并提供平均运输时间。以下是我到目前为止的情况: select * from (select createdate [Date Processed], stateprovince as [Province], count(*) as [Total], avg(datediff(day,createdate,t.e

我正在尝试创建一个PIVOT TSQL statment,它按日期和州/省汇总产品,并提供平均运输时间。以下是我到目前为止的情况:

select *
from    (select createdate [Date Processed], 
                stateprovince as [Province],
                count(*) as [Total],
                avg(datediff(day,createdate,t.eventdate)) as [AVG Delivery],
                product

            from recipient C left outer join 
            (select delivid, product, eventdesc, eventdate, eventcode
                from deliverystatus 
                where delivid in (select max(deliv_id) 
                                        from deliverystatus 
                                        where eventcode = 'DELIVERED' 
                                        group by product)) as t  ON c.product = t.product
            where account = 3519 and consol <>'' and trknum <> '' and C.createdate between '2/4/2016' and '2/4/2016'
            group by C.createdate, c.stateprovince, c.product
        ) as Q 
pivot   (
            count(product)
            for [Province] in (NY, IL, GA)
        ) as PVT
我需要的结果是:

Date Processed          Total   AVG Transit NY  IL  GA
2016-02-04 00:00:00.000 8       11.5        2   4   2
最终目标是按州/省列出平均运输量,如下所示:

Date Processed          Total   Total AVG   NY AVG  IL  AVG   GA AVG
2016-02-04 00:00:00.000 8       11.5        2   8   4   11    2  15
提前感谢。

您需要在透视后添加GROUP BY子句,并为每个输出列获取平均和或最大值:

select [Date Processed], SUM(NY+IL+GA) AS [Total], AVG([AVG Delivery]) AS [AVG Delivery], SUM(NY) AS NY, SUM(IL) AS IL, SUM(GA) AS GA 
from    (select createdate [Date Processed], 
                stateprovince as [Province],
                count(*) as [Total],
                avg(datediff(day,createdate,t.eventdate)) as [AVG Delivery],
                product

            from recipient C left outer join 
            (select delivid, product, eventdesc, eventdate, eventcode
                from deliverystatus 
                where delivid in (select max(deliv_id) 
                                        from deliverystatus 
                                        where eventcode = 'DELIVERED' 
                                        group by product)) as t  ON c.product = t.product
            where account = 3519 and consol <>'' and trknum <> '' and C.createdate between '2/4/2016' and '2/4/2016'
            group by C.createdate, c.stateprovince, c.product
        ) as Q 
pivot   (
            count(product)
            for [Province] in (NY, IL, GA)
        ) as PVT
group by [Date Processed]

如果不按c.product进行分组,会发生什么情况?这给了我正确的分组,但计数为off。4次而不是8次感谢让我到达了终点线,我不得不做了一些更正。为了得到正确的总数,我必须对所有州/省进行求和,并且我还必须将每个州的最大值改为求和。现在一切看起来都很好。
select [Date Processed], SUM(NY+IL+GA) AS [Total], AVG([AVG Delivery]) AS [AVG Delivery], SUM(NY) AS NY, SUM(IL) AS IL, SUM(GA) AS GA 
from    (select createdate [Date Processed], 
                stateprovince as [Province],
                count(*) as [Total],
                avg(datediff(day,createdate,t.eventdate)) as [AVG Delivery],
                product

            from recipient C left outer join 
            (select delivid, product, eventdesc, eventdate, eventcode
                from deliverystatus 
                where delivid in (select max(deliv_id) 
                                        from deliverystatus 
                                        where eventcode = 'DELIVERED' 
                                        group by product)) as t  ON c.product = t.product
            where account = 3519 and consol <>'' and trknum <> '' and C.createdate between '2/4/2016' and '2/4/2016'
            group by C.createdate, c.stateprovince, c.product
        ) as Q 
pivot   (
            count(product)
            for [Province] in (NY, IL, GA)
        ) as PVT
group by [Date Processed]