SQL Subqueries from ASPBy Bill Graziano on 9 August 2000 | Tags: Queries kieron writes "I have three tables. I wish to count the number of hits for each flyer and produce a record set that shows the owner details, flyer details and no of hits for each flyer . . ."
kieron writes "I have three tables. I wish to count the number of hits for each flyer and produce a record set that shows the owner details, flyer details and no of hits for each flyer.
This is how ms access has done it using two quries (the first referencing the second): SELECT owners.ownerid, flyers.flyerid, owners.name, flyers.flyername, flyers.active, flyers.enddate, flyers.url, [hits Query].[Count Of hits] hits query SELECT DISTINCTROW hits.flyerid, Count(*) AS [Count Of hits] Please can you tell how to combine these into one sql statment that can be executed from an asp page. Thanks for your time and help. kieron" We don't normally handle Access questions and after you see my SQL statement you'll understand why. I'm going to show you the answer in SQL Server syntax (mostly) and let you convert it to Access. A simple approach would be something like this: SELECT owners.ownerid, flyers.flyerid, Basically all I did was incorporate the subquery into the query. Your main query now joins just two tables. The subquery is actually part of the query itself. You can use fields from the main query in the where clause of the subquery. This should run fine once you convert the syntax to Access which unfortunately, I can't help you with. The fastest query would join all three tables and include a large group by clause. It might look something like this: SELECT owners.ownerid, flyers.flyerid, Again my syntax is some twisted combination of SQL Server and Access but hopefully you get the point. We are just joining all three tables and doing a GROUP BY. This is very similar to what you did in the second of your initial queries. I hope this gets you going in the right direction. Good luck fixing my syntax :)
|
- Advertisement - |