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_Sql Server_Loops_While Loop - Fatal编程技术网

SQL server,而循环不工作。只有第一排

SQL server,而循环不工作。只有第一排,sql,sql-server,loops,while-loop,Sql,Sql Server,Loops,While Loop,目前,我正在创建一个报告,按月列出每个地区的三角洲,这样我可以在最后创建趋势线,以指示三角洲的递减模式 这是我当前的代码,可以生成本周的增量。代码非常混乱,我现在仍在努力找到正确的号码 select t0.descript as 'Territory', t0.[Current Delta] as 'Current Delta', t0.[Current Sales] as 'Current Sales', t2.[Annual Goal] as 'Total Annual Goal' from

目前,我正在创建一个报告,按月列出每个地区的三角洲,这样我可以在最后创建趋势线,以指示三角洲的递减模式

这是我当前的代码,可以生成本周的增量。代码非常混乱,我现在仍在努力找到正确的号码

select t0.descript as 'Territory', t0.[Current Delta] as 'Current Delta', t0.[Current Sales] as 'Current Sales', t2.[Annual Goal] as 'Total Annual Goal'
from
    (select DeltaMainTable.descript,  sum(DeltaMainTable.Delta) as 'Current Delta', Sum(DeltaMaintable.[Yearly Sales]) as "Current Sales"
    from
    (
        select isnull(datepart(ww,T3.DocDate),0) as 'weeks'
        , t2.descript
        , t0.cardcode
        , sum(isnull(sum(t3.[total Sales]),0)) over (partition by t0.cardcode order by datepart(ww,T3.DocDate)) as 'Yearly Sales'
        , cast((case when min(t0.U_CG) - sum(isnull(sum(t3.[total Sales]),0)) over (partition by t0.cardcode order by datepart(ww,T3.DocDate)) > 0 then min(t0.U_CG) - sum(isnull(sum(t3.[total Sales]),0)) over (partition by t0.cardcode order by datepart(ww,T3.DocDate)) else 0 end) as int) as 'Delta'
        from 
        OCRD T0 
        left join oter T2 on T0.territory = T2.territryID
        left join 
--------INV + RIN------Credit + Debit Calculation In Between--------------------------------------INV + RIN------------------------------------
            (select SalesTable.name as 'name', SalesTable.docdate, sum(SalesTable.[total Sales]) as 'Total Sales'
            from (
                select T0.basecard as 'name', t0.docdate, sum(T0.linetotal) as 'total Sales'
                from hmltd.dbo.inv1 t0
                left join oitm t1 on t0.itemcode = t1.itemcode 
                where t1.U_BR = 'GPNA' and year(t0.docdate) = year(getdate()) 
                group by T0.basecard, T0.docDate
                union all
                select T0.basecard as 'name', t0.docdate, sum(t0.linetotal)*-1 as 'total Sales'
                from hmltd.dbo.rin1 t0
                left join oitm t1 on t0.itemcode = t1.itemcode 
                where t1.U_BR = 'GPNA' and year(t0.docdate) = year(getdate()) 
                group by T0.basecard, T0.docDate
            ) SalesTable 
            group by SalesTable.name, SalesTable.DocDate
            ) T3 on T0.cardcode = T3.name
--------INV + RIN------Credit + Debit Calculation In Between--------------------------------------INV + RIN------------------------------------
        where T0.U_CRR <> '' and t0.U_CR1 = 'R'
        group by t0.cardcode , t2.descript, datepart(ww, T3.DocDate)
    )DeltaMainTable
    inner join 
    (
        select
            t0.cardcode, ISNULL(max(datepart(ww,T3.DocDate)),0) as 'maxweeks'
        from 
        OCRD T0 
        left join 
--------INV + RIN------Credit + Debit Calculation In Between--------------------------------------INV + RIN------------------------------------
            (select SalesTable.name as 'name', SalesTable.docdate, sum(SalesTable.[total Sales]) as 'Total Sales'
            from (
                select T0.basecard as 'name', t0.docdate, sum(T0.linetotal) as 'total Sales'
                from hmltd.dbo.inv1 t0
                left join oitm t1 on t0.itemcode = t1.itemcode 
                where t1.U_BR = 'GPNA' and year(t0.docdate) = year(getdate()) 
                group by T0.basecard, T0.docDate
                union all
                select T0.basecard as 'name', t0.docdate, sum(t0.linetotal)*-1 as 'total Sales'
                from hmltd.dbo.rin1 t0
                left join oitm t1 on t0.itemcode = t1.itemcode 
                where t1.U_BR = 'GPNA' and year(t0.docdate) = year(getdate()) 
                group by T0.basecard, T0.docDate
                ) SalesTable 
            group by SalesTable.name, SalesTable.DocDate
            )T3 on T0.cardcode = T3.name
--------INV + RIN------Credit + Debit Calculation In Between--------------------------------------INV + RIN------------------------------------
        where T0.U_CRR <> '' and t0.U_CR1 = 'R'
        group by t0.cardcode
    )DeltaWeekTable on DeltaMainTable.cardcode = DeltaWeekTable.cardcode and DeltaMainTable.weeks = DeltaWeekTable.maxweeks
    group by DeltaMainTable.descript
    )t0
left join
(select t2.descript, sum(T0.U_CG) as 'Annual Goal', sum(T0.U_NG) as 'Next Level Annual Goal'
from ocrd t0
left join OTER t2 on T0.territory = T2.territryID
where   T0.U_CRR <> '' and t0.U_CR1 = 'R'
Group by T2.descript
) t2 on t0.descript = t2.descript
order by t0.descript
我想做的是为今年的每个星期创建一个Delta表。我试图做的是为这个添加一个while循环,但是它给我的结果与没有循环的结果相同,所以只有一行当前周的delta

我就是这样做的

declare @i int
declare @cnt int
set @i = 36 
set @cnt = datepart(ww, getdate())

while @i =< @cnt
begin

select 
-- Sales
    sum(case when T0.descript = 'AL' then T0.[Current Sales] else Null end) as 'AL Total'  
    , sum(case when T0.descript = 'BC' then T0.[Current Sales] else Null end) as 'BC Total'
    , sum(case when T0.descript = 'MS' then T0.[Current Sales] else Null end) as 'MS Total'  
    , sum(case when T0.descript = 'ONC' then T0.[Current Sales] else Null end) as 'ONC Total' 
    , sum(case when T0.descript = 'ONE' then T0.[Current Sales] else Null end) as 'ONE Total'
    , sum(case when T0.descript = 'ONF' then T0.[Current Sales] else Null end) as 'ONF Total'
    , sum(case when T0.descript = 'ONN' then T0.[Current Sales] else Null end) as 'ONN Total'
    , sum(case when T0.descript = 'ONS' then T0.[Current Sales] else Null end) as 'ONS Total'
    , sum(case when T0.descript = 'ONW' then T0.[Current Sales] else Null end) as 'ONW Total'
    , sum(case when T0.descript = 'QCE' then T0.[Current Sales] else Null end) as 'QCE Total'
    , sum(case when T0.descript = 'QCW' then T0.[Current Sales] else Null end) as 'QCW Total'
