If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Please help in SQL

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-17-04, 12:20
leau leau is offline
Registered User
 
Join Date: Feb 2004
Posts: 33
Please help in SQL

In SQL I need to do the following:

If CallTime < 100 Minutes Then Indicator = '0'
If CallTime >100 and CallTime < 199 minutes, Then Indicator = '1'
If CallTime > 200 Then

Record CallTime Indicator
1st 200 4
n 100(each additional 200 minutes of call) 5
last Remainder minutes of call(Must be < 200) 6

The Indicator is what I need to return.

e.g.

CallTime = 500 minutes

Record CallTime Indicator
1 200 4
2 100 5
3 100 5
4 100 6

In my Stored Procedure, I wrote as:

Case When CallTime < '100' Then '0' Else
Case When (CallTime > '100' and CallTime < '199') Then '7' Else
??????

I don't know whether I can achieve in this way.

Could anyone give me a suggestion how to do that?

Thanks alot.
Reply With Quote
  #2 (permalink)  
Old 05-17-04, 12:48
Enigma Enigma is offline
The SQL Apostle
 
Join Date: Jul 2003
Location: The Dark Planet
Posts: 1,387
I got confused in the
Quote:
Record CallTime Indicator
1st 200 4
n 100(each additional 200 minutes of call) 5
last Remainder minutes of call(Must be < 200) 6

The Indicator is what I need to return.
part ... could you elaborate ???
__________________
Get yourself a copy of the The Holy Book

order has no physical Brett in The meaning of a Kaiser . -database data
Reply With Quote
  #3 (permalink)  
Old 05-17-04, 12:54
leau leau is offline
Registered User
 
Join Date: Feb 2004
Posts: 33
sorry about the confused

Record CallTime Indicator
1st 200 4
n 100(each additional 200 minutes of call) 5
last Remainder minutes of call(Must be < 200) 6


So, suppose the Total CallTime = 500 Then it will be seperated into 4 records as following


Record CallTime Indicator
1 200 4
2 100 5
3 100 5
4 100 6
Reply With Quote
  #4 (permalink)  
Old 05-17-04, 12:57
Enigma Enigma is offline
The SQL Apostle
 
Join Date: Jul 2003
Location: The Dark Planet
Posts: 1,387
So you mean if we have a record

Code:
1st     700    4
then
it will become
Code:
1     200      4
2     100      5
3     100      5
4     100      5
5     100      5
6     100      6
__________________
Get yourself a copy of the The Holy Book

order has no physical Brett in The meaning of a Kaiser . -database data
Reply With Quote
  #5 (permalink)  
Old 05-17-04, 12:59
leau leau is offline
Registered User
 
Join Date: Feb 2004
Posts: 33
yeah

I mean change the original table into our required table.

But currently, I only need to identify the Indicator

Thanks

Last edited by leau; 05-17-04 at 13:02.
Reply With Quote
  #6 (permalink)  
Old 05-17-04, 13:16
leau leau is offline
Registered User
 
Join Date: Feb 2004
Posts: 33
Can I create a function to get the indicator, then use the function in Stored Procedure instead of using the "case"?

Thanks alot
Reply With Quote
  #7 (permalink)  
Old 05-17-04, 13:24
Enigma Enigma is offline
The SQL Apostle
 
Join Date: Jul 2003
Location: The Dark Planet
Posts: 1,387
I must say ... this looks like a job which will need a loop ... a cursor or a while loop ...
__________________
Get yourself a copy of the The Holy Book

order has no physical Brett in The meaning of a Kaiser . -database data
Reply With Quote
  #8 (permalink)  
Old 05-17-04, 14:04
leau leau is offline
Registered User
 
Join Date: Feb 2004
Posts: 33
my function

The following is my function, is this right?

Please help!

CREATE Function getInd(@CT varchar(50))
returns varchar(50)
as
begin

--@CT is CallTime


declare @Ind varchar(50)

If (@CT <= '100') Begin
Set @Ind = '0'
End
Else If (@CT > '100' and @CT < '200') Begin
Set @Ind = '1'
End
Else If (@CT > ='200') Begin
set @CT = @CT - 200
set @Ind = '4'

While (@CT - 100 > 0) Begin

Set @CT = @CT - 100
Set @Ind = '5'
End

