Solved

sql query that does an "allocation"

Posted on 2010-11-22
2
287 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
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

947 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now