Cant get a Count( * ) on INNER JOIN using OPENQUERY

Posted on 2004-10-07
Last Modified: 2008-01-09
I need to get the count(*) of an inner join statement using OPENQUERY using 2 linked queries.   Is this possible?  If so how?

Here is what I have, and keep coming up with an error.

OpenQuery(AS400, 'SELECT SUBNO, PERNO, cgrpid FROM hmodta.hmembp') as a INNER JOIN
OpenQuery(WEBSRVR2, 'SELECT Count(*), External_System_Key, User_ID FROM mcnet..user_header')  AS p ON a.SUBNO + a.PERNO = p.External_System_Key  
WHERE a.cgrpid = 'M00005'
Question by:vikingg97
LVL 26

Accepted Solution

Hilaire earned 300 total points
ID: 12249640
>>'SELECT Count(*), External_System_Key, User_ID FROM mcnet..user_header'<<
This is not a parsable SQL statement
When selecting count(*) along with other columns, you need a group by clause

SELECT Count(*), External_System_Key, User_ID FROM mcnet..user_header
group by External_System_Key, User_ID
LVL 17

Assisted Solution

BillAn1 earned 200 total points
ID: 12249965
also, you would be better off putting the WHERE clause inside the OPENQUERY -
as is, the AS400 database is going to return the whole table to SQLServer, which will then join these rows with the resolts from WEBSRVR2, and filter for a particular value of cgrpid. You will almost suredly get better performance if you have the AS400 db return only the records you want, (it may have an index on this column, and at the very minimum, it will reduce network traffic)

OpenQuery(AS400, 'SELECT SUBNO, PERNO, cgrpid FROM hmodta.hmembp WHERE cgrpid = ''M00005''') as a INNER JOIN
OpenQuery(WEBSRVR2, 'SELECT Count(*), External_System_Key, User_ID FROM mcnet..user_header GROUP BY External_System_Key, User_ID')  AS p ON a.SUBNO + a.PERNO = p.External_System_Key  

Author Comment

ID: 12250351
That is awesome Billan1.  It worked so much faster including that inside.  Thanks also Hilaire for initial answer.  I split the points giving more to Hillare since she originally answered it.  Billan1 though moved the query time from 20-30 sec to less than a second.

I am going to post another question related to this about how to include this into Classic ASP to return the record set, so if you guys know feel free to answer.  

Thanks again!


Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Live Storage Vmotion VMs with shared VMDK 10 58
Sql server function help 15 31
transaction in, sql server 6 33
T-SQL: New to using transactions 9 29
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question