Trouble converting cursor to CTE

  • I'm trying to convert a cursor in a stored procedure (that updates a table variable) into a CTE that does this update.

    While the cursor produces the correct results, the CTE isn't... it seems to be updating each record in the temp table only once, instead of as necessary.

    I'm attaching code that demonstrates both methods. (The @tmpTbl variable has data that I'm using to get the results. In the proc, it's actually a select statement against several tables, but I'm keeping it simple here.)

    I'd really like to know what I doing wrong with this CTE / update.

    Thanks,

    Wayne

    SET NOCOUNT ON

    declare

    @PubCode char(8),

    @IssueNumber smallint,

    @LastIssue smallint,

    @PieceRate money,

    @Currency varchar(25),

    @NumLabels numeric(2),

    @PaymentLength char(1),

    @StartIssue smallint

    set @IssueNumber = 72

    declare @tmpDI TABLE (

    PubCode char(8),

    Issue smallint,

    DeferredCurrency varchar(25),

    PaymentLength char(1),

    DeferredAmount money DEFAULT (0),

    DeferredCount integer DEFAULT (0),

    RealizedAmount money DEFAULT (0),

    RealizedCount integer DEFAULT (0))

    declare @tmpTbl TABLE (

    SubscribID integer,

    PubCode varchar(8),

    LastIssue smallint,

    DeferredCurrency varchar(25),

    PaymentLength char(1),

    PieceRate numeric(10,8),

    NumLabels numeric(2,0),

    StartIssue smallint)

    insert into @tmpDI (PubCode, Issue, DeferredCurrency, PaymentLength)

    select 'MUSICTEC',72,'US DOLLARS','A' UNION

    select 'MUSICTEC',73,'US DOLLARS','A' UNION

    select 'MUSICTEC',74,'US DOLLARS','A' UNION

    select 'MUSICTEC',75,'US DOLLARS','A' UNION

    select 'MUSICTEC',76,'US DOLLARS','A' UNION

    select 'MUSICTEC',77,'US DOLLARS','A' UNION

    select 'MUSICTEC',78,'US DOLLARS','A' UNION

    select 'MUSICTEC',79,'US DOLLARS','A' UNION

    select 'MUSICTEC',80,'US DOLLARS','A' UNION

    select 'MUSICTEC',81,'US DOLLARS','A' UNION

    select 'MUSICTEC',82,'US DOLLARS','A' UNION

    select 'MUSICTEC',83,'US DOLLARS','A' UNION

    select 'MUSICTEC',84,'US DOLLARS','A' UNION

    select 'MUSICTEC',85,'US DOLLARS','A' UNION

    select 'MUSICTEC',86,'US DOLLARS','A' UNION

    select 'MUSICTEC',87,'US DOLLARS','A' UNION

    select 'MUSTECCN',72,'CANADIAN DOLLARS','A' UNION

    select 'MUSTECCN',73,'CANADIAN DOLLARS','A' UNION

    select 'MUSTECCN',74,'CANADIAN DOLLARS','A' UNION

    select 'MUSTECCN',75,'CANADIAN DOLLARS','A' UNION

    select 'MUSTECCN',76,'CANADIAN DOLLARS','A' UNION

    select 'MUSTECCN',77,'CANADIAN DOLLARS','A' UNION

    select 'MUSTECCN',78,'CANADIAN DOLLARS','A' UNION

    select 'MUSTECCN',79,'CANADIAN DOLLARS','A' UNION

    select 'MUSTECCN',80,'CANADIAN DOLLARS','A' UNION

    select 'MUSTECCN',81,'CANADIAN DOLLARS','A' UNION

    select 'MUSICTEC',72,'US DOLLARS','B' UNION

    select 'MUSICTEC',73,'US DOLLARS','B' UNION

    select 'MUSICTEC',74,'US DOLLARS','B' UNION

    select 'MUSICTEC',75,'US DOLLARS','B' UNION

    select 'MUSICTEC',76,'US DOLLARS','B' UNION

    select 'MUSICTEC',77,'US DOLLARS','B' UNION

    select 'MUSICTEC',78,'US DOLLARS','B' UNION

    select 'MUSICTEC',79,'US DOLLARS','B' UNION

    select 'MUSICTEC',80,'US DOLLARS','B' UNION

    select 'MUSICTEC',81,'US DOLLARS','B' UNION

    select 'MUSICTEC',82,'US DOLLARS','B' UNION

    select 'MUSICTEC',83,'US DOLLARS','B' UNION

    select 'MUSICTEC',84,'US DOLLARS','B' UNION

    select 'MUSICTEC',85,'US DOLLARS','B' UNION

    select 'MUSICTEC',86,'US DOLLARS','B' UNION

    select 'MUSICTEC',87,'US DOLLARS','B' UNION

    select 'MUSICTEC',88,'US DOLLARS','B' UNION

    select 'MUSICTEC',89,'US DOLLARS','B' UNION

    select 'MUSICTEC',90,'US DOLLARS','B' UNION

    select 'MUSICTEC',91,'US DOLLARS','B' UNION

    select 'MUSICTEC',92,'US DOLLARS','B' UNION

    select 'MUSICTEC',93,'US DOLLARS','B' UNION

    select 'MUSICTEC',94,'US DOLLARS','B' UNION

    select 'MUSICTEC',95,'US DOLLARS','B' UNION

    select 'MUSICTEC',96,'US DOLLARS','B' UNION

    select 'MUSICTEC',72,'US DOLLARS','Q' UNION

    select 'MUSICTEC',73,'US DOLLARS','Q' UNION

    select 'MUSICTEC',74,'US DOLLARS','Q' UNION

    select 'MUSICTEC',75,'US DOLLARS','Q'

    insert into @TmpTbl (PubCode, SubscribID, LastIssue, DeferredCurrency, PieceRate, NumLabels, StartIssue, PaymentLength)

    select 'MUSICTEC',211088,72,'US DOLLARS',13.9990000000000,1,63,'A' UNION

    select 'MUSICTEC',211089,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSICTEC',211090,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211091,72,'US DOLLARS',9.3326666666666,1,58,'A' UNION

    select 'MUSICTEC',211091,96,'US DOLLARS',9.1250000000000,1,73,'B' UNION

    select 'MUSICTEC',211092,78,'US DOLLARS',11.6658333333333,1,67,'A' UNION

    select 'MUSICTEC',211093,81,'US DOLLARS',9.2142857142857,1,68,'A' UNION

    select 'MUSICTEC',211094,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSICTEC',211095,95,'US DOLLARS',9.1662500000000,1,72,'B' UNION

    select 'MUSICTEC',211096,72,'US DOLLARS',12.7263636363636,1,62,'A' UNION

    select 'MUSTECCN',211098,73,'CANADIAN DOLLARS',12.2491666666666,1,62,'A' UNION

    select 'MUSICTEC',211099,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSTECCN',211100,74,'CANADIAN DOLLARS',11.3069230769230,1,62,'A' UNION

    select 'MUSICTEC',211101,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSTECCN',211103,73,'CANADIAN DOLLARS',13.1675000000000,1,62,'A' UNION

    select 'MUSICTEC',211104,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSICTEC',211106,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSICTEC',211107,74,'US DOLLARS',11.6658333333333,1,63,'A' UNION

    select 'MUSICTEC',211108,78,'US DOLLARS',11.6658333333333,1,67,'A' UNION

    select 'MUSTECCN',211109,73,'CANADIAN DOLLARS',12.2491666666666,1,62,'A' UNION

    select 'MUSICTEC',211110,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211111,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSTECCN',211113,73,'CANADIAN DOLLARS',12.2491666666666,1,62,'A' UNION

    select 'MUSICTEC',211114,78,'US DOLLARS',11.6658333333333,1,67,'A' UNION

    select 'MUSICTEC',211115,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSTECCN',211119,73,'CANADIAN DOLLARS',12.2491666666666,1,62,'A' UNION

    select 'MUSICTEC',211121,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSICTEC',211122,91,'US DOLLARS',9.1662500000000,1,68,'B' UNION

    select 'MUSICTEC',211123,74,'US DOLLARS',11.6658333333333,1,63,'A' UNION

    select 'MUSICTEC',211124,74,'US DOLLARS',11.6658333333333,1,63,'A' UNION

    select 'MUSICTEC',211126,78,'US DOLLARS',11.6658333333333,1,67,'A' UNION

    select 'MUSICTEC',211127,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSICTEC',211137,74,'US DOLLARS',11.6658333333333,1,63,'A' UNION

    select 'MUSICTEC',211138,78,'US DOLLARS',11.6658333333333,1,67,'A' UNION

    select 'MUSICTEC',211140,93,'US DOLLARS',8.4611538461538,1,68,'B' UNION

    select 'MUSTECCN',211142,73,'CANADIAN DOLLARS',13.1675000000000,1,62,'A' UNION

    select 'MUSTECCN',211143,78,'CANADIAN DOLLARS',12.2491666666666,1,67,'A' UNION

    select 'MUSICTEC',211146,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211147,95,'US DOLLARS',9.1662500000000,1,72,'B' UNION

    select 'MUSICTEC',211148,74,'US DOLLARS',11.6658333333333,1,63,'A' UNION

    select 'MUSICTEC',211149,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSICTEC',211150,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSICTEC',211151,74,'US DOLLARS',11.6658333333333,1,63,'A' UNION

    select 'MUSICTEC',211152,83,'US DOLLARS',11.6658333333333,1,72,'A' UNION

    select 'MUSICTEC',211156,74,'US DOLLARS',11.6658333333333,1,63,'A' UNION

    select 'MUSTECCN',211157,73,'CANADIAN DOLLARS',12.2491666666666,1,62,'A' UNION

    select 'MUSTECCN',211159,80,'CANADIAN DOLLARS',11.6658333333333,1,69,'A' UNION

    select 'MUSICTEC',211161,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211163,92,'US DOLLARS',9.1662500000000,1,69,'B' UNION

    select 'MUSTECCN',211166,77,'CANADIAN DOLLARS',12.2491666666666,1,66,'A' UNION

    select 'MUSICTEC',211167,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSICTEC',211203,74,'US DOLLARS',11.6658333333333,1,63,'A' UNION

    select 'MUSTECCN',211204,78,'CANADIAN DOLLARS',12.2491666666666,1,67,'A' UNION

    select 'MUSICTEC',211206,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211207,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSTECCN',211209,77,'CANADIAN DOLLARS',12.2491666666666,1,66,'A' UNION

    select 'MUSTECCN',211210,77,'CANADIAN DOLLARS',12.2491666666666,1,66,'A' UNION

    select 'MUSICTEC',211211,74,'US DOLLARS',11.6658333333333,1,63,'A' UNION

    select 'MUSICTEC',211222,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211224,81,'US DOLLARS',9.9166666666666,1,70,'A' UNION

    select 'MUSICTEC',211225,78,'US DOLLARS',11.6658333333333,1,67,'A' UNION

    select 'MUSICTEC',211226,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSTECCN',211227,78,'CANADIAN DOLLARS',11.6658333333333,1,67,'A' UNION

    select 'MUSICTEC',211228,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211230,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSTECCN',211232,77,'CANADIAN DOLLARS',12.2491666666666,1,66,'A' UNION

    select 'MUSTECCN',211234,73,'CANADIAN DOLLARS',12.2491666666666,1,62,'A' UNION

    select 'MUSICTEC',211235,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211236,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211237,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211238,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211239,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211241,77,'US DOLLARS',11.6658333333333,1,66,'A' UNION

    select 'MUSICTEC',211242,95,'US DOLLARS',9.1662500000000,1,72,'B' UNION

    select 'MUSICTEC',211244,75,'US DOLLARS',5.8316666666666,1,70,'Q' UNION

    select 'MUSICTEC',211244,87,'US DOLLARS',9.9166666666666,1,76,'A' UNION

    select 'MUSTECCN',211244,72,'CANADIAN DOLLARS',9.9758823529411,1,56,'A' UNION

    select 'MUSICTEC',211247,78,'US DOLLARS',11.6658333333333,1,67,'A' UNION

    select 'MUSICTEC',211248,73,'US DOLLARS',11.6658333333333,1,62,'A' UNION

    select 'MUSICTEC',211248,86,'US DOLLARS',10.7684615384615,1,74,'A' UNION

    select 'MUSICTEC',211249,82,'US DOLLARS',11.6658333333333,1,71,'A' UNION

    select 'MUSICTEC',211250,74,'US DOLLARS',11.6658333333333,1,63,'A' UNION

    select 'MUSICTEC',211253,78,'US DOLLARS',11.6658333333333,1,67,'A' UNION

    select 'MUSTECCN',211254,77,'CANADIAN DOLLARS',13.1675000000000,1,66,'A' UNION

    select 'MUSICTEC',211286,76,'US DOLLARS',9.9166666666666,1,65,'A' UNION

    select 'MUSICTEC',211299,83,'US DOLLARS',11.6658333333333,1,72,'A' UNION

    select 'MUSICTEC',211300,77,'US DOLLARS',10.7684615384615,1,65,'A' UNION

    select 'MUSICTEC',211302,84,'US DOLLARS',9.2142857142857,1,71,'A' UNION

    select 'MUSICTEC',211306,82,'US DOLLARS',11.6658333333333,1,71,'A' UNION

    select 'MUSICTEC',211308,82,'US DOLLARS',11.6658333333333,1,71,'A' UNION

    select 'MUSICTEC',211310,82,'US DOLLARS',11.6658333333333,1,71,'A' UNION

    select 'MUSICTEC',211315,82,'US DOLLARS',11.6658333333333,1,71,'A' UNION

    select 'MUSICTEC',211582,80,'US DOLLARS',11.6658333333333,1,69,'A' UNION

    select 'MUSICTEC',211819,79,'US DOLLARS',12.4166666666666,1,68,'A' UNION

    select 'MUSICTEC',211837,79,'US DOLLARS',12.4166666666666,1,68,'A' UNION

    select 'MUSICTEC',211884,79,'US DOLLARS',12.4166666666666,1,68,'A' UNION

    select 'MUSICTEC',211905,79,'US DOLLARS',12.4166666666666,1,68,'A' UNION

    select 'MUSICTEC',211964,79,'US DOLLARS',12.4166666666666,1,68,'A' UNION

    select 'MUSICTEC',212031,80,'US DOLLARS',12.4166666666666,1,69,'A' UNION

    select 'MUSICTEC',212361,81,'US DOLLARS',10.7684615384615,1,69,'A' UNION

    select 'MUSICTEC',212425,81,'US DOLLARS',11.4615384615384,1,69,'A' UNION

    select 'MUSICTEC',212535,81,'US DOLLARS',10.7684615384615,1,69,'A' UNION

    select 'MUSICTEC',212679,83,'US DOLLARS',9.7493333333333,1,69,'A' UNION

    select 'MUSICTEC',212742,81,'US DOLLARS',12.4166666666666,1,70,'A' UNION

    select 'MUSICTEC',212768,82,'US DOLLARS',10.7684615384615,1,70,'A' UNION

    select 'MUSICTEC',212893,81,'US DOLLARS',12.4166666666666,1,70,'A' UNION

    select 'MUSICTEC',212918,72,'US DOLLARS',11.6633333333333,1,70,'Q' UNION

    select 'MUSICTEC',212918,75,'US DOLLARS',11.6633333333333,1,73,'Q' UNION

    select 'MUSICTEC',212999,82,'US DOLLARS',10.7684615384615,1,70,'A' UNION

    select 'MUSICTEC',213290,82,'US DOLLARS',10.6923076923076,1,70,'A' UNION

    select 'MUSICTEC',213297,82,'US DOLLARS',10.7684615384615,1,70,'A' UNION

    select 'MUSICTEC',213340,82,'US DOLLARS',10.7684615384615,1,70,'A' UNION

    select 'MUSICTEC',213423,79,'US DOLLARS',11.1100000000000,1,71,'A' UNION

    select 'MUSICTEC',213478,79,'US DOLLARS',11.1100000000000,1,71,'A' UNION

    select 'MUSICTEC',213518,79,'US DOLLARS',11.1100000000000,1,71,'A' UNION

    select 'MUSICTEC',213592,73,'US DOLLARS',11.6633333333333,1,71,'Q' UNION

    select 'MUSICTEC',213630,83,'US DOLLARS',10.7684615384615,1,71,'A' UNION

    select 'MUSICTEC',213695,82,'US DOLLARS',12.4991666666666,1,71,'A' UNION

    select 'MUSICTEC',213764,73,'US DOLLARS',11.6633333333333,1,71,'Q' UNION

    select 'MUSICTEC',213907,83,'US DOLLARS',10.7684615384615,1,71,'A' UNION

    select 'MUSICTEC',213956,83,'US DOLLARS',10.7684615384615,1,71,'A' UNION

    select 'MUSICTEC',214336,86,'US DOLLARS',6.6660000000000,1,72,'A' UNION

    select 'MUSICTEC',214406,83,'US DOLLARS',9.9166666666666,1,72,'A' UNION

    select 'MUSICTEC',214453,83,'US DOLLARS',9.9166666666666,1,72,'A' UNION

    select 'MUSICTEC',214465,80,'US DOLLARS',11.1100000000000,1,72,'A' UNION

    select 'MUSICTEC',214675,75,'US DOLLARS',11.6633333333333,1,73,'Q' UNION

    select 'MUSICTEC',214677,83,'US DOLLARS',12.4991666666666,1,72,'A' UNION

    select 'MUSICTEC',214688,83,'US DOLLARS',9.9166666666666,1,72,'A' UNION

    select 'MUSICTEC',214775,74,'US DOLLARS',11.6633333333333,1,72,'Q' UNION

    select 'MUSICTEC',214915,83,'US DOLLARS',9.9166666666666,1,72,'A' UNION

    select 'MUSICTEC',215194,83,'US DOLLARS',9.9166666666666,1,72,'A' UNION

    select 'MUSICTEC',215217,83,'US DOLLARS',9.9166666666666,1,72,'A' UNION

    select 'MUSICTEC',215284,84,'US DOLLARS',10.7684615384615,1,72,'A' UNION

    select 'MUSICTEC',215286,83,'US DOLLARS',9.9166666666666,1,72,'A' UNION

    select 'MUSICTEC',215584,83,'US DOLLARS',9.9166666666666,1,72,'A' UNION

    select 'MUSICTEC',215639,83,'US DOLLARS',12.4991666666666,1,72,'A' UNION

    select 'MUSICTEC',215674,83,'US DOLLARS',9.9166666666666,1,72,'A' UNION

    select 'MUSICTEC',215739,95,'US DOLLARS',9.1662500000000,1,72,'B'

    -- cursor method

    declare ctmpDI cursor for

    select t.PubCode, t.LastIssue, t.DeferredCurrency, t.PaymentLength, t.PieceRate, t.NumLabels, t.StartIssue

    from @tmpTbl t

    order by t.PubCode, t.DeferredCurrency, t.LastIssue

    open ctmpDI

    fetch next from ctmpDI into @Pubcode, @LastIssue, @Currency, @PaymentLength, @PieceRate, @NumLabels, @StartIssue

    while @@fetch_status = 0 begin

    update @tmpDI

    set

    DeferredAmount = DeferredAmount + case when Issue > @IssueNumber then IsNull(@PieceRate,0) else 0 end,

    DeferredCount = DeferredCount + case when Issue > @IssueNumber then IsNull(@NumLabels,1) else 0 end,

    RealizedAmount = RealizedAmount + case when Issue <= @IssueNumber then IsNull(@PieceRate,0) else 0 end,

    RealizedCount = RealizedCount + case when Issue <= @IssueNumber then IsNull(@NumLabels,1) else 0 end

    where PubCode = @PubCode

    and DeferredCurrency = @Currency

    and Issue between @StartIssue and @LastIssue

    and PaymentLength = @PaymentLength

    fetch next from ctmpDI into @Pubcode, @LastIssue, @Currency, @PaymentLength, @PieceRate, @NumLabels, @StartIssue

    end

    close ctmpDi

    deallocate ctmpDI

    select * from @tmpDI

    order by PubCode, DeferredCurrency, PaymentLength, Issue

    update @tmpDI

    set DeferredAmount = 0,

    DeferredCount = 0,

    RealizedAmount = 0,

    RealizedCount = 0

    -- cte method

    ;with TempTbl (SubscribID, PubCode, LastIssue, DeferredCurrency, PaymentLength, PieceRate, NumLabels, StartIssue) AS

    (

    select t.SubscribID, t.PubCode, t.LastIssue, t.DeferredCurrency, t.PaymentLength, t.PieceRate, t.NumLabels, t.StartIssue

    from @tmpTbl t

    )

    update @tmpDI

    set

    DeferredAmount = DeferredAmount + case when Issue > @IssueNumber then IsNull(t2.PieceRate,0) else 0 end,

    DeferredCount = DeferredCount + case when Issue > @IssueNumber then IsNull(t2.NumLabels,1) else 0 end,

    RealizedAmount = RealizedAmount + case when Issue <= @IssueNumber then IsNull(t2.PieceRate,0) else 0 end,

    RealizedCount = RealizedCount + case when Issue <= @IssueNumber then IsNull(t2.NumLabels,1) else 0 end

    from @tmpDI t1

    JOIN TempTbl t2 ON

    t1.PubCode = t2.PubCode

    and t1.DeferredCurrency = t2.DeferredCurrency

    and t1.PaymentLength = t2.PaymentLength

    where t1.Issue between t2.StartIssue and t2.LastIssue

    select * from @tmpDI

    order by PubCode, DeferredCurrency, PaymentLength, Issue

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Why are you using CTE at all ?

    SELECT

    t1.PubCode,

    t1.Issue,

    t1.DeferredCurrency,

    t1.PaymentLength ,

    DeferredAmount = SUM (case when t1.Issue > @IssueNumber then IsNull(t2.PieceRate,0) else 0 end),

    DeferredCount = SUM( case when t1.Issue > @IssueNumber then IsNull(t2.NumLabels,1) else 0 end),

    RealizedAmount = SUM( case when t1.Issue <= @IssueNumber then IsNull(t2.PieceRate,0) else 0 end),

    RealizedCount = SUM( case when t1.Issue <= @IssueNumber then IsNull(t2.NumLabels,1) else 0 end)

    FROM @tmpDI t1

    JOIN @tmpTbl t2 ON

    t1.PubCode = t2.PubCode

    AND t1.DeferredCurrency = t2.DeferredCurrency

    AND t1.PaymentLength = t2.PaymentLength

    WHERE t1.Issue between t2.StartIssue and t2.LastIssue

    GROUP BY t1.PubCode,

    t1.Issue,

    t1.DeferredCurrency,

    t1.PaymentLength

    ORDER BY t1.PubCode, t1.DeferredCurrency, t1.PaymentLength, t1.Issue


    * Noel

  • noeld (2/3/2009)


    Why are you using CTE at all ?

    I had mentioned:

    The @tmpTbl variable has data that I'm using to get the results. In the proc, it's actually a select statement against several tables, but I'm keeping it simple here

    Well, that statement involves several tables and uses distinct. I had thought it would be easier to use a CTE. But, with what you supplied to me, I was able to get it working without a CTE.

    Thanks!

    🙂

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply