I have a table with a series of dates. What I want to do is take the very last date in the table and propagate the next 52 weeks. I don't have any clues as to how to do this. I know I'll need a loop structure to count 52 loops but where I'm lost is how to do the loop function.
My table is very simple:
ID (Autonum) | Datevalue (smalldatetime)
is loop count = 52? If yes quit
if no then begin loop -
// Grab last Datevalue ID and then add 7 days to that date
-
-
SELECT MAX 1 id, Datevalue AS newdateval from Table ORDER BY id desc
-
-
newdateval = DateAdd(day,7,newdateval)
-
-
// Insert the new date
-
-
INSERT INTO Table ([Datevalue]) values ([newdateval])
repeat loop
repeat grabbing last entry until we cycle through 52 iterations (this is where I'm confused) so I need help with the loop, any ideas?
10 1761
I got this snippet of code to generate a sequential series of number. You can use this technique and modify it into an INSERT statement.
Happy Coding!!!
~~ CK
Very cool, that helped. Now, I have some further work to do that perhaps maybe someone here can help me do. What I want to do now is add two more fields to this query: the month number which is always sequential and a person id from 1 to 6. Here's what i have so far: - DECLARE @count int;
-
SET @count = 0;
-
WHILE (@count < 52)
-
BEGIN
-
SELECT TOP 1 id, DateAdd(Day, 7, datevalue) AS dateval, monthid, personid from dates ORDER BY id desc
-
INSERT INTO dates (datevalue) values (dateval)
-
SET @count= @count + 1
-
END
-
GO
-
I have decided the month number field is unnecessary. So now my situation is just adding the person id to the schedule. I know its going to be another loop but the big question I have is where should this loop sit?
Ok, here's a strange thing I encountered in my quest. I try using an alias to update the value in the DB but it will not accept it for my insert clause. Here's my query: - DECLARE @count int;
-
SET @count = 0;
-
WHILE @count < 52
-
BEGIN
-
@personid = 1
-
SELECT TOP 1 id, DateAdd(Day, 7, weekend_val) AS wkendval from Weekends ORDER BY id desc
-
INSERT INTO Weekends (weekend_val,employee_id) values (wkendval,personid)
-
SET @count= @count + 1
-
END
-
GO
-
Each time I try my insert, I get this:
The name "wkendval" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
I thought you could use aliased names in insert clauses, am I wrong? What do I do to fix this?
Can you provide a subset of sample data and expected results?
I suspect you can just do something along the lines of - INSERT INTO SomeTable (DateIDField, EmployeeIDField)
-
SELECT DateIDField, EmployeeIDField
-
FROM DateTable, EmployeeTable
-
WHERE DATEPART('yyyy', DateField) = 2011
No need to create some complicated loop code.
I could see that working if I didn't have to populate the Employee field automatically with numbers 1 to 6. That is why I need the loop structure. Its more than just populating the dates. What I am trying to do is populate a date every 7 days in the table and thereby assign an employee number on a rotating schedule.
If that's the case, you can use the week number mod 6 + 1 and that will give you the rotating employee ID without using a loop. - INSERT INTO SomeTable (DateIDField, EmployeeIDField)
-
SELECT DateIDField,
-
((DATEPART('ww', DateField) % 6) + 1) AS EmployeeID
-
FROM DateTable
-
WHERE DATEPART('yyyy', DateField) = 2011
It's complianing about DATEPART saying its an invalid parameter 1 specified for datepart. Here is what I have now: -
INSERT INTO weekends (weekend_val, coordinator_id)
-
SELECT weekends,
-
((DATEPART('ww', weekends) % 6) + 1) AS coordinator_id
-
FROM weekends
-
WHERE DATEPART('yyyy', weekend_val) = 2011
-
and the error: - Msg 1023, Level 15, State 1, Line 3
-
Invalid parameter 1 specified for datepart.
I figured it out. Since I need a custom order for two of the employees, I ended up using a loop structure. Now of course if there is a way to simplify this code I'm all ears. :) -
DECLARE @count int;
-
DECLARE @wkendval smalldatetime;
-
DECLARE @personid int;
-
SET @count = 0;
-
set @personid = 0;
-
WHILE @count < 38
-
BEGIN
-
set @personid = 1
-
INSERT INTO weekends (weekend_val,employee_id)
-
SELECT TOP 1 DateAdd(Day, 7, weekend_val), @personid from weekends ORDER BY id desc
-
SET @count= @count + 1
-
-
set @personid = 2
-
INSERT INTO weekends (weekend_val,employee_id)
-
SELECT TOP 1 DateAdd(Day, 7, weekend_val), @personid from weekends ORDER BY id desc
-
SET @count= @count + 1
-
-
set @personid = 3
-
INSERT INTO weekends (weekend_val,employee_id)
-
SELECT TOP 1 DateAdd(Day, 7, weekend_val), @personid from weekends ORDER BY id desc
-
SET @count= @count + 1
-
-
set @personid = 4
-
INSERT INTO weekends (weekend_val,employee_id)
-
SELECT TOP 1 DateAdd(Day, 7, weekend_val), @personid from weekends ORDER BY id desc
-
SET @count= @count + 1
-
-
set @personid = 5
-
INSERT INTO weekends (weekend_val,employee_id)
-
SELECT TOP 1 DateAdd(Day, 7, weekend_val), @personid from weekends ORDER BY id desc
-
SET @count= @count + 1
-
-
set @personid = 1
-
INSERT INTO weekends (weekend_val,employee_id)
-
SELECT TOP 1 DateAdd(Day, 7, weekend_val), @personid from weekends ORDER BY id desc
-
SET @count= @count + 1
-
-
set @personid = 2
-
INSERT INTO weekends (weekend_val,employee_id)
-
SELECT TOP 1 DateAdd(Day, 7, weekend_val), @personid from weekends ORDER BY id desc
-
SET @count= @count + 1
-
-
set @personid = 6
-
INSERT INTO weekends (weekend_val,employee_id)
-
SELECT TOP 1 DateAdd(Day, 7, weekend_val), @personid from weekends ORDER BY id desc
-
SET @count= @count + 1
-
-
set @personid = 4
-
INSERT INTO weekends (weekend_val,employee_id)
-
SELECT TOP 1 DateAdd(Day, 7, weekend_val), @personid from weekends ORDER BY id desc
-
SET @count= @count + 1
-
-
set @personid = 5
-
INSERT INTO weekends (weekend_val,employee_id)
-
SELECT TOP 1 DateAdd(Day, 7, weekend_val), @personid from weekends ORDER BY id desc
-
SET @count= @count + 1
-
-
END
-
GO
-
It should be - INSERT INTO weekends (weekend_val, coordinator_id)
-
SELECT datevalue,
-
((DATEPART('ww', datevalue) % 6) + 1) AS coordinator_id
-
FROM dates
-
WHERE DATEPART('yyyy', datevalue) = 2011
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Gerry |
last post by:
As I'm not a PHP-prgrammer at all, I just need Help with this: I have
had a guestbook-page in Europe and will
now have to move it to a US based-server. This makes the
time-function showing time...
|
by: Rowlie |
last post by:
Hi
I am very new to access. I am starting a new job and have been asked to
design a data base.
After much messing around I have got something that is just o.k.
The problem is that every time I...
|
by: Mark |
last post by:
Hello,
We currently have a table (access 2000) that has the following values:
706 6789 1 5.11 N N 20040923
792 6592 1 10.05 N N 20040923
795 1605 1 6.30 N N 20040923
807 5817 1 6.33 N N...
|
by: tomerslider |
last post by:
i need some code that show me all the expired product that i have in
every month
example:
the date today is 4/11/05 i need that it will show me all the proudct
that there dates betwenn 1/11/05...
|
by: erekose666 |
last post by:
I need a java prog to do the following:
Create class Date with the following capabilities:
a) Output the date in multiple formats, such as:
MM/DD/YYYY
June 14, 2005
DDD YYYY
b) Use...
|
by: xadrian |
last post by:
Hi,
I'm trying to put together a query that will order entries in a table by the rating values which decay over time. I'm not too well versed in the intricacies of Postgres, or complex queries,...
|
by: eskelies |
last post by:
I have two queries. My goal is to pull a range of numbers that fall on or in a certain date range. For example, $100 on 9/1/2007 and $200 on 9/24/2007. I want the date range to pick up both the...
|
by: crystal2005 |
last post by:
Hi i encounterd some problem when i implement CSS for rounded Corner table.
Need help please... :D
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />...
|
by: jayjayplane |
last post by:
Currently I use Access form/subform to design a database interface, one field is date type field, but I need if the user inputs the future date like later than system currently date, I want it pop up...
|
by: Stepheno |
last post by:
Hi,
I am a recently converted Iseries (AS/400) RPG programmer trying to learn HTML/CSS/JavsScript all at the same time (not fun). My problem deals mostly with CSS. I will be reveiving a table,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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: 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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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,...
| |