Solved

Retrieve records that are not in specified timeframe

Posted on 2012-03-15
6
191 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best counters for cpu high usage 3 21
Sql server function help 15 27
T-SQL: "HAVING CASE" Clause 1 23
Increment column based of a FK 8 19
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

816 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

11 Experts available now in Live!

Get 1:1 Help Now