|
-
Apr 19th, 2024, 03:50 AM
#1
Re: [RESOLVED] Abnormal Unrolling
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.
-
Apr 19th, 2024, 04:26 AM
#2
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>
Last edited by wqweto; Apr 19th, 2024 at 04:29 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|