Hi all,
I am writing a database using SQL as the back end and Access as the
front end. My project is to create a rota for when people have to
work. The problem I have is setting up the tables so I can query them
correctly.
I currently have a staff table which holds the staff_id and staff_name
fields. I have a worktime table that has the staff_id, date, start
time, end time as fields. What I would like to do able to do is have a
query that returns the times a person is working in date order along
the row
i.e
staff_id, date, day1_start, day1_end, day2_start, day2_end etc..
1, 8/1/04, 9:00, 18:00, 9:00, 18:00, etc..
I can't seem to work out a query that will do this easily. I can
think of ways of manipulating the data heavily but that defeats the
object. I wonder if my table structure is incorrect. Is it good
practice to have a table where each field is a date? Seeing this will
need to be referred to for year on year comparison it seems a bad way
of doing it. Basically is this the correct structure for a diary
application?
Any help from you great people out there would be superb! Thanks in
anticipation.
Cheers
Mike 2 10478
Try this out (TSQL):
CREATE TABLE WorkTime (staff_id INTEGER, start_datetime DATETIME,
end_datetime DATETIME, PRIMARY KEY (staff_id, start_datetime))
INSERT INTO WorkTime VALUES (1,'2003-01-08T09:00:00','2003-01-08T18:00:00')
INSERT INTO WorkTime VALUES (1,'2003-01-09T10:00:00','2003-01-09T19:00:00')
INSERT INTO WorkTime VALUES (1,'2003-01-10T09:00:00','2003-01-10T18:00:00')
INSERT INTO WorkTime VALUES (2,'2003-01-08T09:00:00','2003-01-08T18:00:00')
DECLARE @dt DATETIME
SET @dt = '20030108'
SELECT staff_id,
MIN(CASE DATEDIFF(DAY,@dt,start_datetime) WHEN 0 THEN
CONVERT(CHAR(8),start_datetime,8) END) AS day1_start,
MAX(CASE DATEDIFF(DAY,@dt,end_datetime) WHEN 0 THEN
CONVERT(CHAR(8),end_datetime,8) END) AS day1_end,
MIN(CASE DATEDIFF(DAY,@dt,start_datetime) WHEN 1 THEN
CONVERT(CHAR(8),start_datetime,8) END) AS day2_start,
MAX(CASE DATEDIFF(DAY,@dt,end_datetime) WHEN 1 THEN
CONVERT(CHAR(8),end_datetime,8) END) AS day2_end,
MIN(CASE DATEDIFF(DAY,@dt,start_datetime) WHEN 2 THEN
CONVERT(CHAR(8),start_datetime,8) END) AS day3_start,
MAX(CASE DATEDIFF(DAY,@dt,end_datetime) WHEN 2 THEN
CONVERT(CHAR(8),end_datetime,8) END) AS day3_end,
MIN(CASE DATEDIFF(DAY,@dt,start_datetime) WHEN 3 THEN
CONVERT(CHAR(8),start_datetime,8) END) AS day4_start,
MAX(CASE DATEDIFF(DAY,@dt,end_datetime) WHEN 3 THEN
CONVERT(CHAR(8),end_datetime,8) END) AS day4_end,
MIN(CASE DATEDIFF(DAY,@dt,start_datetime) WHEN 4 THEN
CONVERT(CHAR(8),start_datetime,8) END) AS day5_start,
MAX(CASE DATEDIFF(DAY,@dt,end_datetime) WHEN 4 THEN
CONVERT(CHAR(8),end_datetime,8) END) AS day5_end
FROM WorkTime
WHERE start_datetime >= @dt AND end_datetime < DATEADD(DAY,5,@dt)
GROUP BY staff_id
--
David Portas
------------
Please reply only to the newsgroup
--
Thats great I understand what you are doing and everything. Although
ultimatly I want to be able to update the database. So by altering
that table then it won't pass the changes on to the other tables. This
is why I was questioning my structure in the inital question. Cheers
for replying though I learnt a technique there.
Mike
"David Portas" <RE****************************@acm.org> wrote in message news:<SY********************@giganews.com>... Try this out (TSQL):
CREATE TABLE WorkTime (staff_id INTEGER, start_datetime DATETIME, end_datetime DATETIME, PRIMARY KEY (staff_id, start_datetime))
INSERT INTO WorkTime VALUES (1,'2003-01-08T09:00:00','2003-01-08T18:00:00') INSERT INTO WorkTime VALUES (1,'2003-01-09T10:00:00','2003-01-09T19:00:00') INSERT INTO WorkTime VALUES (1,'2003-01-10T09:00:00','2003-01-10T18:00:00') INSERT INTO WorkTime VALUES (2,'2003-01-08T09:00:00','2003-01-08T18:00:00')
DECLARE @dt DATETIME SET @dt = '20030108'
SELECT staff_id, MIN(CASE DATEDIFF(DAY,@dt,start_datetime) WHEN 0 THEN CONVERT(CHAR(8),start_datetime,8) END) AS day1_start, MAX(CASE DATEDIFF(DAY,@dt,end_datetime) WHEN 0 THEN CONVERT(CHAR(8),end_datetime,8) END) AS day1_end, MIN(CASE DATEDIFF(DAY,@dt,start_datetime) WHEN 1 THEN CONVERT(CHAR(8),start_datetime,8) END) AS day2_start, MAX(CASE DATEDIFF(DAY,@dt,end_datetime) WHEN 1 THEN CONVERT(CHAR(8),end_datetime,8) END) AS day2_end, MIN(CASE DATEDIFF(DAY,@dt,start_datetime) WHEN 2 THEN CONVERT(CHAR(8),start_datetime,8) END) AS day3_start, MAX(CASE DATEDIFF(DAY,@dt,end_datetime) WHEN 2 THEN CONVERT(CHAR(8),end_datetime,8) END) AS day3_end, MIN(CASE DATEDIFF(DAY,@dt,start_datetime) WHEN 3 THEN CONVERT(CHAR(8),start_datetime,8) END) AS day4_start, MAX(CASE DATEDIFF(DAY,@dt,end_datetime) WHEN 3 THEN CONVERT(CHAR(8),end_datetime,8) END) AS day4_end, MIN(CASE DATEDIFF(DAY,@dt,start_datetime) WHEN 4 THEN CONVERT(CHAR(8),start_datetime,8) END) AS day5_start, MAX(CASE DATEDIFF(DAY,@dt,end_datetime) WHEN 4 THEN CONVERT(CHAR(8),end_datetime,8) END) AS day5_end FROM WorkTime WHERE start_datetime >= @dt AND end_datetime < DATEADD(DAY,5,@dt) GROUP BY staff_id This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sean R. Lynch |
last post by:
I've been playing around with Zope's RestrictedPython, and I think I'm
on the way to making the modifications necessary to create a
capabilities-based restricted execution system. The idea is to...
|
by: Steve |
last post by:
Can anyone point me to a primer for creating OCX controls in VB .net? In particular, I want to access a web service in a VB6 application (not an easy thing to do). So, if I can write an ActiveX...
|
by: groups |
last post by:
If you haven't read the book, nor watched the movie, "Harry Potter and
the Chamber of Secrets". Harry Potter finds a diary with nothing
written in it. When he starts writing in it, the diary...
|
by: Mike |
last post by:
Hi all,
I am writing a database using SQL as the back end and Access as the
front end. My project is to create a rota for when people have to
work. The problem I have is setting up the tables...
|
by: epigram |
last post by:
I've been creating some toy ASP.NET apps in an effort to understand the
technology. I've something odd with regards to project/solution creation.
If you create a new asp.net application, it...
| |
by: nondisclosure007 |
last post by:
I am actually trying to do this and found in this thread some very good
information (begin quote):
{
In ASP.NET you should store and manipulate the word document on the
server.
You can...
|
by: jonathan.beckett |
last post by:
Hi all,
Just announcing the latest version of my blog / diary / journal script.
You can get it (and read all about it) at;
http://www.pluggedout.com/index.php?pk=dev_blog
It's edging closer...
|
by: SandipAdhikary |
last post by:
hello sir,
I have faced a prob.how can i make a diary control using .net with c#.
exactly microsoft diary control.
explanation=: there is one calender control ,u have to click a cell of...
|
by: Ben Grimwade |
last post by:
Hi, im developing an Application that integrates with Outlook by creating an appointment in there calender.
The code in question is below
Dim appt As Outlook.AppointmentItem
obj =...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |