Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Select N rows and min

 
   Database Forums (Home) -> Datamining RSS
Next:  DataBase Project  
Author Message
small brother

External


Since: Dec 08, 2007
Posts: 3



(Msg. 1) Posted: Sat Dec 08, 2007 2:15 am
Post subject: Select N rows and min
Archived from groups: microsoft>public>sqlserver>datamining (more info?)

Dear All
I need your help to sove this problem.

I have a table Customers_Bal containing the following Information:

Date Balance
01/01/2007 1000
01/02/2007 700
01/03/2007 800
01/04//2007 600
01/05/2007 1200
01/06/2007 1800
01/07/2007 2600
01/08/2007 900
01/09/2007 600

I need to select the first N rows and get the min(balance) and put it a new
field (Bal_Min) in the Nth record.
after select nrows from the second row - Get the min(balnce) put it a new
field (Bal_Min) in the Nth+1 record and so on till the EOF.

Let's suppose that N=3 the result will be:

Date Balance Bal_min
01/01/2007 1000
01/02/2007 700
01/03/2007 800 700 (Min (1000,700,800))
01/04//2007 600 600 (Min(700,800,600))
01/05/2007 1200 600 (Min(800,600,1200))
01/06/2007 1800 600 (Min(600,1200,1800))
01/07/2007 2600 1200 (Min(1200,1800,2600))
01/08/2007 900 900 (Min(1800,2600,900))
01/09/2007 600 600 (Min(1200,900,600))

N Is a Variable.

Thanks for your help.

 >> Stay informed about: Select N rows and min 
Back to top
Login to vote
Dejan Sarka

External


Since: Mar 18, 2004
Posts: 317



(Msg. 2) Posted: Mon Dec 10, 2007 2:58 am
Post subject: Re: Select N rows and min [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You need sliding aggregations query. Something like


SELECT O1.date,
MIN(O2.Balance) AS sliding_minimum
FROM Customers_Bal O1 JOIN Customers_Bal AS O2
ON O2.date > DATEADD(day, -3, O1.date)
AND O2.date<= O1.date
GROUP BY O1.date
ORDER BY O1.date

should be close to what you need. As you did not priovide DDL and DML
statements for the table, I did not check the query.

--
Dejan Sarka
http://blogs.solidq.com/EN/dsarka/default.aspx

"small brother" wrote in message

> Dear All
> I need your help to sove this problem.
>
> I have a table Customers_Bal containing the following Information:
>
> Date Balance
> 01/01/2007 1000
> 01/02/2007 700
> 01/03/2007 800
> 01/04//2007 600
> 01/05/2007 1200
> 01/06/2007 1800
> 01/07/2007 2600
> 01/08/2007 900
> 01/09/2007 600
>
> I need to select the first N rows and get the min(balance) and put it a
> new
> field (Bal_Min) in the Nth record.
> after select nrows from the second row - Get the min(balnce) put it a new
> field (Bal_Min) in the Nth+1 record and so on till the EOF.
>
> Let's suppose that N=3 the result will be:
>
> Date Balance Bal_min
> 01/01/2007 1000
> 01/02/2007 700
> 01/03/2007 800 700 (Min (1000,700,800))
> 01/04//2007 600 600 (Min(700,800,600))
> 01/05/2007 1200 600 (Min(800,600,1200))
> 01/06/2007 1800 600 (Min(600,1200,1800))
> 01/07/2007 2600 1200 (Min(1200,1800,2600))
> 01/08/2007 900 900 (Min(1800,2600,900))
> 01/09/2007 600 600 (Min(1200,900,600))
>
> N Is a Variable.
>
> Thanks for your help.

 >> Stay informed about: Select N rows and min 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Select and sub select - Hi, I was wondering if I could get some help with the query below. The idea is to list the incomes generated by every agent per month. The query works however; one of the agents (Agent_ID = 35) has sub agents. The latter have assigned an additional I...

select into - Is there a way to create a table using something similar to 'select ..... into' in a dmx query?

SELECT in sections - I want to know how to retrieve records from a SELECT command in sections. I mean, if total records of a SELECT command is 100,000, I want to use SELECT that retrieves me them in 20 "sections" of 5000 : first 5000, second 5000, sixth 5000 or ...

Select Sum Group By - Hi. I have the following scenario.. TABLE1= sale.accom TABLE2= sale.meals TABLE3= sale.other I would like to get the Sum(price) of all 3 table and match against a client. I would just like to know the official way of doing this before I implement..

Select table name - How can I select 'table name' in select clause if I use &#85;NION ALL? Example: select * from table1 &#117;nion all select * from table2 &#117;nion all select * from table3 Now I want to see the tablename from where I'll get data. Ex: select...
   Database Forums (Home) -> Datamining All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]