Wrox Home  
Search P2P Archive for: Go

  Return to Index  

sql_language thread: Rolling up data on multiple levels


Message #1 by David Cameron <dcameron@i...> on Wed, 9 May 2001 10:27:52 +1000
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C0D81E.E61A9376

I have been trying to get a sp up that can add up amounts that are
distributed over a few tables in one to many joins. There are 3 tables
involved. There is a table (table2) joined to the main table (table1) with a
one to many join. That table has another table (table3) joined to it by a
one to many join. table2 stores some int data and table 3 stores further int
data. I could create a SQL sp to get that sum of the data from table 2 for
each record in table1, but ran into problems trying to return the sum for
table3, ie one join further down the chain. I couldn't add in a SELECT
statement inside the SUM function. I could do all this in some iterative
procedure in ASP, but it would be more efficient to run it in a sp. 

The successful SELECT:
SELECT (SELECT Sum(intSum1) 
     FROM table2
     WHERE intJoin1ID = table1.Join1ID)
     AS intSum1
FROM table1

Failed SELECT:
SELECT (SELECT Sum(intSum1) 
     FROM table2
     WHERE intJoin1ID = table1.Join1ID)
     AS intSum1
     (SELECT Sum(
          SELECT Sum(intSum2)
          FROM table3
          WHERE intJoin2ID = table2.Join2ID)
      FROM table2
      WHERE intJoin1ID = table1.Join1ID)
FROM table1 

David Cameron
nOw.b2b
dcameron@i...


  Return to Index