Advertisement

02.10.2008 at 05:40AM PST, ID: 23151271
[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!

Need efficient way to loop through one table and insert records in another
Tags: SQL Server 2000
Fellow Experts,

I am usually pretty handy with basic SQL operations, but I have a need for an operation
that requires some sort of loop and/or cursor/table variable/temp table approach that
needs to be as efficient as possible.

Here is what I need to do, in SQL Server 2000:

1. I have two tables, one each in two databases on the same server.

DatabaseA.dbo.ShiftHrs_Update
--------------------------------------------------------------------
shift_id uniqueidentifier
BlockStart datetime
BlockDur int
pattern_id uniqueidentifier

This table has 1 to 7 records for each shift; each record represents the working hours at a
particular company location.  Each location may be open different days of the week (and
thus a limit of 7 records for any given shift_id), and be open during various hours on the
days they are open.  BlockStart specifies the start date/time of the shift, and BlockDur is
the length of the shift in seconds.  The dates are all within a "reference week" that started
Sunday 4 Jan 2004.


DatabaseB.dbo.pattern_values
--------------------------------------------------------------------
pattern_id uniqueidentifier
sample_date datetime
sample_value float

This table is used to spread a forecasted weekly work requirement throughout a day, split into
half-hour intervals.  Thus, if I want to spread the workload evenly throughtout open hours, I
would have a record for each half-hour interval inside the shift's open hours, with the same
sample_value for each record.  The dates are all within a "reference week" that started
Sunday 4 Jan 2004.

2. For each record in DatabaseA.dbo.ShiftHrs_Update, I have to insert records in DatabaseB.dbo.pattern_values
such that for the time period specified in DatabaseA.dbo.ShiftHrs_Update I have every half-hour
interval covered in DatabaseB.dbo.pattern_values.  For example, if the record in DatabaseA.dbo.ShiftHrs_Update
is:

<shift_id>, 2004-01-05 09:00:00, 28800, <pattern_id>      (that is, Monday from 9am - 5pm)

Then I would need to insert the following in DatabaseB.dbo.pattern_values:

<pattern_id>,2004-01-05 09:00:00,1
<pattern_id>,2004-01-05 09:30:00,1
<pattern_id>,2004-01-05 10:00:00,1
<pattern_id>,2004-01-05 10:30:00,1
<pattern_id>,2004-01-05 11:00:00,1
<pattern_id>,2004-01-05 11:30:00,1
<pattern_id>,2004-01-05 12:00:00,1
<pattern_id>,2004-01-05 12:30:00,1
<pattern_id>,2004-01-05 13:00:00,1
<pattern_id>,2004-01-05 13:30:00,1
<pattern_id>,2004-01-05 14:00:00,1
<pattern_id>,2004-01-05 14:30:00,1
<pattern_id>,2004-01-05 15:00:00,1
<pattern_id>,2004-01-05 15:30:00,1
<pattern_id>,2004-01-05 16:00:00,1
<pattern_id>,2004-01-05 16:30:00,1

(Note that there is no record for the 5:00 pm interval, because that is the end time of the shift
that day.)

Any help will be greatly appreciated.  If anything above is unclear, please do not hesitate to ask
for clarification.  I will be away for a few hours, but will be checking back in in the early afternoon
during USA Eastern time.

Best regards,

Patrick
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: matthewspatrick
Solution Provided By: nmcdermaid
Participating Experts: 2
Solution Grade: A
Views: 19
Translate:
Loading Advertisement...
02.10.2008 at 07:46PM PST, ID: 20863862

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 04:15AM PST, ID: 20865641

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 04:38AM PST, ID: 20865754

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.10.2008 at 07:46PM PST, ID: 20863862

Rank: Genius

You've pretty much got all you need without using a loop. Just alter the pattern_values table to contain the actual hourly timeslot, then join on the pattern_id

For example (making assumptions here but you can correct me)

ShiftHrs_Update has this:

shift_id , BlockStart ,                 BlockDur ,  pattern_id
xyz       , 2004-01-05 09:00:00, 28800    ,     abc

pattern_values needs to have this (it probably doesn't at the moment):

pattern_id , shift_minutes , sample_value
abc            ,          0      ,          1
abc            ,         30     ,          1
abc            ,         60     ,          1
abc            ,         90     ,          1
abc            ,        120    ,          1

etc........

Now you just join those tables on pattern_id, and have a calc that adds pattern_values.sample_date to ShiftHrs_Update.BlockStart

SELECT P.pattern_id, DATEADD(mi,U.BlockStart,P.Shift_Minutes), U.Sample_Value
FROM pattern_values P
INNER JOIN
ShiftHrs_Update U
ON P.pattern_id = U.pattern_id

On a different note, I suggest you change your FLOAT to a NUMERIC unless you have a good reason.

Also I avoid the use of uniqueidentifier unless you want this to be unique accross databases worldwide.. just my preference.


Anyway I've made assumption about exactly what dependencies and functions the tables server and you'll probably need to correct me.

The main point is that you can avoid loops by use of one to many joins

Also if you find you're doing this kind of things a lot, its worth either creating a special work table, or a special work UDF that accepts parameters and returns a suitable recordset to join to.
Accepted Solution
 
02.11.2008 at 04:15AM PST, ID: 20865641

Rank: Sage

<<that requires some sort of loop and/or cursor/table variable/temp table approach that
needs to be as efficient as possible.>>
As nmcdermaid politely pointed out,  *Looping* and *efficient* are contradictory words in SQL Server's realm...

My two cents...
 
02.11.2008 at 04:38AM PST, ID: 20865754
Racimo wrote...
> As nmcdermaid politely pointed out,  *Looping* and *efficient* are contradictory words in SQL Server's realm...

Indeed!

Yesterday afternoon I started trying some approaches using a "calendar" table, similar to what
nmcdermaid describes, and I started seeing that the solution was much, much easier than I thought.
For some reason, I had developed a sort of tunnel vision that was closing off other ways of seeing the
problem.

As for the design of "DatabaseB", with the usage of uniqueidentifier and float columns...  I have no
control over that one.  DatabaseB follows the schema of another designer and cannot be altered.
I have total control over DatabaseA, but of course some of my choices there are contingent on the
(in my opinion, sometimes questionable) choices made by the designers of DatabaseB.  So, I am
stuck with uniqueidentifier for the foreign keys, and float for the patern weights.

Thanks a bunch for the help.

Regards,

Patrick
 
 
02.11.2008 at 04:44AM PST, ID: 20865781
<<For some reason, I had developed a sort of tunnel vision that was closing off other ways of seeing the
problem.>>
If that can make you feel better, keep in mind that thinking in terms of sets as opposed to think procedurally(looping) is anything but natural or intuitive.  It is however much more adequate to get the best out of database systems.

Hope this helps..
 
 
02.12.2008 at 01:30AM PST, ID: 20873431
Well at least ou know where you stand with the original designer..... he's certainly no guru.
 
 
 
20080236-EE-VQP-29 / EE_QW_EXPERT_20070906