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
 General SQL Server Forums
 New to SQL Server Programming
 how to add count(trailer record) to end of report

Author  Topic 

cleeperman
Starting Member

1 Post

Posted - 2013-10-09 : 09:56:41
HI,

I want to export records from the customer table then the final record needs to be a count of all records exported.

I am always recieving an error - All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.


SELECT
CUSTTABLE.ACCOUNTNUM
,CUSTTABLE.NAME
,CUSTTABLE.BLOCKED
,CUSTTABLE.DATAAREAID
FROM
CUSTTABLE

where blocked!='2'

UNION

SELECT COUNT(ACCOUNTNUM) AS TRAILER FROM CUSTTABLE

WHERE blocked!='2';

Each different select statement works fine on its own but not when i combine them together with the union

Please help how do i do this?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-09 : 10:06:14
When you UNION the two queries, the two queries must return same number of columns. In your case it doesn't work because the two queries have different number and types of columns. You might simply keep them as two separate queries, run both queries, and combine both a tthe client side where you are presenting the data.

If you are using SSRS, it has features that will allow you to calculate the count and present in the report.
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2013-10-09 : 11:11:15
you can also do just one query that calculate the count results.

Please poste a sample of your output.

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2013-10-09 : 11:11:44
you can also do just one query that calculate the count results.

Please poste a sample of your output.

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page
   

- Advertisement -