Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gagani
Posting Yak Master

112 Posts

Posted - 05/31/2012 :  09:33:29  Show Profile  Reply with Quote
ORDERID INACTIVE TRANTIMESTAMP
804172 1 2012-05-31 14:08:33.903
804172 0 2012-05-31 14:10:53.933
804172 0 2012-05-31 14:12:01.417

I want the rows of orderid with min(trantimestamp) and inactive = 0

khtan
In (Som, Ni, Yak)

Singapore
17689 Posts

Posted - 05/31/2012 :  09:34:32  Show Profile  Reply with Quote

SELECT TOP 1 *
FROM   yourtable
WHERE  inactive - 0
ORDER BY trantimestamp



KH
Time is always against us

Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 05/31/2012 :  09:36:57  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
with cte as
(
select *, seq = row_number() over (partition by orderid order by timestamp) from tbl where inactive = 0
)
select *
from cte
where seq = 1


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

gagani
Posting Yak Master

112 Posts

Posted - 05/31/2012 :  09:41:07  Show Profile  Reply with Quote
ORDERID INACTIVE TRANTIMESTAMP
804172 1 2012-05-31 14:08:33.903
804172 0 2012-05-31 14:10:53.933
804172 0 2012-05-31 14:12:01.417
804173 0 2012-05-31 14:12:01.417
804173 0 2012-05-31 14:12:02.417

With the first reply, I am getting only the first row as the output,

804172 1 2012-05-31 14:08:33.903

But I want all the rows in that table meeting that criteria.

I am using sql 2000, row_number() is not found in it.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17689 Posts

Posted - 05/31/2012 :  09:45:39  Show Profile  Reply with Quote
you mean for each ORDERID ?

SELECT *
FROM   yourtable t
       INNER JOIN
       (
           SELECT ORDERID, TRANTIMESTAMP = MIN(TRANTIMESTAMP)
           FROM   yourtable
           WHERE  INACTIVE = 0
           GROUP BY ORDERID
       ) m  ON t.ORDERID = m.ORDERID and t.TRANTIMESTAMP = m.TRANTIMESTAMP
WHERE  t.INACTIVE = 0



KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000