Any way I can do this? Should I do this?

J

Jeff Uchtman

I'd like to combine (if possible these two statements drawing from a total
of 3 tables.

sql = "SELECT p.Sub_ID, p.Cat_ID, p.Sub_Name, COUNT(c.Sub_ID) as ct FROM
Subs p LEFT JOIN Links c ON p.Sub_Id = c.Sub_Id GROUP BY p.Sub_ID, p.Cat_ID,
p.Sub_Name ORDER BY p.Sub_Name"

sql = "select * from Subs inner join Cats on Cats.Cat_ID=Subs.Cat_ID ORDER
BY Sub_Name"

Is it possible? Am I totaly screwed up for even trying? The goal is to
count the relations ships in the Subs and Links table based on Sub_ID, to
display needed data from the Subs table, and to display the Cat_name table
from the Cats table in relationship to the Cat_ID in both the Cats and Subs
Table.

Thanks
Jeff
 
B

Bob Barrows

Jeff said:
I'd like to combine (if possible these two statements drawing from a
total of 3 tables.

sql = "SELECT p.Sub_ID, p.Cat_ID, p.Sub_Name, COUNT(c.Sub_ID) as ct
FROM Subs p LEFT JOIN Links c ON p.Sub_Id = c.Sub_Id GROUP BY
p.Sub_ID, p.Cat_ID, p.Sub_Name ORDER BY p.Sub_Name"

sql = "select * from Subs inner join Cats on Cats.Cat_ID=Subs.Cat_ID
ORDER BY Sub_Name"

Is it possible? Am I totaly screwed up for even trying? The goal is
to count the relations ships in the Subs and Links table based on
Sub_ID, to display needed data from the Subs table, and to display
the Cat_name table from the Cats table in relationship to the Cat_ID
in both the Cats and Subs Table.
It's always easiest to use an example to get your idea across. Show us some
sample data in tabular format, and then show us the results you wish to
achieve, also using a tabular format (queries return data in tabular form,
so that really helps us understand what you want).

And don't forget to tell us what kind of database you are using, as well as
its version.

Bob Barrows
 
J

Jeff Uchtman

My bad, it was late and I was frustrated.

The DB is Access 2000. 3 tables named Cats, Subs, and Links. Tables in
Cats are Cat_Id, Cat_Name, and Cat_Date. Tables in Subs are Sub_Id, Cat_ID
(relationship to table Cats), Sub_Name, and Sub_Date. Tables in Links are
Link_ID, Cat_Id, Sub_Id (relationship to table Subs), Link_Name, Link_Disc,
Link_Url, and Link_Date. The main info I need to draw from the subs table
needing the Sub_Id, Cat_Id and Sub_Name. I need the relationship in Cats
table on Cat_Id pulling the Cat_Name, and I need the number of relationships
between the Subs table and Links table on the Sub_Id in each of those
tables. Hope this makes sense.

Thanks
Jeff
 
B

Bob Barrows

I'm still having trouble understanding. Please provide sample data in
tabular format:

table name
col1 col2 col3 ...
A 52 23 ...
B 78 62 ...


And show the results you wish to obtain from that sample data in the same
format:

results
col1 col2 col3 ...
row1
row2

Thx,
Bob Barrows
 
J

Jeff Uchtman

OK, here go's;

Cats
Cat_ID Cat_Name
1 Internet
2 PC
3 Sports

Subs
Sub_ID Cat_ID Sub_Name
2 1 Software
3 2 Microsoft
3 3 Football

Links
Link_ID Sub_ID Cat_ID Link_Desc
1 2 1 Netscape
2 3 2 Microsoft
3 3 3 NCAA

Table relationship between Cats Cat_ID and Subs Cat_ID. Table relationship
between Subs Sub_ID and Links Sub_ID.

Needing from Subs draw Sub_ID, Cat_ID, Sub_Name
Needing from Cats draw Cat_Name
Needing from Links draw count number of Link_ID that fall under each Sub_ID

Results

Cat_ID, Cat_Name, Sub_ID, Sub_Name, count of links ID under each Sub_ID.

Hope this makes sense.

Thanks
Jeff
 
J

Jeff Uchtman

Typo corrected below~

Cats
Cat_ID Cat_Name
1 Internet
2 PC
3 Sports

Subs
Sub_ID Cat_ID Sub_Name
2 1 Software
3 2 Microsoft
4 3 Football

