Solved

sql query that does an "allocation"

Posted on 2010-11-22
2
291 Views
Last Modified: 2012-06-27
I'm need a query that does an "allocation".

I start with a table where each row is data for a span of multiple minutes,
I want to generate a table that has that data equally distributed
among one minute time intervals starting at the same time.  

For example, I start with this:


CREATE TABLE [dbo].[INPUT_DATA](
      [START_TIME] [datetime] NULL,
      [END_TIME] [datetime] NULL,
      [UNITS] [real] NULL
) ON [PRIMARY]

<table has one row of data, covering 5 minutes>

START_TIME      END_TIME      UNITS
2010-05-26 13:30:00.000      2010-05-26 13:35:00.000      15

I want this:


CREATE TABLE [dbo].[OUTPUT_DATA](
      [TIME] [datetime] NULL,
      [UNITS] [real] NULL
) ON [PRIMARY]

<table has 5 rows of data, each covering one minute, starting at the same time as above>

TIME      UNITS
2010-05-26 13:30:00.000      3
2010-05-26 13:31:00.000      3
2010-05-26 13:32:00.000      3
2010-05-26 13:33:00.000      3
2010-05-26 13:34:00.000      3


Seems a little like a pivot but I can't quite see how to do it.

Thanks!
0
Comment
Question by:cipriano555
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34192788

CREATE TABLE [dbo].[INPUT_DATA](
      [START_TIME] [datetime] NULL,
      [END_TIME] [datetime] NULL,
      [UNITS] [real] NULL
)
insert [INPUT_DATA] select '2010-05-26 13:30:00.000', '2010-05-26 13:35:00.000', 15
insert [INPUT_DATA] select '2010-05-27 13:30:00.000', '2010-05-27 13:35:00.000', 17

CREATE TABLE [dbo].[OUTPUT_DATA](
      [TIME] [datetime] NULL,
      [UNITS] [real] NULL
)

--- IGNORE ABOVE THIS LINE. IT SETS UP THE TABLES AND DATA

insert OUTPUT_DATA
select dateadd(mi, number, d.start_time),
	d.UNITS / m.n
from input_data d
cross apply (select datediff(mi,d.START_TIME, d.END_TIME) n) m
inner join master..spt_values v on v.type='P' and v.number between 0 and m.n-1

Open in new window

0
 

Author Closing Comment

by:cipriano555
ID: 34197176
Thanks!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question