Solved

Retrieve records that are not in specified timeframe

Posted on 2012-03-15
6
189 Views
Last Modified: 2012-03-20
Hello, Experts!

I am still a newbie in SQL Server and I need your help.  I have a table with the following structure:

Container_no, nvarchar(12)
Line, nchar(10)
Activity_Date, datetime
Activity, nvarchar(50)

Each container could have 50 or more activity records with different dates.  I need to be able to retrieve the container numbers that do not have activity between the dates 01/01/2011 and 12/31/2011.  I tried the following script but doesn't work:

SELECT DISTINCT Container_no
  FROM Reefer_Movements
  WHERE NOT EXISTS (SELECT Container_no, Activity_Date
  FROM Reefer_Movements
  WHERE Activity_Date BETWEEN '01/01/2011' AND '12/31/2011')
  ORDER BY Container_no

Any help would be very much appreciated.

Thanks!
0
Comment
Question by:Rick0124
6 Comments
 
LVL 9

Expert Comment

by:armchair_scouse
ID: 37726083
How about

SELECT DISTINCT Container_no
FROM Reefer_Movements
WHERE Activity Date <  '01/01/2011' AND Activity Date > '12/31/2011'
ORDER BY Container_no
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37726089
try

SELECT DISTINCT container_no
FROM   reefer_movements
WHERE  activity_date NOT BETWEEN '01/01/2011' AND '12/31/2011'
ORDER  BY container_no
0
 

Author Comment

by:Rick0124
ID: 37726754
Thanks, Experts!
I tried both scripts but the first one still gets records that have activity for the given dates, while the second one doesn't return any records.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Accepted Solution

by:
Rick0124 earned 0 total points
ID: 37727080
I'm still checking the results but I think I got the script right.

SELECT DISTINCT Container_no
FROM Reefer_Movements
WHERE Container_no NOT IN (SELECT Container_no
FROM Reefer_Movements
WHERE Activity_Date BETWEEN '01/01/2011' AND '12/01/2011')
ORDER BY Container_no
0
 
LVL 18

Expert Comment

by:deighton
ID: 37729136
SELECT DISTINCT Container_no
  FROM Reefer_Movements
  WHERE NOT EXISTS (SELECT 0
  FROM Reefer_Movements RM
  WHERE RM.Activity_Date BETWEEN '01/01/2011' AND '12/31/2011' AND RM.Container_no = Reefer_Movements.Container_no)
  ORDER BY Container_no
0
 

Author Closing Comment

by:Rick0124
ID: 37741264
I checked random containers to see if their activity does not fall between the specified dates and so far, they don't.  Out of a total of more than 110k distinct containers, the query result gave me a little less than 6k distinct containers.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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