Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Help Groups for Snitz Forums 2000 Users
 Help: General / Classic ASP versions(v3.4.XX)
 Combine two SQL queries
 New Topic  Topic Locked
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

Podge
Support Moderator

Ireland
3776 Posts

Posted - 20 April 2005 :  21:12:08  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
I need to get the total amount of topics and replies in a forum which I can do using the following two sql queries.

SELECT Count(T_Status) AS Expr1 FROM FORUM_TOPICS WHERE FORUM_TOPICS.CAT_ID=2 AND FORUM_TOPICS.FORUM_ID=5

SELECT Sum(T_Replies) AS Expr1 FROM FORUM_TOPICS WHERE FORUM_TOPICS.CAT_ID=2 AND FORUM_TOPICS.FORUM_ID=5

I add the results together and it returns the correct amount of total posts in the forum but I can help thinking there's a better way.

Anyone know how I can do it with one query?

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 20 April 2005 :  21:16:51  Show Profile  Send ruirib a Yahoo! Message
SELECT Count(T_Status) AS MyCount, Sum(T_Replies) as MySum FROM FORUM_TOPICS WHERE FORUM_TOPICS.CAT_ID=2 AND FORUM_TOPICS.FORUM_ID=5


Snitz 3.4 Readme | Like the support? Support Snitz too

Edited by - ruirib on 20 April 2005 21:17:09
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 20 April 2005 :  21:19:28  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
So simple.

SELECT Count(T_Status) AS Expr1, Sum(T_Replies) AS Expr2 FROM FORUM_TOPICS WHERE FORUM_TOPICS.CAT_ID=2 AND FORUM_TOPICS.FORUM_ID=5

Which returns the topic count as expr1 and reply count as expr2.

Now if I could get it to return just the total posts (i.e. topic count + reply count) as expr3 I would be happy.

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 20 April 2005 :  21:20:45  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Thanks Ruirib. I actually twigged it just after I posted. What about this?

"Now if I could get it to return just the total posts (i.e. topic count + reply count) as expr3 I would be happy."

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 21 April 2005 :  04:44:04  Show Profile  Send ruirib a Yahoo! Message
SELECT Count(T_Status) + Sum(T_Replies) as TotalPosts FROM FORUM_TOPICS WHERE FORUM_TOPICS.CAT_ID=2 AND FORUM_TOPICS.FORUM_ID=5


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 21 April 2005 :  07:42:05  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Excellent. Thanks ruirib.

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 21 April 2005 :  08:01:33  Show Profile  Send ruirib a Yahoo! Message
You're welcome.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 21 April 2005 :  09:12:03  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
I have a few more if your up to it.

I need a query which will return the author's name instead of id in a query.

SELECT FORUM_TOPICS.TOPIC_ID, FORUM_TOPICS.T_SUBJECT, FORUM_TOPICS.T_AUTHOR FROM FORUM_TOPICS

and I intend to use something like this to get the authors name.

SELECT FORUM_MEMBERS.MEMBER_ID, FORUM_MEMBERS.M_NAME, FORUM_MEMBERS.M_USERNAME FROM FORUM_MEMBERS WHERE FORUM_MEMBERS.MEMBER_ID=1

Is there any way to return the authors name in the first query?


Incidentally, which is the authors name ? M_NAME or M_USERNAME ?

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.

Edited by - Podge on 21 April 2005 09:12:41
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 21 April 2005 :  10:22:58  Show Profile  Send ruirib a Yahoo! Message
The authors name is M_NAME. You need to add a INNER JOIN to your first query, to the members table, joining T_AUTHOR with MEMBER_ID. Can you do it or do you need further help?


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 21 April 2005 :  11:20:10  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
I should've known that. I did all this in college last year but haven't put it to practical use.

I'll try a join and see how I get on.

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 25 April 2005 :  08:30:39  Show Profile
Or you could use:

SELECT T.TOPIC_ID,T.T_SUBJECT,T.T_AUTHOR,M.M_NAME FROM FORUM_TOPICS T,FORUM_MEMBERS M WHERE T.T_AUTHOR=M.M_NAME


Search is your friend
“I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 28 April 2005 :  11:33:38  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Thanks Shaggy & Ruirib. I'm nearly there with this query. What I have now is this

SELECT T.TOPIC_ID, R.TOPIC_ID, R.REPLY_ID, T.T_SUBJECT, T.T_MESSAGE, R.R_MESSAGE, T.T_AUTHOR, R.R_AUTHOR, R.R_DATE, T.T_DATE
FROM FORUM_TOPICS T LEFT JOIN FORUM_REPLY R ON T.TOPIC_ID = R.TOPIC_ID where T.FORUM_ID=2


I need to add M.M_NAME to the SELECT part (easy) but don't know where to add the FORUM_MEMBERS M e.g.

