SQL Subqueries from ASP

By Bill Graziano on 9 August 2000 | 0 Comments | 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]
FROM owners INNER JOIN (flyers INNER JOIN [hits Query] ON flyers.flyerid = [hits Query].flyerid) ON owners.ownerid = flyers.ownerid
WHERE (((owners.ownerid)=1));


hits query

SELECT DISTINCTROW hits.flyerid, Count(*) AS [Count Of hits]
FROM hits
GROUP BY hits.flyerid;


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,
  owners.name, flyers.flyername, flyers.active,
  flyers.enddate, flyers.url,
  Count_of_hits = (Select Count(*) from hits where hits.flyerid = flyers.flyerid)
FROM owners
INNER JOIN flyers ON owners.ownerid = flyers.ownerid
WHERE owners.ownerid=1;


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,
owners.name, flyers.flyername, flyers.active,
flyers.enddate, flyers.url,
Count(*) as Hits
FROM owners
INNER JOIN flyers ON owners.ownerid = flyers.ownerid
Inner Join Hits On flyers.flyerID = hits.flyerid
WHERE owners.ownerid=1
Group by owners.ownerid, flyers.flyerid,
owners.name, flyers.flyername, flyers.active,
flyers.enddate, flyers.url
;


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 :)

Discuss this article: 0 Comments so far. Print this Article.

If you like this article you can sign up for our weekly newsletter. There's an opt-out link at the bottom of each newsletter so it's easy to unsubscribe at any time.

Email Address:

Related Articles

Using Dynamic SQL in Stored Procedures (7 March 2011)

Joining to the Next Sequential Row (2 April 2008)

Writing Outer Joins in T-SQL (11 February 2008)

Aggregating Correlated Sub-Queries (23 October 2007)

How to Use GROUP BY with Distinct Aggregates and Derived tables (31 July 2007)

How to Use GROUP BY in SQL Server (30 July 2007)

Returning Complex Data from User-Defined Functions with CROSS APPLY (11 June 2007)

Returning a week number for any given date and starting fiscal month (2 May 2007)

Other Recent Forum Posts

SSRS export multiple pdf file (2 Replies)

need help with summing multiple rows in columns (22 Replies)

Sum old data and new data in Update Query (0 Replies)

vb.net import csv to sql, how to handle empty cell (1 Reply)

SQL to XML grouping (2 Replies)

Msg 4104 (2 Replies)

error (0 Replies)

Msg 4104, Level 16, State 1, Line 1 The multi-part (5 Replies)

Subscribe to SQLTeam.com

Weekly SQL Server newsletter with articles, forum posts, and blog posts via email. Subscribers receive our white paper with performance tips for developers.

SQLTeam.com Articles via RSS

SQLTeam.com Weblog via RSS

- Advertisement -