|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, February 05, 2013 8:52 PM
Points: 21,
Visits: 70
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:52 AM
Points: 1,277,
Visits: 1,608
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, February 05, 2013 8:52 PM
Points: 21,
Visits: 70
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:52 AM
Points: 1,277,
Visits: 1,608
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:52 AM
Points: 1,277,
Visits: 1,608
|
|
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://beyondrelational.com/modules/2/blogs/77/nakuls-blog.aspx Be courteous. Drive responsibly.
Follow me on Twitter: @nakulv_sql Google Plus: +Nakul
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:52 AM
Points: 1,277,
Visits: 1,608
|
|
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://beyondrelational.com/modules/2/blogs/77/nakuls-blog.aspx Be courteous. Drive responsibly.
Follow me on Twitter: @nakulv_sql Google Plus: +Nakul
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, February 05, 2013 8:52 PM
Points: 21,
Visits: 70
|
|
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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 9:57 PM
Points: 32,906,
Visits: 26,790
|
|
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 "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, February 05, 2013 8:52 PM
Points: 21,
Visits: 70
|
|
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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 9:57 PM
Points: 32,906,
Visits: 26,790
|
|
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 "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|