Links
Link_ID Sub_ID Cat_ID Link_Desc
1 2 1 Netscape
2 3 2 Microsoft
3 4 3 NCAA

Table relationship between Cats Cat_ID and Subs Cat_ID. Table relationship
between Subs Sub_ID and Links Sub_ID.

Needing from Subs draw Sub_ID, Cat_ID, Sub_Name
Needing from Cats draw Cat_Name
Needing from Links draw count number of Link_ID that fall under each Sub_ID

Results

Cat_ID, Cat_Name, Sub_ID, Sub_Name, count of links ID under each Sub_ID.

Hope this makes sense.

Thanks
Jeff
 
B

Bob Barrows

Jeff said:
OK, here go's;

Cats
Cat_ID Cat_Name
1 Internet
2 PC
3 Sports

Subs
Sub_ID Cat_ID Sub_Name
2 1 Software
3 2 Microsoft
3 3 Football

Links
Link_ID Sub_ID Cat_ID Link_Desc
1 2 1 Netscape
2 3 2 Microsoft
3 3 3 NCAA

Table relationship between Cats Cat_ID and Subs Cat_ID. Table
relationship between Subs Sub_ID and Links Sub_ID.

Needing from Subs draw Sub_ID, Cat_ID, Sub_Name
Needing from Cats draw Cat_Name
Needing from Links draw count number of Link_ID that fall under each
Sub_ID

Results

Cat_ID, Cat_Name, Sub_ID, Sub_Name, count of links ID under each
Sub_ID.
So, using your sample data, the results you want would be as follows?

CatID Cat_Name Sub_ID Sub_Name LinksCount
1 Internet 2 Software 1
2 PC 3 Microsoft 1
3 Sports 3 Football 1

Select c.CatID,c.Cat_Name, s.Sub__ID, count(*) LinksCount
FROM Cats c Inner Join Subs s ON c.Cat_ID = s.Cat_ID
Inner Join Links l ON s.SubID = l.Sub_ID AND s.Cat_ID = l.Cat_ID
Group By c.CatID, s.Sub__ID

Something seems strange with this design, especially that Subs table: why
does the Sub_Name depend on both the Sub_ID AND the Cat_ID?
 
J

Jeff Uchtman

Subs name should not be dependent on anything (I think)
Relationship on Cats table and Subs table is between the Cats_ID.

Jeff

Jeff said:
OK, here go's;

Cats
Cat_ID Cat_Name
1 Internet
2 PC
3 Sports

Subs
Sub_ID Cat_ID Sub_Name
2 1 Software
3 2 Microsoft
3 3 Football

Links
Link_ID Sub_ID Cat_ID Link_Desc
1 2 1 Netscape
2 3 2 Microsoft
3 3 3 NCAA

Table relationship between Cats Cat_ID and Subs Cat_ID. Table
relationship between Subs Sub_ID and Links Sub_ID.

Needing from Subs draw Sub_ID, Cat_ID, Sub_Name
Needing from Cats draw Cat_Name
Needing from Links draw count number of Link_ID that fall under each
Sub_ID

Results

Cat_ID, Cat_Name, Sub_ID, Sub_Name, count of links ID under each
Sub_ID.
So, using your sample data, the results you want would be as follows?

CatID Cat_Name Sub_ID Sub_Name LinksCount
1 Internet 2 Software 1
2 PC 3 Microsoft 1
3 Sports 3 Football 1

Select c.CatID,c.Cat_Name, s.Sub__ID, count(*) LinksCount
FROM Cats c Inner Join Subs s ON c.Cat_ID = s.Cat_ID
Inner Join Links l ON s.SubID = l.Sub_ID AND s.Cat_ID = l.Cat_ID
Group By c.CatID, s.Sub__ID

Something seems strange with this design, especially that Subs table: why
does the Sub_Name depend on both the Sub_ID AND the Cat_ID?
 
B

Bob Barrows

Jeff said:
Subs name should not be dependent on anything (I think)
Relationship on Cats table and Subs table is between the Cats_ID.

So does my suggested query give you what you want?

Bob Barrows
 
J

Jeff Uchtman

Close, till getting this error:
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in
query expression 'count(*) LinksCount'.

Jeff

Jeff said:
Subs name should not be dependent on anything (I think)
Relationship on Cats table and Subs table is between the Cats_ID.

So does my suggested query give you what you want?

Bob Barrows
 
B

Bob Barrows

You should be testing this query in Access, using the query builder (switch
to SQL View), before you even think of trying to execute it from asp. You'll
probably get a better error message.
As a guess, either of the following changes should help:

Try this first:
count(*) As LinksCount

then, if that still causes an error:
count(l.*) As LinksCount

FWIW, you should be using the native Jet OLEDB provider in your asp
connection string, instead of the obsolete ODBC driver. See
www.connectionstrings.com for an example.

Bob Barrows


Jeff said:
Close, till getting this error:
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing
operator) in query expression 'count(*) LinksCount'.

Jeff

Jeff said:
Subs name should not be dependent on anything (I think)
Relationship on Cats table and Subs table is between the Cats_ID.

So does my suggested query give you what you want?

Bob Barrows
 
J

Jeff Uchtman

Thanks Bob. I guess I don't have enough knowledge on Access or ASP to do
this. I do get the same error in the page as query in Access. Tried your
suggestions and now get [Microsoft][ODBC Microsoft Access Driver] Extra ) in
query expression 'count(1*) LinksCount'. Will look at your suggestion on
the connection string.

Jeff
You should be testing this query in Access, using the query builder (switch
to SQL View), before you even think of trying to execute it from asp. You'll
probably get a better error message.
As a guess, either of the following changes should help:

Try this first:
count(*) As LinksCount

then, if that still causes an error:
count(l.*) As LinksCount

FWIW, you should be using the native Jet OLEDB provider in your asp
connection string, instead of the obsolete ODBC driver. See
www.connectionstrings.com for an example.

Bob Barrows


Jeff said:
Close, till getting this error:
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing
operator) in query expression 'count(*) LinksCount'.

Jeff

Jeff said:
Subs name should not be dependent on anything (I think)
Relationship on Cats table and Subs table is between the Cats_ID.

So does my suggested query give you what you want?

Bob Barrows
 
B

Bob Barrows

I forgot: Access (Jet) is very picky about multiple joins. there has to be
parentheses around each subjoin. The correct query looks like this:

Select c.Cat_ID,First(c.Cat_Name) AS [Cat_Name], s.Sub_ID,
count(*) AS LinkCount
FROM (Cats c INNER JOIN Subs s ON c.Cat_ID = s.Cat_ID)
INNER JOIN Links l ON
s.Cat_ID = l.Cat_ID AND s.Sub_ID = l.Sub_ID
Group By c.Cat_ID, s.Sub_ID

The best way to get these groupings right is to use the Access Query Builder
in Design View and use the GUI to create the joins. Switch to SQL View to
see the corresponding SQL statement.

HTH,
Bob Barrows


Jeff said:
Thanks Bob. I guess I don't have enough knowledge on Access or ASP
to do this. I do get the same error in the page as query in Access.
Tried your suggestions and now get [Microsoft][ODBC Microsoft Access
Driver] Extra ) in query expression 'count(1*) LinksCount'. Will
look at your suggestion on the connection string.
 
J

Jeff Uchtman

YAHOOOOOOOO That is EXACTLY what I want. I was just doing the same (like I
knew what I was doing) in query analyzer. Thanks your the asp session.
Where do I send the tuition check.

Jeff
I forgot: Access (Jet) is very picky about multiple joins. there has to be
parentheses around each subjoin. The correct query looks like this:

Select c.Cat_ID,First(c.Cat_Name) AS [Cat_Name], s.Sub_ID,
count(*) AS LinkCount
FROM (Cats c INNER JOIN Subs s ON c.Cat_ID = s.Cat_ID)
INNER JOIN Links l ON
s.Cat_ID = l.Cat_ID AND s.Sub_ID = l.Sub_ID
Group By c.Cat_ID, s.Sub_ID

The best way to get these groupings right is to use the Access Query Builder
in Design View and use the GUI to create the joins. Switch to SQL View to
see the corresponding SQL statement.

HTH,
Bob Barrows


Jeff said:
Thanks Bob. I guess I don't have enough knowledge on Access or ASP
to do this. I do get the same error in the page as query in Access.
Tried your suggestions and now get [Microsoft][ODBC Microsoft Access
Driver] Extra ) in query expression 'count(1*) LinksCount'. Will
look at your suggestion on the connection string.
 
J

Jeff Uchtman

