Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Incorrect Syntax When Doing UNION, Please Help

Author  Topic 

jglover
Starting Member

4 Posts

Posted - 2004-09-23 : 07:59:08
I am getting an incorrect syntax error near UNION when I run this. Each statement works individually
but not together. Anyone have any ideas?



rs.Open "SELECT (Col002),COUNT (Col008), SUM (Col004),SUM (Col005),SUM (Col006),
SUM (Col007),SUM (Col008),COUNT(Col010), COUNT (Col011), SUM (Col009), AVG (Col009) FROM SQL1
WHERE Col014 BETWEEN '" & datebox & "' AND '" & datebox2 & "' GROUP BY Col002 ORDER BY
Col002 UNION SELECT COUNT(Col002),
COUNT(Col008),SUM(Col004),SUM(Col005),SUM(Col006), SUM(Col007),SUM(Col008),COUNT(Col010),
COUNT(Col011), SUM(Col009), AVG(Col009) FROM SQL1 WHERE Col014 BETWEEN '" & datebox & "'
AND '" & datebox2 & "'" , objConn

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-09-23 : 08:06:17
try moving the order by...

rs.Open "SELECT (Col002),COUNT (Col008), SUM (Col004),SUM (Col005),SUM (Col006),
SUM (Col007),SUM (Col008),COUNT(Col010), COUNT (Col011), SUM (Col009), AVG (Col009) FROM SQL1
WHERE Col014 BETWEEN '" & datebox & "' AND '" & datebox2 & "' GROUP BY Col002 UNION SELECT COUNT(Col002),
COUNT(Col008),SUM(Col004),SUM(Col005),SUM(Col006), SUM(Col007),SUM(Col008),COUNT(Col010),
COUNT(Col011), SUM(Col009), AVG(Col009) FROM SQL1 WHERE Col014 BETWEEN '" & datebox & "'
AND '" & datebox2 & "' ORDER BY Col002" , objConn

Corey
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2004-09-23 : 08:09:22
remove the order by col002, also why do you need to unite almost exactly the same select? can you not have only 1, removing the union altogether?

Go to Top of Page

jglover
Starting Member

4 Posts

Posted - 2004-09-23 : 08:10:31
You are a genius, I knew it was something simple. I appreciate it very much.
Go to Top of Page

jglover
Starting Member

4 Posts

Posted - 2004-09-23 : 08:12:18
The reason being, the first select does the count for Col002 Where as the select after the UNION gives me the totals for those columns. It might not be the best route but it is now working :)
Go to Top of Page

samsekar
Constraint Violating Yak Guru

437 Posts

Posted - 2004-09-23 : 09:28:15
quote:
Originally posted by jglover

The reason being, the first select does the count for Col002 Where as the select after the UNION gives me the totals for those columns. It might not be the best route but it is now working :)


Look for ROLLUP / CUBE in BOL. HTH you to avoid this union

- Sekar
Go to Top of Page
   

- Advertisement -