473,569 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Report Containing Sequential Dates (removing date gaps)

I have a table containing typed log entries. One log entry is supposed
to be created every twelve hours, but sometimes there are gaps. I need
to create a report showing the time of entry, and the actual log entry.
I can't just list the contents of the log table, because if I do that
there will be dates missing. Instead, when there isn't a log entry for
a date, I need to print the date, and then just leave the log entry
blank.

The SQL bellows shows what the output should look like. HOWEVER, the
code below makes use of a temp table containing all possible dates. My
question is, is there a better way to do this - one that doesn't
involve the temp table? Thanks in advance.
create table StationLog (LogDate datetime, LogText char(11))
insert StationLog values ('1/1/2005 00:00:00','entr y one')
insert StationLog values ('1/1/2005 12:00:00','entr y two')
insert StationLog values ('1/2/2005 00:00:00','entr y three')
insert StationLog values ('1/3/2005 00:00:00','entr y four')

create table Date_List (TempDate datetime)
insert Date_List values ('1/1/2005 00:00:00')
insert Date_List values ('1/1/2005 12:00:00')
insert Date_List values ('1/2/2005 00:00:00')
insert Date_List values ('1/2/2005 12:00:00')
insert Date_List values ('1/3/2005 00:00:00')
insert Date_List values ('1/3/2005 12:00:00')

select TempDate, LogText
from Date_List
left outer join StationLog on Date_List.TempD ate = StationLog.LogD ate

drop table StationLog
drop table Date_List

Jul 23 '05 #1
6 1948
Ray
Basically your've got it. I would suggest creating two permanent tables one
of dates and another for hours in a day. Create date rows for a few years
back and several years forward and using it going forward instead of
creating and dropping each time.

Also adding other associated values like for each day what month, day of
week, day of month, company holiday, etc type information usually comes in
handy sooner or later.

<ch************ ****@gmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
I have a table containing typed log entries. One log entry is supposed
to be created every twelve hours, but sometimes there are gaps. I need
to create a report showing the time of entry, and the actual log entry.
I can't just list the contents of the log table, because if I do that
there will be dates missing. Instead, when there isn't a log entry for
a date, I need to print the date, and then just leave the log entry
blank.

The SQL bellows shows what the output should look like. HOWEVER, the
code below makes use of a temp table containing all possible dates. My
question is, is there a better way to do this - one that doesn't
involve the temp table? Thanks in advance.
create table StationLog (LogDate datetime, LogText char(11))
insert StationLog values ('1/1/2005 00:00:00','entr y one')
insert StationLog values ('1/1/2005 12:00:00','entr y two')
insert StationLog values ('1/2/2005 00:00:00','entr y three')
insert StationLog values ('1/3/2005 00:00:00','entr y four')

create table Date_List (TempDate datetime)
insert Date_List values ('1/1/2005 00:00:00')
insert Date_List values ('1/1/2005 12:00:00')
insert Date_List values ('1/2/2005 00:00:00')
insert Date_List values ('1/2/2005 12:00:00')
insert Date_List values ('1/3/2005 00:00:00')
insert Date_List values ('1/3/2005 12:00:00')

select TempDate, LogText
from Date_List
left outer join StationLog on Date_List.TempD ate = StationLog.LogD ate

drop table StationLog
drop table Date_List

Jul 23 '05 #2

ch************* ***@gmail.com wrote:
I have a table containing typed log entries. One log entry is supposed to be created every twelve hours, but sometimes there are gaps. I need to create a report showing the time of entry, and the actual log entry. I can't just list the contents of the log table, because if I do that there will be dates missing. Instead, when there isn't a log entry for a date, I need to print the date, and then just leave the log entry
blank.

The SQL bellows shows what the output should look like. HOWEVER, the
code below makes use of a temp table containing all possible dates. My question is, is there a better way to do this - one that doesn't
involve the temp table? Thanks in advance.
create table StationLog (LogDate datetime, LogText char(11))
insert StationLog values ('1/1/2005 00:00:00','entr y one')
insert StationLog values ('1/1/2005 12:00:00','entr y two')
insert StationLog values ('1/2/2005 00:00:00','entr y three')
insert StationLog values ('1/3/2005 00:00:00','entr y four')

create table Date_List (TempDate datetime)
insert Date_List values ('1/1/2005 00:00:00')
insert Date_List values ('1/1/2005 12:00:00')
insert Date_List values ('1/2/2005 00:00:00')
insert Date_List values ('1/2/2005 12:00:00')
insert Date_List values ('1/3/2005 00:00:00')
insert Date_List values ('1/3/2005 12:00:00')

select TempDate, LogText
from Date_List
left outer join StationLog on Date_List.TempD ate = StationLog.LogD ate

drop table StationLog
drop table Date_List

You could create a temporary table with the required dates very easily
using the following code:
--------------------------------
declare @startdate datetime
set @startdate = '2005-03-04 12:00:00'

