|
-
Sep 20th, 2002, 10:56 AM
#1
Thread Starter
Hyperactive Member
(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!!!
-
Sep 20th, 2002, 11:31 AM
#2
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
-
Sep 25th, 2002, 07:16 AM
#3
Thread Starter
Hyperactive Member
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!!!
-
Sep 25th, 2002, 08:04 AM
#4
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 )
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...
-
Sep 25th, 2002, 08:22 AM
#5
Frenzied Member
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
-
Sep 25th, 2002, 08:40 AM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|