Forum Replies Created

Viewing 15 posts - 2,446 through 2,460 (of 4,085 total)

  • RE: selecting top rows by counting a variable total

    ChrisM@Work (8/18/2016)


    -- set up some data to code against

    DROP TABLE #MyTable

    SELECT *

    INTO #MyTable

    FROM (

    SELECT 123, 1 UNION ALL

    SELECT 456, 2 UNION ALL

    SELECT 789, 1 UNION ALL

    SELECT 789, 2 UNION ALL

    SELECT...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: selecting top rows by counting a variable total

    This non-recursive CTE works with pre-aggregated (and mixed) data.

    ;

    WITH running_total AS (

    SELECT *, SUM(mt.copies) OVER(ORDER BY mt.subscriberid , mt.copies ROWS UNBOUNDED PRECEDING) AS rt

    FROM #MyTable mt

    )

    SELECT rt.subscriberid,

    CASE

    WHEN rt.rt <= @num_records...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: selecting top rows by counting a variable total

    Are you aggregating your data in your query or is it pre-aggregated? That is, do you have two records for 456 and three records for 789, or do you...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: Re-ordering a Sequence

    DesNorton (8/17/2016)


    Unless my understanding is incorrect, I believe that the movement in this solution is the wrong way around.

    To prove this, let's take a look at only the first movement...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: Are the posted questions getting worse?

    I'm writing an article explaining my solution to Credits and Debits and am looking for two or three people to review it before I submit it. Any takers?

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: Re-ordering a Sequence

    I realized this morning that my code needed a slight update. My updates sample data had the same ordering for the from_seq and the to_seq, but that is not...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: Re-ordering a Sequence

    Here is a version that allows you to update multiple records at the same time.

    DECLARE @A TABLE (

    seqINT,

    orig_seq INT

    )

    INSERT @A( seq, orig_seq)

    VALUES(1, 1), (2, 2), (3, 3), (4, 4), (5,...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: Credits and Debits

    Code that you get on the Internet should never be considered a complete solution. It's impossible to cover every single scenario in a small sample of data. That's...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: What is difference between both query?

    John Mitchell-245523 (8/15/2016)


    I'm going to have to disagree there, I'm afraid. Suppose the query had originally been written with an outer join, with the filter (quite correctly) in the...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: Credits and Debits

    jssashank (8/15/2016)


    drew.allen (8/12/2016)


    Except for the fact that I was selecting the Transaction ID rather than the credit ID, my query produces the exact results that you say you are expecting....

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: What is difference between both query?

    John Mitchell-245523 (8/15/2016)


    There's no difference. But don't take my word for it - compare the execution plans and see for yourself.

    Now, if the join were an outer (instead of...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: Credits and Debits

    Except for the fact that I was selecting the Transaction ID rather than the credit ID, my query produces the exact results that you say you are expecting. Perhaps...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: Credits and Debits

    Okay. I think I figured out where the problem is. I also realized that I didn't need one of the clauses in one of the CASE statements, so...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: Credits and Debits

    jssashank (8/12/2016)


    drew.allen (8/11/2016)


    I found a faster solution (at least on this small dataset).

    ;

    WITH totals AS (

    SELECT *, ABS(Amount) AS Amt, SUM(ABS(Amount)) OVER(PARTITION BY Customer, TransactionType ORDER BY Date ROWS UNBOUNDED...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RE: Credits and Debits

    J Livingston SQL (8/12/2016)


    drew.allen (8/12/2016)


    This is actually an improved version of the test harness. I am putting a slight edge on credits, so that they should appear 60% of...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 15 posts - 2,446 through 2,460 (of 4,085 total)