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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 subquery? or?

Author  Topic 

schonne
Starting Member

3 Posts

Posted - 2007-06-13 : 01:25:54
Hi guys!

I need help. I have two tables (customers & orders). I want to build a query that lists all the customers who haven't placed an order in over a year. Below are the simplified tables (i've removed other fields that I don't think are relevant to this query). Do I need to do a subquery or something? Please help!

TABLES:

CUSTOMERS
custid (unique autonumber)

ORDERS
custid (int)
orderdate (date)


pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-06-13 : 02:34:29
you can do in different ways...

using sub query

Select id, name from @t where id not in
(Select id
from @c where ord_date > dateadd(month, -12, getdate()))



--------------------------------------------------
S.Ahamed
Go to Top of Page

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-06-13 : 02:37:43
if u want to eliminate time part

dateadd(day, 0,datediff(day,0, dateadd(month, -12, getdate())))

--------------------------------------------------
S.Ahamed
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-13 : 08:34:47
Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -