Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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_Ssms_Percentage - Fatal编程技术网

Sql server 每种类型客户的销售额百分比

Sql server 每种类型客户的销售额百分比,sql-server,ssms,percentage,Sql Server,Ssms,Percentage,我想找出每种客户类型每周的销售额百分比。我可以看到数学,但我不知道如何编写查询 SELECT c.customerType as 'Customer Type', DATEPART(WEEK, o.orderDate) as 'Week of the year', COUNT(c.customerType) as 'Number of sales' FROM [dbo].[Order] o JOIN Customer c ON c.id = o

我想找出每种客户类型每周的销售额百分比。我可以看到数学,但我不知道如何编写查询

SELECT 
    c.customerType as 'Customer Type', 
    DATEPART(WEEK, o.orderDate) as 'Week of the year', 
    COUNT(c.customerType)  as 'Number of sales' 
FROM
    [dbo].[Order] o
JOIN 
    Customer c ON c.id = o.customerId
GROUP BY 
    c.customerType, DATEPART(WEEK, o.orderDate)
此查询输出按客户类型分组的每个销售的计数

CustomerType  Week   Number of Sales
------------------------------------
Cash          36      248
Corporate     36       10
Personal      36        5
Cash          37      113
Corporate     37        3
Personal      37        2
Cash          38      136
Corporate     38        7
Personal      38        2
Cash          39      138
Corporate     39        4
Personal      39        3

您可以包装查询并使用窗口函数:

select 
    t.*,
    (100.0 * [Number of sales])
        /(sum([Number of sales]) over(partition by [Week of the year]) 
        [Percent of Total Sales]
from (
    select 
        c.customerType as [Customer Type], 
        datepart(week, o.orderDate) as [Week of the year], 
        count(c.customerType) as [Number of sales],     
    from [dbo].[Order] o
    join Customer c ON c.id = o.customerId
    group by c.customerType, datepart(week, o.orderDate), datepart(year, o.orderDate)
) t
注:

  • 在SQLServer中,最好使用括号而不是引号来定义标识符(引号通常保留给字符串)

  • 100.0
    中的
    .0
    非常重要:它强制SQLServer执行十进制除法(默认情况下,它将使用整数除法,这不是您想要的)

  • 我在集团定义中增加了年份;如果您的数据分布在几年内,您可能不希望将不同年份的同一周计算在一起

旁注:SQLServer在混合窗口函数和聚合方面非常灵活。因此,这也可能起作用:

select 
    c.customerType as [Customer Type], 
    datepart(week, o.orderDate) as [Week of the year], 
    count(c.customerType) as [Number of sales],     
    (100.0 * count(c.customerType))
        / (sum(count(c.customerType)) over(partition by datepart(week, o.orderDate)))
        as [Percent of Total Sales]
from [dbo].[Order] o
join Customer c ON c.id = o.customerId
group by c.customerType, datepart(week, o.orderDate), datepart(year, o.orderDate)

您可以包装查询并使用窗口函数:

select 
    t.*,
    (100.0 * [Number of sales])
        /(sum([Number of sales]) over(partition by [Week of the year]) 
        [Percent of Total Sales]
from (
    select 
        c.customerType as [Customer Type], 
        datepart(week, o.orderDate) as [Week of the year], 
        count(c.customerType) as [Number of sales],     
    from [dbo].[Order] o
    join Customer c ON c.id = o.customerId
    group by c.customerType, datepart(week, o.orderDate), datepart(year, o.orderDate)
) t
注:

  • 在SQLServer中,最好使用括号而不是引号来定义标识符(引号通常保留给字符串)

  • 100.0
    中的
    .0
    非常重要:它强制SQLServer执行十进制除法(默认情况下,它将使用整数除法,这不是您想要的)

  • 我在集团定义中增加了年份;如果您的数据分布在几年内,您可能不希望将不同年份的同一周计算在一起

旁注:SQLServer在混合窗口函数和聚合方面非常灵活。因此,这也可能起作用:

select 
    c.customerType as [Customer Type], 
    datepart(week, o.orderDate) as [Week of the year], 
    count(c.customerType) as [Number of sales],     
    (100.0 * count(c.customerType))
        / (sum(count(c.customerType)) over(partition by datepart(week, o.orderDate)))
        as [Percent of Total Sales]
from [dbo].[Order] o
join Customer c ON c.id = o.customerId
group by c.customerType, datepart(week, o.orderDate), datepart(year, o.orderDate)

您是想要每个客户类型的销售额占客户总销售额的百分比,还是本周总销售额的百分比?请向我们展示您的预期结果。相对于工作日的总销售额,您希望每个客户类型的销售额占客户总销售额的百分比,还是占本周总销售额的百分比?请给我们看看你的预期结果。与本周的总销售额相比