combining two queries

1. Dan
Senior Member
Join Date
Dec 1969
Posts
1,364

## combining two queries

The first block here is the two queries I need to combine. The second block is my attempt at combining the two. This has given me problems, though. Can you tell me what&#039s wrong with the second block?<BR><BR><BR>oQuery = "SELECT S6REGN, S6SRP3, S6AN8 FROM DBIPRDOBJ.SAWKL625 WHERE S6REGN = &#039" &_<BR>Request.Form("reg") & "&#039 and S6SRP3 = &#039"& Request.Form("class") & "&#039 and S6AN8 = &#039"&_ <BR>Request.Form("cust") & "&#039"<BR>oQuery = "SELECT S6BLYR, SUM(S6P1\$)as SUMS6P1, SUM(S6P2\$)as SUMS6P2, SUM(S6P3\$)as SUMS6P3, SUM(S6P1U)as SUMS6U1, SUM(S6P2U)as SUMS6U2, SUM(S6P3U)as SUMS6U3 FROM DBIPRDOBJ.SAWKL625 WHERE S6BLYR BETWEEN 1998 AND 2000 GROUP BY S6BLYR ORDER BY S6BLYR" <BR><BR><BR>oQuery = "SELECT S6REGN, S6SRP3, S6AN8 S6BLYR, SUM(S6P1\$)as SUMS6P1, SUM(S6P2\$)as SUMS6P2, SUM(S6P3\$)as SUMS6P3, SUM(S6P1U)as SUMS6U1, SUM(S6P2U)as SUMS6U2, SUM(S6P3U)as SUMS6U3 FROM DBIPRDOBJ.SAWKL625 WHERE S6REGN = &#039" &_<BR>Request.Form("reg") & "&#039 and S6SRP3 = &#039"& Request.Form("class") & "&#039 and S6AN8 = &#039"&_ <BR>Request.Form("cust") & "&#039 and S6BLYR BETWEEN 1998 AND 2000 GROUP BY S6BLYR ORDER BY S6BLYR"<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
226

## RE: combining two queries

SUM is an what is called aggregate function - it groups the values in the table to give a result ( as well as count(*) )<BR>so if you want to combine selecting of fields values and results of aggregate functions - use "GROUP BY ..." syntaxys.<BR>NOTICE, that while groupong by some values will give you the right results, others may be bad for you.<BR>My adwise to you - study about GROUP BY, only then use it.<BR>It is very helpful, and is explained in many sites, books etc

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•