set @Ind = '6'
End


return @Ind
End

Last edited by leau; 05-17-04 at 14:07.
Reply With Quote
  #9 (permalink)  
Old 05-17-04, 14:06
Enigma Enigma is offline
The SQL Apostle
 
Join Date: Jul 2003
Location: The Dark Planet
Posts: 1,387
I Think that the @ct needs to be numeric ...
__________________
Get yourself a copy of the The Holy Book

order has no physical Brett in The meaning of a Kaiser . -database data
Reply With Quote
  #10 (permalink)  
Old 05-17-04, 14:06
TallCowboy0614 TallCowboy0614 is online now
Throwin' the steel to SQL
 
Join Date: Feb 2004
Location: Marina Del Rey, CA
Posts: 3,551
Quote:
Originally Posted by leau
Can I create a function to get the indicator, then use the function in Stored Procedure instead of using the "case"?

Thanks alot
I use functions in some of my queries to do something similar to what you are doing...there's no reason why you can't send the total time into the function, perform the looping logic inside the function, then spit back out the indicator. And yes, this (the function call) can be done in the actual select.

I am also a relative newbie to SQL Server, but use functions quite a bit, and I think your scenario is an excellent example of when a function should be used.

My .02...
__________________
aka "Paul"
Non est ei similis.

// Dear maintainer:
// Once you are done trying to 'optimize' this routine,
// and have realized what a terrible mistake that was,
// please increment the following counter as a warning
// to the next guy:
// total_hours_wasted_here = 13
Reply With Quote
  #11 (permalink)  
Old 05-17-04, 14:08
TallCowboy0614 TallCowboy0614 is online now
Throwin' the steel to SQL
 
Join Date: Feb 2004
Location: Marina Del Rey, CA
Posts: 3,551
you are also attempting to do numeric operations on your @CT varchar...

if it was me, I'd either send the call time in as an integer, then manipulate it internally to the function, or else immediately convert it to an integer once you get into the function.
__________________
aka "Paul"
Non est ei similis.

// Dear maintainer:
// Once you are done trying to 'optimize' this routine,
// and have realized what a terrible mistake that was,
// please increment the following counter as a warning
// to the next guy:
// total_hours_wasted_here = 13
Reply With Quote
  #12 (permalink)  
Old 05-17-04, 14:11
leau leau is offline
Registered User
 
Join Date: Feb 2004
Posts: 33
thanks

I will change the @CT as numeric

The problem is that now the callingtime will be splited as

if calltime=500

calltime indicator
300 4
200 5
100 5
0 6

Last edited by leau; 05-17-04 at 14:16.
Reply With Quote
  #13 (permalink)  
Old 05-17-04, 14:13
leau leau is offline
Registered User
 
Join Date: Feb 2004
Posts: 33
to TallCowboy0614

But I don't know how to do that?

Could you modify my function or something?

Thanks alot.
Reply With Quote
  #14 (permalink)  
Old 05-17-04, 14:16
Enigma Enigma is offline
The SQL Apostle
 
Join Date: Jul 2003
Location: The Dark Planet
Posts: 1,387
Well ... the function will return only the last value of indicator ... i think you will need to use a table valued function
__________________
Get yourself a copy of the The Holy Book

order has no physical Brett in The meaning of a Kaiser . -database data
Reply With Quote
  #15 (permalink)  
Old 05-17-04, 14:20
TallCowboy0614 TallCowboy0614 is online now
Throwin' the steel to SQL
 
Join Date: Feb 2004
Location: Marina Del Rey, CA
Posts: 3,551
Quote:
Originally Posted by Enigma
Well ... the function will return only the last value of indicator ... i think you will need to use a table valued function
Ooops :blush:

Very good point...I was assuming that he/she just wanted a single value...and now looking back on it, that is not the case...so yeah, I'll just back on out and defer to those who can read better'n I can

Yep, it would need a cursor and loop, or my choice also (now...), the table-returning function.
__________________
aka "Paul"
Non est ei similis.

// Dear maintainer:
// Once you are done trying to 'optimize' this routine,
// and have realized what a terrible mistake that was,
// please increment the following counter as a warning
// to the next guy:
// total_hours_wasted_here = 13
Reply With Quote
Reply

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On