tags:

views:

80

answers:

8

Hi,

I'm sure how to do this and was looking for some help. In SQL, I need a query that goes like this Get all people Ids who's last order date is larger then x(a predefined date). I've tried using Max() but it doesn't seem to be working right.

thanks

+3  A: 

Something like

SELECT ID FROM PeopleTable WHERE LAST_ORDER_DATE > '01-JUN-2009'

How the dates are handled depends on your RDBMS

MattH
A: 

Use the WHERE clause.

MSDN: WHERE Clause

Jon Seigel
A: 

Of course this needs more info... Are you joining to an orders table? Can you provide some ddl, otherwise with what you have above its quite simple:

SELECT blah1, blah2...FROM MyTable WHERE LastOrderDate > x

No seriously without more info that is what it is ...

JonH
+1  A: 

Simply:

select * from People where lastOrderDate > @InputDate

Paul
+1  A: 

That actually depends on the table structure. If there is a "Last Order Date" column in the users table, then:

SELECT UserID
   FROM Users
   WHERE LastOrderDate > 'predefined date'

If you need to find it in an "orders" table, this might be correct

SELECT DISTINCT UserID
   FROM Orders
   WHERE OrderDate > 'predefined date'

Or maybe if you need to take an user status into account, then...

SELECT DISTINCT O.UserID
   FROM Orders O
   INNER JOIN Users U ON U.UserID = O.UserID
   WHERE O.OrderDate > 'predefined date'
     AND U.UserStatus = 1
BlaM
A: 
SELECT PERSON_ID FROM ORDERS WHERE ORDER_DATE>DATE('2000-01-01')
fd
+3  A: 

I'm guessing you don't know what the last order date is?

Select people_id
    , Max(Order_Date) as last_order_date
from orders_table AS O
Group By people_id
Having Max(Order_Date) > @CutOff_Date
Jeff O
A: 

I've gotten it to work with this:

SELECT PeopleId FROM Orders Group By PeopleId Having Max(OrderDate)<'9/23/2009'
jumbojs