Solved

Using alternative to LIMIT command and using INNER JOIN in MS SQL Query

Posted on 2006-07-06
9
1,852 Views
Last Modified: 2008-01-09
Hi,

I have the follwoing SQL:

SELECT     a.ContactID, a.CompanyID, a.ContactPrefix, a.ContactFName, a.ContactLName, a.ContactSurfix, a.ContactJobTitle, a.ContactPhone, a.ContactEmail,
                      b.CompanyName, b.CompanyPhone, b.CompanySegment, a.ContactMobile, a.ContactCustRelat, a.ContactAccntMng, a.ContactNotes,
                      b.CompanyFascimile, b.CompanyWeb, a.ContactRcdMng
FROM         Contact a INNER JOIN
                      Company b ON a.CompanyID = b.CompanyID
WHERE     (b.CompanySegment LIKE 'Finance') AND (a.ContactRemoved IS NULL) AND (b.CompanyRemoved IS NULL)
ORDER BY a.ContactID

This works fine but I also want to add a 'LIMIT' function.

I have the following code which performs the alternative to MySQL LIMIT command.

SELECT     contactid, contactlname
FROM         (SELECT     TOP 15 contactid, contactlname
                       FROM          (SELECT     TOP 45 contactid, contactlname
                                               FROM          contact
                                               ORDER BY contactid ASC) AS newtbl
                       ORDER BY contactid DESC) newtbl2
ORDER BY contactid

I need to use get the LIMIT capability of the second query into the first.

The reason for this is that I am using AJAX to call SQL lines on demand.

Any help would be really appreciated.

Thank you,

SC69

0
Comment
Question by:Stormchild69
  • 2
  • 2
  • 2
9 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 17052172
How about this (not optimized):

SELECT     a.ContactID, a.CompanyID, a.ContactPrefix, a.ContactFName, a.ContactLName, a.ContactSurfix, a.ContactJobTitle, a.ContactPhone, a.ContactEmail,
                      b.CompanyName, b.CompanyPhone, b.CompanySegment, a.ContactMobile, a.ContactCustRelat, a.ContactAccntMng, a.ContactNotes,
                      b.CompanyFascimile, b.CompanyWeb, a.ContactRcdMng
FROM         Contact a INNER JOIN
                      Company b ON a.CompanyID = b.CompanyID
WHERE     (b.CompanySegment LIKE 'Finance') AND (a.ContactRemoved IS NULL) AND (b.CompanyRemoved IS NULL)
AND a.ContactID IN
 (SELECT     TOP 15 contactid
                       FROM          (SELECT     TOP 45 contactid
                                               FROM          contact
                                               ORDER BY contactid ASC)
                       ORDER BY contactid DESC)
ORDER BY a.ContactID
0
 

Author Comment

by:Stormchild69
ID: 17053439
Hello,

Many thanks but I got an error on line 11 (second ORDER from the bottom). I added bach 'AS newtbl' and it ran but produced a mess of duplicated contactids. Contactid is unique in that there should only be one per contact and I would hope that contactid 30-45 would be displayed. This is what I tried

SELECT     a.ContactID, a.CompanyID, a.ContactPrefix, a.ContactFName, a.ContactLName, a.ContactSurfix, a.ContactJobTitle, a.ContactPhone, a.ContactEmail,
                      b.CompanyName, b.CompanyPhone, b.CompanySegment, a.ContactMobile, a.ContactCustRelat, a.ContactAccntMng, a.ContactNotes,
                      b.CompanyFascimile, b.CompanyWeb, a.ContactRcdMng
FROM         Contact a INNER JOIN
                      Company b ON a.CompanyID = b.CompanyID
WHERE     (b.CompanySegment LIKE 'Finance') AND (a.ContactRemoved IS NULL) AND (b.CompanyRemoved IS NULL) AND (a.ContactID IN
                          (SELECT     TOP 15 a.contactid
                            FROM          (SELECT     TOP 45 a.contactid
                                                    FROM          contact a
                                                    ORDER BY a.contactid ASC) AS newtbl
                            ORDER BY a.contactid DESC))
ORDER BY a.ContactID


Many thanks for trying,

SC69
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 500 total points
ID: 17053745
I think I see the problem.  I think reusing the "a" prefix in the subquery was messing it up.  Try

SELECT     a.ContactID, a.CompanyID, a.ContactPrefix, a.ContactFName, a.ContactLName, a.ContactSurfix, a.ContactJobTitle, a.ContactPhone, a.ContactEmail,
                      b.CompanyName, b.CompanyPhone, b.CompanySegment, a.ContactMobile, a.ContactCustRelat, a.ContactAccntMng, a.ContactNotes,
                      b.CompanyFascimile, b.CompanyWeb, a.ContactRcdMng
FROM         Contact a INNER JOIN
                      Company b ON a.CompanyID = b.CompanyID
WHERE     (b.CompanySegment LIKE 'Finance') AND (a.ContactRemoved IS NULL) AND (b.CompanyRemoved IS NULL) AND (a.ContactID IN
                          (SELECT     TOP 15 contactid
                            FROM          (SELECT     TOP 45 contactid
                                                    FROM          contact
                                                    ORDER BY contactid ASC)
                            ORDER BY contactid DESC))
ORDER BY a.ContactID
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17054771
Is this SQL Server 2005?
0
 

Author Comment

by:Stormchild69
ID: 17057666
Hi,

I am running SQL Server 2000.

Thank you for your help.

I added 'AS newtbl' and then your solution worked as required,

Thanks,

SC69
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17058257
While I appreciate the points, you appear to have awarded me in error.  See here:

I accepted the wrong answer. Now what?
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/help.jsp#hi17
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
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.

867 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

12 Experts available now in Live!

Get 1:1 Help Now