Results 1 to 6 of 6

Thread: (RESOLVED) Nested SELECT statements

  1. #1

    Thread Starter
    Hyperactive Member -=XQ=-'s Avatar
    Join Date
    Mar 2002
    Location
    Liverpool, England, UK
    Posts
    278

    (RESOLVED) Nested SELECT statements

    Does anybody know if it is possible to share data between nested SQL statements. I.E.

    Code:
    SELECT SomeFields...,(SELECT SUM(OtherField) FROM table WHERE OtherFieldForiegnKey=SomeFieldPrimaryKey) FROM OTHERTable
    So from the above OtherFieldID would match the ID of the record from the first table. I don't seem to be able to figure it out. Can anybody help?

    Thanks,

    -=XQ=-
    Last edited by -=XQ=-; Sep 25th, 2002 at 08:41 AM.
    See ya later,

    -=XQ=-

    "Reality is merely an illusion, albeit a very persistent one. "
    - Albert Einstein (1879-1955)
    This is the coolest site ever!!!

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Nested SELECT statements

    Originally posted by -=XQ=-
    Does anybody know if it is possible to share data between nested SQL statements. I.E.

    Code:
    SELECT SomeFields...,(SELECT SUM(OtherField) FROM table WHERE OtherFieldForiegnKey=SomeFieldPrimaryKey) FROM OTHERTable
    So from the above OtherFieldID would match the ID of the record from the first table. I don't seem to be able to figure it out. Can anybody help?

    Thanks,

    -=XQ=-
    you want to use a join statement for this

    something like
    Code:
    Select table1.field1, table1.field2, table2.field1 from table1 inner join table2 on table1.UniqueID = table2.UniqueID
    not sure if that syntax is 100% but its close...

    what you shoudl do is build the query in ACCESS (thats what you are using right?) and then look at the code that it generates... you could then use that sql syntax in your VB app

  3. #3

    Thread Starter
    Hyperactive Member -=XQ=-'s Avatar
    Join Date
    Mar 2002
    Location
    Liverpool, England, UK
    Posts
    278
    I cannot do it this way due to the way the tables hold the information. Inner joins will duplicate totals values from the primary table. I probably should have wrote the example nearer to what it should be which is:

    Code:
    SELECT SUM(tbl1.fld1) AS SomeTotal, (SELECT COUNT(tbl2.fld2) 
    FROM tbl2 WHERE tbl2.ForiegnKey=tbl1.PrimaryKey) AS SomeOtherTotal FROM tbl1 
    WHERE .... GROUP BY tbl1.OtherField
    The problem only occurs when I add the GROUP BY. It starts complaining that the tbl1.PrimaryKey is not an aggregate function. Even if I enclose it in MAX() it complains that it should not be used.

    Any ideas?
    See ya later,

    -=XQ=-

    "Reality is merely an illusion, albeit a very persistent one. "
    - Albert Einstein (1879-1955)
    This is the coolest site ever!!!

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    You can only group by a field if you return it..?

    At least I think so - try adding that field to the Select fields bit and see if it runs.


    Vince
    (getting brain dead )

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    You have to add each field you select in the group by clause
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  6. #6

    Thread Starter
    Hyperactive Member -=XQ=-'s Avatar
    Join Date
    Mar 2002
    Location
    Liverpool, England, UK
    Posts
    278
    Thanks for all your help i think I have sorted it
    See ya later,

    -=XQ=-

    "Reality is merely an illusion, albeit a very persistent one. "
    - Albert Einstein (1879-1955)
    This is the coolest site ever!!!

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