OK Bob, you got me this far. I made a few changes in the Access query and
added the Sub_Name. Can I get this to display all fields even if this is no
present relationship, i.e all the sub_names and Id's? Her is what I
changed;

SELECT c.Cat_ID, First(c.Cat_Name) AS Cat_Name, s.Sub_ID, s.Sub_Name,
count(*) AS ct
FROM (Cats AS c INNER JOIN Subs AS s ON c.Cat_ID=s.Cat_ID) INNER JOIN Links
AS l ON (s.Sub_ID=l.Sub_ID) AND (s.Cat_ID=l.Cat_ID)
GROUP BY c.Cat_ID, s.Sub_ID, s.Sub_Name;

Thanks again
Jeff
YAHOOOOOOOO That is EXACTLY what I want. I was just doing the same (like I
knew what I was doing) in query analyzer. Thanks your the asp session.
Where do I send the tuition check.

Jeff
I forgot: Access (Jet) is very picky about multiple joins. there has to be
parentheses around each subjoin. The correct query looks like this:

Select c.Cat_ID,First(c.Cat_Name) AS [Cat_Name], s.Sub_ID,
count(*) AS LinkCount
FROM (Cats c INNER JOIN Subs s ON c.Cat_ID = s.Cat_ID)
INNER JOIN Links l ON
s.Cat_ID = l.Cat_ID AND s.Sub_ID = l.Sub_ID
Group By c.Cat_ID, s.Sub_ID

The best way to get these groupings right is to use the Access Query Builder
in Design View and use the GUI to create the joins. Switch to SQL View to
see the corresponding SQL statement.

HTH,
Bob Barrows


Jeff said:
Thanks Bob. I guess I don't have enough knowledge on Access or ASP
to do this. I do get the same error in the page as query in Access.
Tried your suggestions and now get [Microsoft][ODBC Microsoft Access
Driver] Extra ) in query expression 'count(1*) LinksCount'. Will
look at your suggestion on the connection string.
 
B

Bob Barrows

I'm confused. What does this query show you? What do you want it to show
you? Tabular format please.

Bob Barrows
 
J

Jeff Uchtman

It show only the subs that have a related link below them. If there is no
relating link they do not show up.
Jeff
 
J

Jeff Uchtman

Here is what I am seeing: I'd like to see all the SUB_ID and SUB_NAME in
this query with the assocaiation to the Cat_ID and CAT_NAME giving me the ct
for links even if 0.

Jeff


Description:


SQL Command:

SELECT
c.CAT_ID,
FIRST(c.CAT_NAME) AS
Cat_Name,
s.SUB_ID,
s.SUB_NAME,
COUNT(*) AS ct
FROM
(Cats c

INNER JOIN (Subs s
INNER JOIN Links l ON
s.CAT_ID = l.CAT_ID AND

s.SUB_ID = l.SUB_ID) ON
c.CAT_ID = s.CAT_ID)
GROUP BY
c.CAT_ID,

c.CAT_NAME,
s.SUB_ID,
s.SUB_NAME
QUERY: TEST \ New Query COUNT: 6 record(s)

CAT_ID Cat_Name SUB_ID SUB_NAME ct
1 Arts & Humanities 1 Literature 4
1 Arts & Humanities 2 Photography 6
2 Business & Economy 4 Shopping 1
4 Education 7 College & University 1
4 Education 8 K-12 1
8 News & Media 16 TV 1
10 Reference 20 Libraries 1
 
J

Jeff Uchtman

OK, very close. Getting count of 1 on relationship even if nothing is here.
Other counts are correct. The only problem is the count of 1 even if not
there. Here is what I have;

SELECT
c.CAT_ID,
FIRST(c.CAT_NAME) AS Cat_Name,
s.SUB_ID,
s.SUB_NAME,
COUNT(*) AS ct
FROM
(Cats c
INNER JOIN (Subs s
LEFT JOIN Links l ON
s.SUB_ID = l.SUB_ID AND
s.CAT_ID = l.CAT_ID) ON
c.CAT_ID = s.CAT_ID)
GROUP BY
c.CAT_ID,
c.CAT_NAME,
s.SUB_ID,
s.SUB_NAME
ORDER BY
s.SUB_NAME
 
J

Jeff Uchtman

GOT IT!! All is well. Changed COUNT(*) to COUNT(l.Cat_Id). Thanks Bob for
your input, help guidance, and for all around being a nice guy!

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top