Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now