Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-09-2012, 03:50 AM
jillapass jillapass is offline SQL db query to exclude some entries Windows 7 32bit SQL db query to exclude some entries Office 2007
Novice
SQL db query to exclude some entries
 
Join Date: Dec 2011
Posts: 26
jillapass is on a distinguished road
Default SQL db query to exclude some entries

I have 2 tables, one for clients, and one for client bookings
I want to find all clients that have never made a booking.
How do I do a query that links these 2 tables, but only selects those where there is not an entry in the 2nd table.

Thanks
Reply With Quote
  #2  
Old 11-09-2012, 04:53 AM
Colin Legg's Avatar
Colin Legg Colin Legg is offline SQL db query to exclude some entries Windows 7 32bit SQL db query to exclude some entries Office 2010 32bit
Expert
 
Join Date: Jan 2011
Location: UK
Posts: 369
Colin Legg will become famous soon enough
Default

Here are couple of ways which should get you started.

Code:
 
--load some example data into 
--a couple of table variables
--to demonstrate the answer
DECLARE @TClients TABLE
    (
      [ClientID] [SMALLINT] ,
      [ClientName] [VARCHAR](30)
    )
DECLARE @TOrders TABLE
    (
      [OrderID] [SMALLINT] ,
      [ClientID] [SMALLINT]
    )
INSERT  INTO @TClients
        ( ClientID, ClientName )
VALUES  ( 1, 'Smith' ),
        ( 2, 'Jones' ),
        ( 3, 'JillaPass' ),
        ( 4, 'King' )
INSERT  INTO @TOrders
        ( OrderID, ClientID )
VALUES  ( 1, 1 ),
        ( 2, 1 ),
        ( 3, 2 ),
        ( 4, 1 )
/*
In this example JillaPass and King have never made an order.
Either of the below queries are a good starting point
*/
--query1
SELECT  c.ClientID ,
        c.ClientName
FROM    @TClients c
        LEFT OUTER JOIN @TOrders o ON o.ClientID = c.ClientID
WHERE   o.ClientID IS NULL
--query2
SELECT  c.ClientID ,
        c.ClientName
FROM    @TClients c
WHERE   NOT EXISTS ( SELECT NULL
                     FROM   @TOrders o
                     WHERE  c.ClientID = o.ClientID )
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Query Text in MS-Excel 2010 or Import Data & Query outside of Excel? mag Excel 0 10-18-2012 11:15 AM
SQL db query to exclude some entries Exclude phrase from email kiwiora Outlook 2 07-16-2012 03:03 PM
SQL db query to exclude some entries VBScript.RegExp: exclude a particular word tinfanide Excel Programming 2 06-07-2012 06:52 AM
Edit spell check dic to exclude words? franklekens Word 1 07-03-2010 09:57 AM
Text Box Query Meljord Word 3 12-15-2009 12:25 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:15 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft