Advertisement

05.21.2007 at 08:50AM PDT, ID: 22585006
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.6

While Loop in t-SQL  SQL SERVER  2005

Asked by MickyNin in MS SQL Server

Tags: , , , ,

Hi All,
I am taking a chance on this.I am using SQL Server 2005 with t-sql syntax.
Now this is a bit of a guess that this would work as the other ways didn’t:

I have 2 databases.
I am using one table (CPETRACKING) from database A which will hold a modem number, the BTSid ( base station it touched ) and time-inserted(the time it hit the base station).
(I have pulled all this information into DB B so I can work of one database)
In Database B I have the Account Number of the customer, the modem id and the date the customer cancelled.
I want to find out the Last BTSid of an account that was cancelled.
The only way to join both tables is through Modem id.
The problem is that one modem can be used by more then one customer.
So if customer number 1 cancels the modem is passed onto Customer number 2, if this customer cancels also and I try to find the last BTSid hit by each account before the modem cancelled I am only getting back Customer Number 2 results.
I think if i used a While Loop this might work, I am just unsure how to script this and would appriciate much help.
I have tried using sub queries but that doesn’t work.
I tried bring back the max time_inserted but the problem there is that the modem can hit many different btsid so I will get a results for the max time inserted for each btsid hit.
I think if I used a While Loop this might work, I am just unsure how to script this.

What I want to do is:      
Find the Max row number from  CPETRACKING Table
If the cpetracking.time_inserted < =Account.Cancellation_date then pull the last btsid from
CPETRACKING Table
ELSE Go to the Max row number -1 from CPETRACKING table
If the cpetracking_Time_inserted <= cancellation date .........

 

To find the Row number on the CPETRACKING table I did the following:
SELECT modem,time_inserted, btsid, row_number() over (order by time_inserted) as rownum
from cpetracking  order by rownum desc;

This is the linking I use:
SELCT a.account_id, a.account_status_date,cpe.btsid,cpe.time_inserted,mac.value
FROM  BOSS_ACCOUNT a (account_status_date is the cancellation date when a.account_status_id = 2)
-- code to return ModemID associated with service just before it was cancelled
left join (select max(b.OrderedItemHIstoryID) as max_hist_id, b.AccountID
from BOSS_ORDERED_ITEM_HISTORY b
where b.DateAdded< CONVERT(DateTime, FLOOR( CONVERT( Float, GETDATE()))) -1
group by b.AccountID) eq_hist on eq_hist.AccountID = a.account_id
left join BOSS_ORDERED_ITEM_HISTORY eq_hist2 on eq_hist2.OrderedItemHistoryID = eq_hist.max_hist_id
left join dbo.BOSS_ORDERED_ITEM oi on oi.OrderedItemID = eq_hist2.OrderedItemID
left join BOSS_ORDERED_ITEM_TRACKING_VARIABLES_MAC mac on mac.ItemID = oi.OrderedItemID
 --Then I link CPETRACKING substring(eid,7,14) to mac.value (table above)to get the BTSid.
--Only bring back cancelled accounts
WHERE a.account_status_id = 2

 
Any suggestions would be greatly appriciated.
Regards,
MickyNinStart Free Trial
 
Keywords: While Loop in t-SQL SQL SERVER 2…
 
Loading Advertisement...
 
[+][-]05.21.2007 at 10:21AM PDT, ID: 19128344

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: loop, while, sql, 2005, server
Sign Up Now!
Solution Provided By: bhess1
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.22.2007 at 02:14AM PDT, ID: 19132500

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32