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.
| Author |
Topic |
|
mapidea
Posting Yak Master
124 Posts |
Posted - 2009-02-03 : 23:26:59
|
| Query to write the orders placed in 6 months after the first order was placed for all the customers.To find the first order placedselect o.customerid,(Min(o.Orderdate) Over (partition By o.CustomerId)) as firstorderfrom orders as oTo find the 6 monthsselect o.customerid,--p.productid,(Min(o.Orderdate) Over (partition By o.CustomerId)) as firstorderfrom orders as o--inner join orderdetails as d on o.orderid = d.orderid--inner join products as p on d.productid = p.productidwhere o.orderdate <= dateadd(mm, 6, firstorder)the where condition is not taking this. |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-03 : 23:41:25
|
| check like thisselect o.customerid,--p.productid,o.firstorderfrom (select customerid,Min(Orderdate) Over (partition By CustomerId), orderdate as firstorderfrom orders ) as o--inner join orderdetails as d on o.orderid = d.orderid--inner join products as p on d.productid = p.productidwhere o.orderdate <= dateadd(mm, 6, o.firstorder) |
 |
|
|
mapidea
Posting Yak Master
124 Posts |
Posted - 2009-02-04 : 00:11:31
|
| Thanks Bklr.I have itselect o.customerid,p.productid,o.orderdate,o.firstorderfrom(Select orderid,orderdate,customerid,(Min(Orderdate) Over (partition By CustomerId)) as firstorderfrom orders)as oinner join orderdetails as d on o.orderid = d.orderidinner join products as p on d.productid = p.productidwhere o.orderdate <= dateadd(mm, 6, firstorder) |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-04 : 00:21:01
|
ur welcome |
 |
|
|
|
|
|
|
|