Solved

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

Posted on 2004-10-07
3
1,062 Views
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.

SELECT * FROM
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'
ORDER BY a.SUBNO DESC
0
Comment
Question by:vikingg97
3 Comments
 
LVL 26

Accepted Solution

by:
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
0
 
LVL 17

Assisted Solution

by:BillAn1
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)

SELECT * FROM
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  
ORDER BY a.SUBNO DESC
0
 
LVL 1

Author Comment

by:vikingg97
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!

vikingg97
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.

831 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