转发麻烦,SQL Server

时间:2018-03-07 09:39:27

标签: sql sql-server

查询:

SELECT   
SUM(TotalDue), DATENAME(dw, OrderDate), COUNT(DISTINCT ProductID)  
FROM Sales.SalesOrderHeader headers  
INNER JOIN Sales.SalesOrderDetail details  
ON  headers.SalesOrderID = details.SalesOrderID  
GROUP BY DATENAME(dw, OrderDate);

我目前的结果是:

560644420,8959  Wednesday   262
432484099,6362  Thursday    260
478917953,3263  Saturday    251
212585108,7496  Friday  238
457072471,4514  Monday  241
381773345,5659  Sunday  259
403492724,4161  Tuesday 235

现在我想调整这些数据,所以我有几天作为列。 问题是我不知道如何实现这个结果 - 如何让第一行告诉它代表总和,其次是它计算不同的ID。

提前感谢任何建议和帮助

到目前为止我的尝试,也许这个查询会让你的回答更容易,因为你不必编写所有健壮的代码:

SELECT [Total], [Monday], [Tuesday]
FROM 
    (
        SELECT SUM(TotalDue)[Total], DATENAME(dw, OrderDate) [Day], COUNT(DISTINCT ProductID)[different products]
        FROM Sales.SalesOrderHeader headers
        INNER JOIN Sales.SalesOrderDetail details ON  headers.SalesOrderID = details.SalesOrderID
        GROUP BY DATENAME(dw, OrderDate)
) as source
PIVOT(
    COUNT([different products]) FOR [Day] IN ([Monday], [Tuesday])
) as pivoted;

我的预期结果(日期顺序并不重要):

                        Monday            Tuesday         Wednesday ...
Sums of orders          457072471,4514    403492724,4161  560644420,8959
Different items count   241               235             262

2 个答案:

答案 0 :(得分:4)

你可以结合两个数据透视语句来做这件事。

Select  Totals,Monday  ,Tuesday from (
SELECT  'TotalSum' As Totals, [Monday] as Monday, [Tuesday]
FROM 
    (
        SELECT sum(Totaldue) As TotalDue, DATENAME(dw, OrderDate) [Day] 
        FROM Sales.SalesOrderHeader headers
        INNER JOIN Sales.SalesOrderDetail details ON  headers.SalesOrderID = details.SalesOrderID
        group by  DATENAME(dw, OrderDate) 


) as source
PIVOT(
    sum(TotalDue) FOR [Day] IN ([Monday], [Tuesday])
    ) as pivoted

    union all

SELECT  'TotalCount' As Totals, [Monday] as Monday, [Tuesday]
FROM 
    (
        SELECT  DATENAME(dw, OrderDate) [Day], count(distinct ProductID) as DistinctProduct
        FROM Sales.SalesOrderHeader headers
        INNER JOIN Sales.SalesOrderDetail details ON  headers.SalesOrderID = details.SalesOrderID
        group by  DATENAME(dw, OrderDate) 


) as source
PIVOT(
    sum(DistinctProduct) FOR [Day] IN ([Monday], [Tuesday]) 
    )as pivoted
    )  x 

答案 1 :(得分:2)

你不能一次做多个支点,所以你必须做两次:

;WITH Data AS
(
    SELECT SUM(TotalDue)[Total], DATENAME(dw, OrderDate) [Day], COUNT(DISTINCT ProductID)[different products]
    FROM 
        Sales.SalesOrderHeader headers
        INNER JOIN Sales.SalesOrderDetail details ON  headers.SalesOrderID = details.SalesOrderID
    GROUP BY 
        DATENAME(dw, OrderDate)
),
PrePivot1 AS
(
    SELECT
        D.Day,
        D.[different products]
    FROM
        Data AS D
),
PrePivot2 AS
(
    SELECT
        D.Day,
        D.[Total]
    FROM
        Data AS D
)
SELECT
    Concept = 'Different items count',
    pivoted.Monday,
    pivoted.Tuesday,
    pivoted.Wednesday,
    pivoted.Thursday,
    pivoted.Friday,
    pivoted.Saturday,
    pivoted.Sunday
FROM
    PrePivot1 AS D
    PIVOT (
        SUM([different products]) FOR [Day] IN ([Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday], [Sunday])
    ) as pivoted

UNION ALL

SELECT
    Concept = 'Sums of orders',
    pivoted.Monday,
    pivoted.Tuesday,
    pivoted.Wednesday,
    pivoted.Thursday,
    pivoted.Friday,
    pivoted.Saturday,
    pivoted.Sunday
FROM
    PrePivot2 AS D
    PIVOT (
        SUM([Total]) FOR [Day] IN ([Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday], [Sunday])
    ) as pivoted;

请注意,我将PIVOT汇总功能从COUNT()更改为SUM(),因为您似乎正在添加产品ID。

我正在做2个“PrePivots”的原因是PIVOT操作由GROUP BY中未引用的所有列执行隐式PIVOT,因此其他列(如Total)对于第一个数据透视表)将被分组,并且对于每个不同的总值,您将有一行。