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

Posted on 2004-10-07
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 26

Accepted Solution

Hilaire earned 1200 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 800 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

770 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