MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 29th, 2004, 06:31 PM
kitto kitto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 kitto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How to calculate Peak/ Off-peak & Work days/ weekend

I would appreciate if someone help me to achieve it:

I have a table of call records and I want to distinguish which call is during peak (0700 to 2000 hours), offpeak (2001 to 0659 hours) and whether it is during normal week day or weekend (Satureday/ Sunday). Here is my Table:

Call_ID
Datetime
CLI
Called_Destination
Duration
Peak_off-peak*
Weekday*

* These two columns do not come with data, rather I want to fill them dynamically with flags for instance if time is Peak then value put in Peak_off-Peak column= P , if offpeak then Peak_off-Peak= OP, same with Weekday=D (work day except weekends) and Weekday=W if weekend.

Thanks in advance for your help.

Kitto

Reply With Quote
  #2  
Old November 29th, 2004, 08:15 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 6 sec
Reputation Power: 1018
Code:
update calls
   set [Peak_off-peak] =
         case when convert(char(5),[datetime],8) 
                between '07:00' and '20:00'
              then 'P' else 'OP' end
     , [Weekday] =
         case when datepart(dw,[Datetime])
                between 2 and 6
              then 'D' else 'W' end       
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old November 30th, 2004, 02:45 AM
kitto kitto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 kitto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by r937
Code:
update calls
   set [Peak_off-peak] =
         case when convert(char(5),[datetime],8) 
                between '07:00' and '20:00'
              then 'P' else 'OP' end
     , [Weekday] =
         case when datepart(dw,[Datetime])
                between 2 and 6
              then 'D' else 'W' end       


Thanks. However I shall appreciate if kindly let me know what formula I can put in a computed column. For instance, Peak_off-Peak and Weekday are my computed columns, and values would be filled base on data in datatime field.
Attached Images
File Type: jpg formula_in_computed_column.JPG (76.0 KB, 180 views)

Reply With Quote
  #4  
Old November 30th, 2004, 03:07 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 6 sec
Reputation Power: 1018
use the same formulas as in the UPDATE statement

Reply With Quote
  #5  
Old November 30th, 2004, 04:20 AM
kitto kitto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 kitto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by r937
use the same formulas as in the UPDATE statement


Tried but it said "error when validiating formula".

Reply With Quote
  #6  
Old November 30th, 2004, 07:42 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 6 sec
Reputation Power: 1018
whoa, that's weird, because i tested the UPDATE statement and it works

can we see your attempt?

Reply With Quote
  #7  
Old November 30th, 2004, 07:44 AM
kitto kitto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 kitto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by r937
use the same formulas as in the UPDATE statement


Sorry my mistake, I have found it working. Thanks a lot.

One last thing, when my Weekday = Weekend (W), I want to set my [Peak_off-peak] filed to Off-peak (OP) even it is Peak time (since on weekend, off-peak rate applied for calls).

Thanks for yoru help.

Reply With Quote
  #8  
Old November 30th, 2004, 07:53 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 6 sec
Reputation Power: 1018
well, to make that change, you would have to modify the CASE expression for [Peak_off-peak] slightly to include a test for weekday

Reply With Quote
  #9  
Old November 30th, 2004, 09:58 AM
kitto kitto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 kitto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by r937
well, to make that change, you would have to modify the CASE expression for [Peak_off-peak] slightly to include a test for weekday


Sorry for inconveniece, since I am newbie in SQL that's why stupid questions. This is what I tried but it says invalid column 'D'.

update CALCULATIONS1 set [Weekday] = case when datepart(dw,[dATE]) between 2 and 6 then 'D' else 'W' end
, [Peak_off-peak] = case when convert(char(5),[time],8) between '07:00:00' and '20:00:00' or [Weekday]="W" then 'P' else 'OP' end

Reply With Quote
  #10  
Old November 30th, 2004, 10:19 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 6 sec
Reputation Power: 1018
so did you give up on the idea of computed columns? because you don't update computed columns, you just define them in a CREATE TABLE or ALTER TABLE statement

Reply With Quote
  #11  
Old November 30th, 2004, 10:30 AM
kitto kitto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 kitto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by r937
so did you give up on the idea of computed columns? because you don't update computed columns, you just define them in a CREATE TABLE or ALTER TABLE statement


Well, I decided to use update on existing table (data filled) since I was not able to work computer columns. Can you please guide the correct syntax of Update statement so that it first check if it is Weekend then update [Peak_off-peak] to 'OP'.

Thanks.

Reply With Quote
  #12  
Old November 30th, 2004, 10:41 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 6 sec
Reputation Power: 1018
change the OR to AND and that'll do it


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > How to calculate Peak/ Off-peak & Work days/ weekend


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump