Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > СУБД, общие вопросы > SQL , создание базы данных


Автор: Berliner 27.3.2006, 15:50
Вот подкинули задание
есть база маленькая собирает погоду для регионов(стран).
Вот хранимые процедуры которые надо реализовать.
sp_fetch_within_day(процедура которую нужно сделать) data_within_day(таблица с данными) -Returns data for a specified data item (temperature, sunlight etc), falling between the start date/time and end/time and at the specified resolution.

sp_fetch_daily_summary (процедура нужно сделать) data_daily_summary(таблица с данными) Returns data for a specified data item (temperature, sunlight etc), falling between the start date/time and end/time and at the specified resolution.

sp_create_daily_summary(процедура которую нужно сделать) data_within_day(таблица в данными) Reads a list of the available regions from table regions, and then processes each of the tables containing data collected during the day, calculates summaries, and stores the summarised data in the respective data_daily_summary_* table.

Одна из сложностей в том, для каждого региона (страны)данные храняться в ствоей талице

дело в том что для каждой старны есть своя таблица data_within_day_usa(для USA) или data_withing_day-russia
data_daily_summary_USA data_daily_summary_russia (любые другие страны)
есть таблица regions_def где храняться все дрегионы
и вот что написано в таске The stored procedure sp_create_daily_summary will read the list of regions available in the database, (from table regions_def) and then process all of the data collected during the day, for each of these regions.

эт тпипа такие запрсы будут

Fetch all data available on atmospheric pressure recorded during the day in the region of Russia, starting from 10-Sep-1984 to 25-Sept-2000, and return the data aggregated in 15-day intervals.

This query is of the generic form:

Select all data on <data item type> that is available as daily summary data for region <region> during [start date] and [end date] and aggregate the data using a resolution period of <N> days
я написал такую процедуру


Код

if object_id ('dbo.sp_fetch_within_day') is NOT NULL drop procedure dbo.sp_fetch_within_day
GO
create procedure dbo.sp_fetch_within_day
@temperature varchar
--.................
--................
--................ 
AS

SELECT C(temperature parameters or humidity, from where I can fetch them?) 
as T parameter
from tablewithparameters, data_item_def where (name=@temperature) 
and resolution(вот сюда не знаю что тулить).










а мне прислали такой шаблон:


Код

-- NOTE: generate_series() function requires postgres ver 8.X.X. 




  created a test table like this :

CREATE TABLE test (
 data_ts timestamp,
 epoch integer
)

I tried a sample resolution of 6 minutes = 360 seconds

-- I slightly redesigned the schema, converting timestamps in the data_ts to epoch (number of seconds), to reduce the type conversion time. I will redesign the schema later to use integers (ssm - seconds since midnight) insted of the timestamp.

Here is the query I run:

create temporary table sr as select * from generate_series(360,1800*24,360) as a;
update sr set a = a + (select min(epoch) from test);
create index ss on sr(a); 
select S.a,count(T.epoch) as cnt from sr S left join test T
on T.epoch >= S.a - 360 AND T.epoch <= S.a group by S.a order by S.a asc;

All I had to do to complete the query was to add the following aggregate functions:

first(), max(), min(), last(), sum()



Более подродно в приложении
непонимаю как надо эту задачу решать подскажите плиз.




Автор: Berliner 27.3.2006, 16:24
подскажи плиз что нужно писать почему такая разница в моей схеме и этой sp которую другой человек написал, чтобы я от нее отталкивался

Код

if object_id ('dbo.sp_fetch_within_day') is NOT NULL drop procedure dbo.sp_fetch_within_day
GO
create procedure dbo.sp_fetch_within_day
@temperature varchar
--.................
--................
--................ 
AS

SELECT C(temperature parameters or humidity, from where I can fetch them?) 
as T parameter
from tablewithparameters, data_item_def where (name=@temperature) 
and resolution(вот сюда не знаю что тулить).



заказчик

Код


-- NOTE: generate_series() function requires postgres ver 8.X.X. 




  created a test table like this :

CREATE TABLE test (
 data_ts timestamp,
 epoch integer
)

I tried a sample resolution of 6 minutes = 360 seconds

-- I slightly redesigned the schema, converting timestamps in the data_ts to epoch (number of seconds), to reduce the type conversion time. I will redesign the schema later to use integers (ssm - seconds since midnight) insted of the timestamp.

Here is the query I run:

create temporary table sr as select * from generate_series(360,1800*24,360) as a;
update sr set a = a + (select min(epoch) from test);
create index ss on sr(a); 
select S.a,count(T.epoch) as cnt from sr S left join test T
on T.epoch >= S.a - 360 AND T.epoch <= S.a group by S.a order by S.a asc;

All I had to do to complete the query was to add the following aggregate functions:

first(), max(), min(), last(), sum()


Автор: Berliner 27.3.2006, 17:02
может кто нибудь знает?

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)