SELECT T.TOPIC_ID, R.TOPIC_ID, R.REPLY_ID, T.T_SUBJECT, T.T_MESSAGE, R.R_MESSAGE, T.T_AUTHOR, R.R_AUTHOR, R.R_DATE, T.T_DATE, M.M_NAME
FROM FORUM_MEMBERS M, FORUM_TOPICS T LEFT JOIN FORUM_REPLY R ON T.TOPIC_ID = R.TOPIC_ID where T.FORUM_ID=2

I think it may be possible with a nested select query or is there another way?

Also, is it possible to combine the two date columns R.R_DATE & T.T_DATE so that there is only one date column with the most recent date? If not, I can get around that programatically.

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 28 April 2005 :  13:37:09  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Actually, that query won't cut it either.

It might be easier if I explain exactly what I need.

A list of all individual posts in a forum given the forum id (e.g. topics and replies being treated equally as posts)
I need the following columns

Subject | Author Name | topic or reply date | Topic or Reply ID |

Subject should be the topic subject if the row is a topic and the original topic subject if a reply.

I have tried a left join but some topics & replies are left out e.g. it matches a topic and reply into one row.

The query would be like a union between these two queries run seperately (union doesn't work either - some replies get left out)

SELECT FORUM_TOPICS.T_SUBJECT, FORUM_TOPICS.T_AUTHOR, FORUM_TOPICS.T_DATE, FORUM_TOPICS.TOPIC_ID, FORUM_TOPICS.FORUM_ID
FROM FORUM_TOPICS
WHERE FORUM_TOPICS.FORUM_ID=1

SELECT FORUM_REPLY.R_DATE, FORUM_REPLY.TOPIC_ID, Null AS Expr1, FORUM_REPLY.R_MESSAGE, FORUM_REPLY.R_AUTHOR, FORUM_REPLY.FORUM_ID
FROM FORUM_REPLY
WHERE FORUM_REPLY.FORUM_ID=1

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 28 April 2005 :  14:27:17  Show Profile  Send ruirib a Yahoo! Message
You do need a UNION query:

SELECT FORUM_TOPICS.T_SUBJECT, FORUM_MEMBERS.M_NAME, FORUM_TOPICS.T_DATE, FORUM_TOPICS.TOPIC_ID
FROM FORUM_TOPICS, FORUM_MEMBERS
WHERE (FORUM_MEMBERS.MEMBER_ID=FORUM_TOPICS.T_AUTHOR) AND FORUM_TOPICS.FORUM_ID=1

UNION

SELECT FORUM_TOPICS.T_SUBJECT, FORUM_MEMBERS.M_NAME, FORUM_REPLY.R_DATE, FORUM_REPLY.REPLY_ID
FROM FORUM_REPLY, FORUM_MEMBERS, FORUM_TOPICS
WHERE (FORUM_MEMBERS.MEMBER_ID=FORUM_REPLY.R_AUTHOR) AND (FORUM_TOPICS.TOPIC_ID=FORUM_REPLY.TOPIC_ID) AND FORUM_TOPICS.FORUM_ID=1

Order By R_DATE;


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 28 April 2005 :  14:48:03  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Excellent. I had to make one change though -

Order By R_DATE; should be Order By T_DATE; (with Sql Server at least, not tested with Access)




Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 28 April 2005 :  15:37:35  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Thanks Ruirib for your help so far, I wouldn't be anywhere near completing this without you.

I changed the query slightly and it works in Access but not Sql Server

SELECT FORUM_TOPICS.T_SUBJECT, FORUM_MEMBERS.M_NAME, FORUM_TOPICS.T_DATE, (FORUM_TOPICS.TOPIC_ID & '_0') as ART FROM FORUM_TOPICS, FORUM_MEMBERS WHERE (FORUM_MEMBERS.MEMBER_ID=FORUM_TOPICS.T_AUTHOR) AND FORUM_TOPICS.FORUM_ID=1 UNION SELECT ('Re: ' + FORUM_TOPICS.T_SUBJECT), FORUM_MEMBERS.M_NAME, FORUM_REPLY.R_DATE, (FORUM_REPLY.TOPIC_ID & '_' & FORUM_REPLY.REPLY_ID) FROM FORUM_REPLY, FORUM_MEMBERS, FORUM_TOPICS WHERE (FORUM_MEMBERS.MEMBER_ID=FORUM_REPLY.R_AUTHOR) AND (FORUM_TOPICS.TOPIC_ID=FORUM_REPLY.TOPIC_ID) AND FORUM_TOPICS.FORUM_ID=1
ORDER BY T_DATE;

I added Re: to the subject for replies (no problem there)

In order to give each post (topic or reply) a unique id of their own I combined the topic id & "_" & reply id e.g. 1_14 would denote topic 1 reply 14. This works fine in access but not in Sql Server.

I can see three ways to solve this problem;

1. Give each row a running count which will act as a unique id BUT later in order to find the row the query would have to be run again (expensive cpu-wise)
2. Use casting to change the column to varchar for sql server. The benefit would be that when I need to retrieve the post I can split the 1-14 and retrieve the post easily and efficiently.
3. You have a better idea

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.4 seconds. Powered By: Snitz Forums 2000 Version 3.4.07