--Current Delta
    , sum(case when T0.descript = 'AL' then T0.[Current Delta] else Null end) as 'AL Current Delta'  
    , sum(case when T0.descript = 'BC' then T0.[Current Delta] else Null end) as 'BC Current Delta'
    , sum(case when T0.descript = 'MS' then T0.[Current Delta] else Null end) as 'MS Current Delta'  
    , sum(case when T0.descript = 'ONC' then T0.[Current Delta] else Null end) as 'ONC Current Delta' 
    , sum(case when T0.descript = 'ONE' then T0.[Current Delta] else Null end) as 'ONE Current Delta'
    , sum(case when T0.descript = 'ONF' then T0.[Current Delta] else Null end) as 'ONF Current Delta'
    , sum(case when T0.descript = 'ONN' then T0.[Current Delta] else Null end) as 'ONN Current Delta'
    , sum(case when T0.descript = 'ONS' then T0.[Current Delta] else Null end) as 'ONS Current Delta'
    , sum(case when T0.descript = 'ONW' then T0.[Current Delta] else Null end) as 'ONW Current Delta'
    , sum(case when T0.descript = 'QCE' then T0.[Current Delta] else Null end) as 'QCE Current Delta'
    , sum(case when T0.descript = 'QCW' then T0.[Current Delta] else Null end) as 'QCW Current Delta'
from
    (select DeltaMainTable.descript,  sum(DeltaMainTable.Delta) as 'Current Delta', Sum(DeltaMaintable.[Yearly Sales]) as "Current Sales"
    from
    (
        select isnull(datepart(ww,T3.DocDate),0) as 'weeks'
        , t2.descript
        , t0.cardcode
        , sum(isnull(sum(t3.[total Sales]),0)) over (partition by t0.cardcode order by datepart(ww,T3.DocDate)) as 'Yearly Sales'
        , cast((case when min(t0.U_CG) - sum(isnull(sum(t3.[total Sales]),0)) over (partition by t0.cardcode order by datepart(ww,T3.DocDate)) > 0 then min(t0.U_CG) - sum(isnull(sum(t3.[total Sales]),0)) over (partition by t0.cardcode order by datepart(ww,T3.DocDate)) else 0 end) as int) as 'Delta'
        from 
        OCRD T0 
        left join oter T2 on T0.territory = T2.territryID
        left join 
--------INV + RIN------Credit + Debit Calculation In Between--------------------------------------INV + RIN------------------------------------
            (select SalesTable.name as 'name', SalesTable.docdate, sum(SalesTable.[total Sales]) as 'Total Sales'
            from (
                select T0.basecard as 'name', t0.docdate, sum(T0.linetotal) as 'total Sales'
                from hmltd.dbo.inv1 t0
                left join oitm t1 on t0.itemcode = t1.itemcode 
                where t1.U_BR = 'GPNA' and year(t0.docdate) = year(getdate()) 
                group by T0.basecard, T0.docDate
                union all
                select T0.basecard as 'name', t0.docdate, sum(t0.linetotal)*-1 as 'total Sales'
                from hmltd.dbo.rin1 t0
                left join oitm t1 on t0.itemcode = t1.itemcode 
                where t1.U_BR = 'GPNA' and year(t0.docdate) = year(getdate()) 
                group by T0.basecard, T0.docDate
            ) SalesTable 
            group by SalesTable.name, SalesTable.DocDate
            ) T3 on T0.cardcode = T3.name
--------INV + RIN------Credit + Debit Calculation In Between--------------------------------------INV + RIN------------------------------------
        where T0.U_CRR <> '' and t0.U_CR1 = 'R'
        group by t0.cardcode , t2.descript, datepart(ww, T3.DocDate)
    )DeltaMainTable
    inner join 
    (
        select
            t0.cardcode, ISNULL(max(datepart(ww,T3.DocDate)),0) as 'maxweeks'
        from 
        OCRD T0 
        left join 
--------INV + RIN------Credit + Debit Calculation In Between--------------------------------------INV + RIN------------------------------------
            (select SalesTable.name as 'name', SalesTable.docdate, sum(SalesTable.[total Sales]) as 'Total Sales'
            from (
                select T0.basecard as 'name', t0.docdate, sum(T0.linetotal) as 'total Sales'
                from hmltd.dbo.inv1 t0
                left join oitm t1 on t0.itemcode = t1.itemcode 
                where t1.U_BR = 'GPNA' and year(t0.docdate) = year(getdate()) 
                group by T0.basecard, T0.docDate
                union all
                select T0.basecard as 'name', t0.docdate, sum(t0.linetotal)*-1 as 'total Sales'
                from hmltd.dbo.rin1 t0
                left join oitm t1 on t0.itemcode = t1.itemcode 
                where t1.U_BR = 'GPNA' and year(t0.docdate) = year(getdate()) 
                group by T0.basecard, T0.docDate
                ) SalesTable 
            group by SalesTable.name, SalesTable.DocDate
            )T3 on T0.cardcode = T3.name
--------INV + RIN------Credit + Debit Calculation In Between--------------------------------------INV + RIN------------------------------------
        where T0.U_CRR <> '' and t0.U_CR1 = 'R' and datepart(ww, T3.Docdate) =< @i
        group by t0.cardcode
    )DeltaWeekTable on DeltaMainTable.cardcode = DeltaWeekTable.cardcode and DeltaMainTable.weeks = DeltaWeekTable.maxweeks
    group by DeltaMainTable.descript
    )t0
left join
(select t2.descript, sum(T0.U_CG) as 'Annual Goal', sum(T0.U_NG) as 'Next Level Annual Goal'
from ocrd t0
left join OTER t2 on T0.territory = T2.territryID
where   T0.U_CRR <> '' and t0.U_CR1 = 'R'
Group by T2.descript
) t2 on t0.descript = t2.descript



set @i =  @i + 1
end;
一个地区在某一周的增量是通过将该地区的所有负增量相加来计算的。假设一个地区只有两个人。这个地区的第一个人买了40000个,但配额只有30000个,那么这个人的增量是0,其他人买了200个,他的配额是20000个,总增量是19800个。我们不考虑正三角洲。因此,计算特定时间范围内的每个增量时,采用配额减去每个地区每个客户当前的年销售额,最小增量为0。我在代码的第一段中所做的是通过获取所有客户文档日期的max()来查找本周的增量摘要,并计算他们在上一个文档日期(购买日期)之前的销售总额,然后找到他们的增量。所以基本上第一段计算了今天的增量总结,但我需要直到上周、2周前、3周前、4周前的增量


有没有关于如何在没有循环的情况下做到这一点的建议?

没有人会试图理解这样的混乱。但是循环是直截了当的

建议尝试将代码分成小块,以便易于调试

从以下内容开始:

declare @i int
declare @cnt int
set @i = 0 
set @cnt = 10

While @i < @cnt
begin
    SELECT @i;
    set @i = @i+1;
end;    
declare@i int
声明@cnt int
设置@i=0
设置@cnt=10
而@i<@cnt
开始
选择@i;
设置@i=@i+1;
结束;

显示数据库模式、示例数据、当前和预期输出。请阅读,这里是学习如何提高问题质量和获得更好答案的好地方。尝试在中创建一个示例为什么在这里使用循环?我想说,当您决定将此作为第一个循环时,它走错了方向。这里有很多代码需要查看,但是您是否尝试过按日期部分分组(周,YourDateColumn)?这里根本没有理由使用循环。如果代码缩进,我会更容易阅读。这比插入许多空行更有帮助。这段代码完全是一团糟。只需在问题中添加模式和一些测试数据,就可以获得更好的反馈。肯定有更好的方法来实现你想要做的事情。我指的不仅仅是循环。
Territory   AL Delta    BC Delta    MS Delta    ONC Delta   ONE Delta   ONF Delta   ONN Delta   ONS Delta   ONW Delta   QCE Delta   QCW Delta                                                   

Week43    120906    41047   50600   100494  4518    8043    46748   79545   46619   30000   174553
week42    120000    40000   50000   100000   4000    8000   40000   70000    46000   20000   170000
week41    3906    21047   1600    1494   518    43    6748    9545   6619    10000     14553
....
declare @i int
declare @cnt int
set @i = 0 
set @cnt = 10

While @i < @cnt
begin
    SELECT @i;
    set @i = @i+1;
end;