select identity(int,0,-12) as hours
into #tmphours
from sysobjects

select dateadd(hh, hours, @startdate)
from #tmphours
order by 1
-------------------------------

Once you have your base table (#tmphours), use an outer join to your
station log table.

--
Cheers
David Rowland
http://dbmonitor.tripod.com

Jul 23 '05 #3
On 3 Mar 2005 15:06:40 -0800, ch************* ***@gmail.com wrote:
The SQL bellows shows what the output should look like. HOWEVER, the
code below makes use of a temp table containing all possible dates. My
question is, is there a better way to do this - one that doesn't
involve the temp table? Thanks in advance.


Instead of a temp table, you could create the needed dates on the fly:

CREATE VIEW Digits (NUM)
AS
SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL
SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
SELECT 9
GO

SELECT TempDate, LogText
FROM StationLog
RIGHT OUTER JOIN
(
SELECT dateadd(hour, 6 * NUM,
(select min(LogDate) FROM StationLog)) AS TempDate
FROM (
SELECT D1.NUM + 10* D10.NUM + 100* D100.NUM AS NUM
FROM DIGITS D1, DIGITS D10, DIGITS D100
) AS NUMBERS
WHERE NUM <= datediff(hour,
(select min(LogDate) FROM StationLog),
(select max(LogDate) FROM StationLog)) / 6
) AS Date_List
ON StationLog.LogD ate = Date_List.TempD ate

here's the output:

TempDate LogText
------------------------------------------------------ -----------
2005-01-01 00:00:00.000 entry one
2005-01-01 06:00:00.000 NULL
2005-01-01 12:00:00.000 entry two
2005-01-01 18:00:00.000 NULL
2005-01-02 00:00:00.000 entry three
2005-01-02 06:00:00.000 NULL
2005-01-02 12:00:00.000 NULL
2005-01-02 18:00:00.000 NULL
2005-01-03 00:00:00.000 entry four
Jul 23 '05 #4
That's very slick. Is it ok to use sysobjects like that? On the
database I'm using, sysobjects contains 1393 rows, so that means I
could get about 700 days worth of data before I'd run into problems.

Jul 23 '05 #5
If you use sysobjects twice with no join, then you will get 1393*1393
rows!

i.e.
FROM sysobjects,
sysobjects

There is no reason why you can't use this table at all.

Jul 23 '05 #6
You could also use the select top n clause to limit the output to only
the number of rows you want

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3177
by: Jim Fox | last post by:
I have a report that I created that has no data, just boxes, lines and one date field not tied to anything. Basically this report will just print pages according to the dates you entered. Then according to the day, it will hide certain boxes. Here is the code (BELOW) I've drummed up, but how do I use it to print out a range of pages? ...
1
17648
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
1
1352
by: Peter Bailey | last post by:
I have a student table with studentid as pk and an enrolment date field as date. I have made a qry to group on date and count the number of enrolments for a particular day. What this doesnt give me of course are days where there were no enrolments ie there are gaps in the dates. Ideas would be welcome as to how I might get the missing...
1
1663
by: Mike Cooper | last post by:
Hi everyone, This is a tough one. I have a database full of solicitations, identifying a customer and recording initial call, first followup, second followup, etc. My boss want to be able to generate a report showing a list of customer who were call between two different dates that he types into a form. That, I have done. My boss...
5
2289
by: DJ Craig | last post by:
I have written a program that uses the built-in PHP date functions. All that the program does is calculate the amout of time between two dates, or calculate the date a certain amount of time before or after another date. It works, but I didn't realize until after I finished it that it won't work for dates outside the range of 1969-2033. I...
2
1781
by: Arnold | last post by:
Greetings Gurus, In a report showing the names of students and their progress, I am getting an error in the name field (Name: #Error). The report gets its data from an unbound form containing two unbound textboxes, "txtStartDate" and "txtEndDate," and a multi-select listbox, "listName." I think the error occurs when there is no correct...
5
2483
by: AdrianE | last post by:
I have a database where a record has a start date and an end date and is linked to an employee. Ho do I create a report that will show each employee and gaps between the employees next record start date and end date? Many thanks, a confuesed Adrian :confused:
1
2754
by: Rob Woodworth | last post by:
Hi, I'm having serious problems getting my report to work. I need to generate a timesheet report which will contain info for one employee between certain dates (one week's worth of dates). I have a table containing records for each job done, the records contain date, employee name, job done (a code representing the type of job), cost code...
8
4424
by: Ryan | last post by:
Hello, I'm new to Access and DB's in general. I've taken over some light duty support for a lab information system we use in house. Most of our dates are reported as "10/31/2006 12:30:00 PM" (With seconds listed in the report field). However, nobody wants to see the seconds. So I tried to format it =Format(DateCollected, "mm/dd/yy hh:nn...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.