Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

TSQL copy data in weekdays Expand / Collapse
Author
Message
Posted Saturday, August 21, 2010 12:57 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 19, 2014 9:22 AM
Points: 21, Visits: 72
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
Post #972915
Posted Saturday, August 21, 2010 4:10 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, September 12, 2014 2:30 AM
Points: 1,422, Visits: 1,838
Hello!

Can you post the CREATE TABLE scripts and some sample data generation scripts as well?


Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Post #972924
Posted Saturday, August 21, 2010 4:16 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 19, 2014 9:22 AM
Points: 21, Visits: 72
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
Post #972927
Posted Saturday, August 21, 2010 4:21 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, September 12, 2014 2:30 AM
Points: 1,422, Visits: 1,838
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
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Post #972928
Posted Saturday, August 21, 2010 4:36 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, September 12, 2014 2:30 AM
Points: 1,422, Visits: 1,838
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
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Post #972931
Posted Saturday, August 21, 2010 4:47 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, September 12, 2014 2:30 AM
Points: 1,422, Visits: 1,838
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
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Post #972932
Posted Saturday, August 21, 2010 12:19 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 19, 2014 9:22 AM
Points: 21, Visits: 72
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
Post #972983
Posted Saturday, August 21, 2010 1:18 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:46 PM
Points: 35,218, Visits: 31,676
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #972992
Posted Saturday, August 21, 2010 6:51 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 19, 2014 9:22 AM
Points: 21, Visits: 72
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
Post #973010
Posted Sunday, August 22, 2010 1:29 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:46 PM
Points: 35,218, Visits: 31,676
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #973095
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse