Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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/5/bash/18.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_Sql Server 2008 - Fatal编程技术网

Sql server 获取SQL Server中两列的%差异

Sql server 获取SQL Server中两列的%差异,sql-server,sql-server-2008,Sql Server,Sql Server 2008,我正在根据给定的月份/年份为列选择计数。我需要返回与上个月相比的百分比差异 这是我到目前为止所做的。这将返回我需要的前半部分(当前月份计数) 因此,它的输出如下所示 Widget 1 - 200 sold Widget 2 - 190 sold 同样,我还想包括第三列,它捕获了上个月的%变化 另外,对于如何使用datePart进行选择,我也不是很清楚,因此如果您对如何更好地进行选择有什么建议,我将不胜感激 有很多方法可以产生你想要的结果。这一个使用一个CTE来计算两个月的总数,然后将其合并

我正在根据给定的月份/年份为列选择计数。我需要返回与上个月相比的百分比差异

这是我到目前为止所做的。这将返回我需要的前半部分(当前月份计数)

因此,它的输出如下所示

Widget 1  - 200 sold
Widget 2  - 190 sold
同样,我还想包括第三列,它捕获了上个月的%变化


另外,对于如何使用
datePart
进行选择,我也不是很清楚,因此如果您对如何更好地进行选择有什么建议,我将不胜感激

有很多方法可以产生你想要的结果。这一个使用一个CTE来计算两个月的总数,然后将其合并回自身

With cte as 
(
SELECT 

       o.productname, 
       Datepart(month, datetimereceived) m,
       Count(*) AS totalSales 
FROM   order o 
       INNER JOIN product p 
               ON o.productid = p.productid 
WHERE  Datepart(month, datetimereceived) in ( 2, 2-1) 
       AND Datepart(year, datetimereceived) = 2012 
GROUP  BY o.productname, 
       Datepart(month, datetimereceived) 
ORDER  BY totalsales DESC )
SELECT 
      a.productName ,
      a.totalSales,
      a.totalSales / b.totalSales percentage_of_prevous_month

FROM 
      cte a 
      INNER JOIN cte b 
      ON a.productname = b.productname
         and a.month = b.month - 1
少了一些碎片。如果除法的结果是一个小于0的数字,那么您可能实际上需要做一些事情,比如获得一个非零值

cast(a.totalSales as decimal)/ cast( b.totalSales  as decimal) 
另外,如果您的输入是一月,那么像这样使用月份输入也会导致问题。因此,最好将DateTimeReceived标准化为每月的第一天

e、 g

修改了选择和分组方式

   CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)-1),datetimerecieved),101) m
修改在哪里

   CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(datetimerecieved)-1),datetimerecieved),101)
                  in ( '2/1/2012', DateAdd(m,-1,'2/1/2012'))
修改

a.M > b.M

已编辑:未阅读有关百分比列的位。这将返回4列:productName、currentMonth、previousMonth和percentChange。ISNULL函数用于防止“被零除”错误。您可以将其调整到一个进程中,只需传入CurrentYear和CurrentMonth参数,或者传入一个datetime参数,让它从中计算出月份/年份,或者甚至使用GETDATE()而不传递任何参数

declare @CurrentYear int,
    @CurrentMonth int,
    @previousYear int,
    @previousMonth int

set @CurrentYear = 2012
set @CurrentMonth = 2

if @CurrentMonth = 1 begin
    set @previousMonth = 12
    set @previousYear = @CurrentYear - 1
end else begin
    set @previousMonth = @CurrentMonth - 1
    set @previousYear = @CurrentYear
end

select  productName,
    (select COUNT(*) 
    from    [order] o 
    where   DATEPART(year, dateTimeReceived) = @CurrentYear
    and DATEPART(month, dateTimeReceived) = @CurrentMonth
    and o.productid = p.productid) as currentMonth,
    (select COUNT(*) 
    from    [order] o 
    where   DATEPART(year, dateTimeReceived) = @previousYear
    and DATEPART(month, dateTimeReceived) = @previousMonth
    and o.productid = p.productid) as previousMonth,
    convert(decimal(10, 2), 100*(
    (select COUNT(*) 
    from    [order] o 
    where   DATEPART(year, dateTimeReceived) = @CurrentYear
    and DATEPART(month, dateTimeReceived) = @CurrentMonth
    and o.productid = p.productid) 
    -
    (select COUNT(*) 
    from    [order] o 
    where   DATEPART(year, dateTimeReceived) = @previousYear
    and DATEPART(month, dateTimeReceived) = @previousMonth
    and o.productid = p.productid)
    /
    NULLIF(
    (select COUNT(*) 
    from    [order] o 
    where   DATEPART(year, dateTimeReceived) = @previousYear
    and DATEPART(month, dateTimeReceived) = @previousMonth
    and o.productid = p.productid), 0))) as percentChange
from    product p

谢谢康拉德,如果月份是一月,我们该如何称呼年份?肖恩,谢谢!仅供参考,公式应转换为foo(十进制(10,2),100*(新旧)/旧)。你有一个paren在错误的地方,但是你是99.9%正确的,所以我把这个标记为答案。感谢您的帮助。NULLIF使用得很好,但是select子句的内联视图表现糟糕,在这种情况下完全没有必要。此外,这将包括所有产品,即使它们没有订购。不确定这对手术是好是坏。
declare @CurrentYear int,
    @CurrentMonth int,
    @previousYear int,
    @previousMonth int

set @CurrentYear = 2012
set @CurrentMonth = 2

if @CurrentMonth = 1 begin
    set @previousMonth = 12
    set @previousYear = @CurrentYear - 1
end else begin
    set @previousMonth = @CurrentMonth - 1
    set @previousYear = @CurrentYear
end

select  productName,
    (select COUNT(*) 
    from    [order] o 
    where   DATEPART(year, dateTimeReceived) = @CurrentYear
    and DATEPART(month, dateTimeReceived) = @CurrentMonth
    and o.productid = p.productid) as currentMonth,
    (select COUNT(*) 
    from    [order] o 
    where   DATEPART(year, dateTimeReceived) = @previousYear
    and DATEPART(month, dateTimeReceived) = @previousMonth
    and o.productid = p.productid) as previousMonth,
    convert(decimal(10, 2), 100*(
    (select COUNT(*) 
    from    [order] o 
    where   DATEPART(year, dateTimeReceived) = @CurrentYear
    and DATEPART(month, dateTimeReceived) = @CurrentMonth
    and o.productid = p.productid) 
    -
    (select COUNT(*) 
    from    [order] o 
    where   DATEPART(year, dateTimeReceived) = @previousYear
    and DATEPART(month, dateTimeReceived) = @previousMonth
    and o.productid = p.productid)
    /
    NULLIF(
    (select COUNT(*) 
    from    [order] o 
    where   DATEPART(year, dateTimeReceived) = @previousYear
    and DATEPART(month, dateTimeReceived) = @previousMonth
    and o.productid = p.productid), 0))) as percentChange
from    product p