August 21, 2010 at 12:57 am
Hi,
I am very new and never write a TSQL code before. I started learning from yesterday.
I have those tables.
Holiday
- HolidayDate
All the Holiday dates in a year will be populated.
Routes
- RouteID(Identity), StaffID, SatDayDelivery(bit)
Pickup
- PickUpID(Identity), RouteID, StaffID, PickupDate
What I want to do is ---
1) Copy all records from Routes to Pickup in every weekdays.
2) And copy the (SatDayDelivery = True) records on Friday or the day before Holiday.
After learning I know how to copy the data but I don't know how to do complex things. Still learning..
Any help will be greatly appreciated.
rgds,
zhtway
August 21, 2010 at 4:10 am
Hello!
Can you post the CREATE TABLE scripts and some sample data generation scripts as well?
Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Follow me on
Twitter: @sqltwins
August 21, 2010 at 4:16 am
Hi,
I have just tested with SQL Agent.
I don't know how to retrieve date from Holiday table.
use testDB
Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate()) from Routes
August 21, 2010 at 4:21 am
Hello!
Shouldn't your "SatDayDelivery" BIT be in the Pickup table and not in the Routes table?
The way I read it, the Routes table associates a staff member with a particular route. It is the Pickup table that has the pickup related information, and therefore the "SatDayDelivery" should be in Pickup, not in Routes.
Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Follow me on
Twitter: @sqltwins
August 21, 2010 at 4:36 am
See if the following does the trick. I have used TABLE variables instead of actual tables just for the POC.
/*
Major Assumption Here:
The "SatDayDelivery" BIT should be in the Pickup table and not in the Routes table.
The way I read it, the Routes table associates a staff member with a particular route.
It is the Pickup table that has the pickup related information, and therefore the "SatDayDelivery" should be in Pickup, not in Routes.
*/
-- Simulate the tables
DECLARE @Holiday TABLE (HolidayDate DATE)
DECLARE @Routes TABLE (RouteID INT,
StaffID INT)
DECLARE @Pickup TABLE (PickUpID INT IDENTITY (1,1),
RouteID INT,
StaffID INT,
PickupDate DATE,
SatDayDelivery BIT)
-- Holds Today's date
DECLARE @TodaysDate DATE
--Generate Test data
INSERT INTO @Holiday VALUES ('08/21/2010'), ('08/22/2010'), ('08/24/2010')
INSERT INTO @Routes VALUES (1, 1), (1, 2), (1, 3), (2, 4), (2, 5)
----Test Point# 1
--SELECT * FROM @Holiday
--SELECT * FROM @Routes
SET @TodaysDate = CONVERT(DATE, GETDATE(), 101)
--Test Point# 2 (Comment the line above and uncomment the one below to test)
--SET @TodaysDate = '08/23/2010'
INSERT INTO @Pickup (RouteID, StaffID, PickupDate, SatDayDelivery)
SELECT r.RouteID, r.StaffID, @TodaysDate AS PickupDate, CASE WHEN EXISTS (SELECT * FROM @Holiday WHERE HolidayDate = @TodaysDate) THEN 1
ELSE 0 END AS SatDayDelivery
FROM @Routes r
--Test Point# 3
SELECT * FROM @Pickup
Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Follow me on
Twitter: @sqltwins
August 21, 2010 at 4:47 am
The SQL2005 version of the script is:
/*
Major Assumption Here:
The "SatDayDelivery" BIT should be in the Pickup table and not in the Routes table.
The way I read it, the Routes table associates a staff member with a particular route.
It is the Pickup table that has the pickup related information, and therefore the "SatDayDelivery" should be in Pickup, not in Routes.
*/
-- Simulate the tables
DECLARE @Holiday TABLE (HolidayDate DATETIME)
DECLARE @Routes TABLE (RouteID INT,
StaffID INT)
DECLARE @Pickup TABLE (PickUpID INT IDENTITY (1,1),
RouteID INT,
StaffID INT,
PickupDate DATETIME,
SatDayDelivery BIT)
-- Holds Today's date
DECLARE @TodaysDate DATETIME
--Generate Test data
INSERT INTO @Holiday VALUES ('08/21/2010')
INSERT INTO @Holiday VALUES ('08/22/2010')
INSERT INTO @Holiday VALUES ('08/24/2010')
INSERT INTO @Routes VALUES (1, 1)
INSERT INTO @Routes VALUES (1, 2)
INSERT INTO @Routes VALUES (1, 3)
INSERT INTO @Routes VALUES (2, 4)
INSERT INTO @Routes VALUES (2, 5)
----Test Point# 1
--SELECT * FROM @Holiday
--SELECT * FROM @Routes
SET @TodaysDate = CONVERT(DATETIME, CONVERT(NCHAR,GETDATE(), 112))
--Test Point# 2 (Comment the line above and uncomment the one below to test)
--SET @TodaysDate = '08/23/2010'
INSERT INTO @Pickup (RouteID, StaffID, PickupDate, SatDayDelivery)
SELECT r.RouteID, r.StaffID, @TodaysDate AS PickupDate, CASE WHEN EXISTS (SELECT * FROM @Holiday WHERE HolidayDate = @TodaysDate) THEN 1
ELSE 0 END AS SatDayDelivery
FROM @Routes r
--Test Point# 3
SELECT * FROM @Pickup
Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Follow me on
Twitter: @sqltwins
August 21, 2010 at 12:19 pm
Hi Nakul Vachhrajani,
Thanks for your replies.
Let me explain to you what I want to do.
Routes is the Master table for all routes.
Pickup is the table for a day where all the staffs have to pickup for their routes.
And All the data in the Pickup will be copied to PickupHistory when all the packages are delivered (may be in the same day or the next days).
Saturday Delivery has to be prepared on Friday. If the Holiday is Friday, Saturday Delivery will has to prepared on Thursday.
As holiday, records will not be copied to Pickup.
That TSQL will be in the SQL Agent Job and will schedule for weekdays.
Basically my logic is like that..
if today <> holiday then
copy all the records from routes to pickup
if today = friday or today= holiday-1 then
copy all the SatDelivery records to pickup
endif
endif
Actually, I can write code in VB.net and run as job schedule or windows service, yet I am not expert.
But I know there is something I can do with TSQL and I want to learn TSQL a bit.
Thanks
zhtway
August 21, 2010 at 1:18 pm
zawhtway (8/21/2010)
Hi,I am very new and never write a TSQL code before. I started learning from yesterday.
I have those tables.
Holiday
- HolidayDate
All the Holiday dates in a year will be populated.
Routes
- RouteID(Identity), StaffID, SatDayDelivery(bit)
Pickup
- PickUpID(Identity), RouteID, StaffID, PickupDate
What I want to do is ---
1) Copy all records from Routes to Pickup in every weekdays.
2) And copy the (SatDayDelivery = True) records on Friday or the day before Holiday.
After learning I know how to copy the data but I don't know how to do complex things. Still learning..
Any help will be greatly appreciated.
rgds,
zhtway
My recommendation is to learn the basics before trying to do such a thing. You can do that at sites like w3Schools.com .
--Jeff Moden
Change is inevitable... Change for the better is not.
August 21, 2010 at 6:51 pm
Hi,
Here is the code I have done and tested and seems working.
Correct me if something is wrong. I'll have some more to do
use MobilePOD
Declare @chkHoliday int
Declare @chkTomorrow int
declare @DayName varchar(20)
Declare @DayNo int
SELECT @chkHoliday = count(*) from holidays where CONVERT(CHAR(10),freedate,101) = CONVERT(CHAR(10),GETDATE(),101)
SELECT @chkTomorrow = count(*) from holidays where CONVERT(CHAR(10),freedate,101) = CONVERT(CHAR(10),GETDATE()+1,101)
set @DayNo=(DATEPART(dw, getdate()) + @@DATEFIRST) % 7
Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate())
from Routes
where @chkHoliday = 0
Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate()+1)
from Routes
where @chkHoliday = 0 and Routes.SatDayDelivery = 1 and@DayNo = 6
Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate()+2)
from Routes
where @chkHoliday = 0 and Routes.SatDayDelivery = 1 and @chkTomorrow = 1 and @DayNo = 5
August 22, 2010 at 1:29 pm
zawhtway (8/21/2010)
Hi,Here is the code I have done and tested and seems working.
Correct me if something is wrong. I'll have some more to do
use MobilePOD
Declare @chkHoliday int
Declare @chkTomorrow int
declare @DayName varchar(20)
Declare @DayNo int
SELECT @chkHoliday = count(*) from holidays where CONVERT(CHAR(10),freedate,101) = CONVERT(CHAR(10),GETDATE(),101)
SELECT @chkTomorrow = count(*) from holidays where CONVERT(CHAR(10),freedate,101) = CONVERT(CHAR(10),GETDATE()+1,101)
set @DayNo=(DATEPART(dw, getdate()) + @@DATEFIRST) % 7
Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate())
from Routes
where @chkHoliday = 0
Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate()+1)
from Routes
where @chkHoliday = 0 and Routes.SatDayDelivery = 1 and@DayNo = 6
Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate()+2)
from Routes
where @chkHoliday = 0 and Routes.SatDayDelivery = 1 and @chkTomorrow = 1 and @DayNo = 5
Ask yourself if you'll have the correct number of entries if the second INSERT kicks in according to the criteria... if you think so, then look at the first INSERT and see why you'll end up with double the number of rows you expect.
Also, did you actually want times on the PickupDate your inserting into the PickupTable with?
Be advised that @@DateFirst is fickle if someone changes it. @@DateFirst should probably NOT be used here.
Last but not least, the SELECTs you used on the Holiday table make it impossible for an index to be used correctly in at least two different ways. We really need for you to generate the CREATE TABLE statements for the Holidays and Routes tables along with their indexes. Start by right clicking on each table and look for "Script Table as" and follow your nose. If it doesn't script the indexes at the same time, then do the same for the indexes.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply