Solved

Retrieve records that are not in specified timeframe

Posted on 2012-03-15
6
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 53

Expert Comment

by:Huseyin KAHRAMAN
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

752 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