Solved

SQL Lookup table with multiple references based on update date field

Posted on 2010-08-24
5
412 Views
Last Modified: 2012-06-27
Hello,

Trying to wrap my head around the best approach with this.  One table tracks the status of a staff member with whatever code they select.  It records the date and time in which they make their selection.

Another table stores the description of the codes available.  It also stores the date in which that code was updated (which can also apply to it being created).  So you could potentially have the same code repeated but with different dates.

So what I would like to do is produce a report which reports the correct code for the date the selection was made.  In the example attached, August 20th Code 2 meant they were on break.  But by August 23rd, code 2 has changed to On Phone.  So for all days between Aug 20th and Aug 22nd it should report it as On Break.  After the 23rd it should report it as On Phone.  

How would this be constructed in SQL? (I include my weak and feable attempt as it doesnt work)

SELECT *
  FROM CodeLogging log
 INNER JOIN CodeReference ref ON log.code = ref.code AND log.Date >= ref.LastUpdate

Open in new window

SampleReport.xls
0
Comment
Question by:Wedmore
[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
5 Comments
 
LVL 9

Expert Comment

by:rg20
ID: 33516416
You would need and audit table that shows the version of the code and what the previous values are for that date range.  Then you could select the message where the date selected is between the start and end dates.
0
 
LVL 11

Assisted Solution

by:Larissa T
Larissa T earned 100 total points
ID: 33516431
You need 2 dates for each code: start date - end date

ON log.code = ref.code AND log.Date >= ref.LastUpdate and log.Date <ref.lastValidDate

 
0
 

Expert Comment

by:benzadon
ID: 33516618

SELECT *
FROM CodeLogging log
INNER JOIN CodeReference ref ON log.code = ref.code
WHERE log.Date >= ref.LastUpdate
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 400 total points
ID: 33517719
You could create a days calendar and join your tables to it
CREATE FUNCTION [dbo].[DAY_CALENDAR]
(
      @StartDate TDATETIME,
      @EndDate TDATETIME
)
RETURNS
      @DYCALENDAR TABLE
(
      StartDate DATETIME,
      EndDate DATETIME,
      OnDay DATETIME
)
AS
BEGIN
      DECLARE @varStartTime TDATETIME, @varEndTime TDATETIME
      
      set @varStartTime = DATEADD(dd, 0, DATEDIFF(dd, 0, @StartDate))
      
      while @varStartTime <= @EndDate
      begin
            set @varEndTime = DATEADD(DD, 1, @varStartTime) --end time
            set @varRes = @varStartTime
            
            insert into @DYCALENDAR
            values (@varStartTime, @varEndTime, @varStartTime)
            
            set @varStartTime = @varEndTime
      end            
      
      RETURN
END
       
Query
SELECT *
FROM CodeLogging log
INNER JOIN CodeReference ref ON log.code = ref.code
INNER JOIN  DAY_CALENDAR(08/01/2010', '08/31/2010') LOGDAYS ON
  (log.date >= LOGDAYS.StartDate  and log.Date < LOGDAYS.EndDate)

0
 

Author Closing Comment

by:Wedmore
ID: 33532576
I have a dates table already so I can see that I could maybe use that.

benzdon thats just repeating the query I sent by moving the filter from the join to the where clause, it still wouldnt work.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

732 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