Results 1 to 15 of 15

Thread: [RESOLVED] Abnormal Unrolling

Hybrid View

  1. #1
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    3,560

    Re: [RESOLVED] Abnormal Unrolling

    Rawhide!
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,192

    Re: [RESOLVED] Abnormal Unrolling

    Here is my attempt

    Code:
    SELECT      SUM(t2.SomeCount)
    FROM        (
                SELECT      t2.FK
                            , 1                 AS SomeCount
                            , t2.OtherStuff
                            , t2.MoreStuff
                FROM        table2 t2
    
                UNION ALL
    
                SELECT      t1.PK
                            , MAX(t1.SomeCount) - COUNT(t2.PK) AS SomeCount
                            , NULL              AS OtherStuff
                            , NULL              AS MoreStuff
                FROM        table1 t1
                LEFT JOIN   table2 t2
                ON          t2.FK = t1.PK
                GROUP BY    t1.PK
                HAVING      MAX(t1.SomeCount) - COUNT(t2.PK) <> 0
                ) t2
    And here is my reasoning:
    - I'm adding a new column SomeCount to t2 with a constant value of 1.
    - I'm adding "missing" rows to t2 with enough SomeCount so that for each t2.FK there is enough SUM(t2.SomeCount) to match the total in t1.SomeCount. This is what the second SELECT after UNION ALL does.
    - On new rows the values of OtherStuff, MoreStuff, CoolStuff, etc. are NULLs
    - I'm using basic ANSI SQL-92 -- no CTEs, no windowing functions so this should work on MSSQL 2000, MS Access, dBase (probably:-))

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width