SQL服务器while循环不工作。只获得第一行

时间:2017-10-23 20:36:03

标签: 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 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

这是当前的结果,本周的增量是第43周。

AL Delta    BC Delta    MS Delta    ONC Delta   ONE Delta   ONF Delta   ONN Delta   ONS Delta   ONW Delta   QCE Delta   QCW Delta                                                   
120906  41047   50600   100494  4518    8043    46748   79545   46619   30000   174553

我想要做的是为今年的每个星期创建一个增量表。我试图做的是为此添加一个while循环,但它给出了与没有循环的结果相同的结果,因此只有当前一周的一行的增量。

我就这样做了

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;

这是我第一次在sql中使用循环。有人能告诉我哪里错了吗?

对不起,我不好意思, 预期的产出将是

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
....

特定周的某个地区的差异是通过将该地区内所有客户的负增量相加来计算的。所以我们说一个地区只有2个人。该地区的第一个人买了40000,但配额只有30000,那个人的三角洲是0,其他人买了200,他的配额是20000,总和是19800.我们不考虑积极的增量。因此,计算特定时间范围内的每个增量值,将配额减去每个区域中每个客户的当前年销售额,最小值为0。我在第一段代码中所做的是通过获取所有客户的文档日期的max()来计算本周的增量摘要,并计算他们的运行总销售额,直到他们之前的文档日期(购买日期)并找到他们的三角洲。所以基本上第一段计算今天的Delta摘要,但我需要deltas直到上周,2周前,3周前,4周前........

有关如何在没有循环的情况下执行此操作的任何建议吗?

1 个答案:

答案 0 :(得分:0)

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

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

从这样的事情开始:

<强> SQL DEMO

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

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