Solved

sql query that does an "allocation"

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

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 …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

792 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