August 18, 2008 at 9:40 am
Hello
I have a table that consist of order placed by customers which also includes the shipdate.
I want to write a query that pulls out records of cusotmer who have requested for next day delivery, can anyone help
August 18, 2008 at 9:47 am
You'll obviously have to replace with your column names....
SELECT
*
FROM
Orders O
WHERE
DATEDIFF(dd,O.Date_Order_Placed,O.Delivery_Date) <= 1
August 18, 2008 at 10:26 am
When a where clause has any function using column values as parameters, a comparison of the returned values to a constant disallows the usage of any indexing unless the table has a persistent computed column using that function that has been indexed.
Instead of
DATEDIFF(dd,O.Date_Order_Placed,O.Delivery_Date)
Recommend when there is an index on Date_Order_Placed:
O.Date_Order_Placed = DATEADD(dd,-1,O.Delivery_Date)
When there is an index on Delivery_Date:
O.Delivery_Date = DATEADD(dd,1,O.Date_Order_Placed)
Or have a compute column:
Alter table X
Add Order_Notice_Days as DATEDIFF(dd,Date_Order_Placed,Delivery_Date)
PERSISTENT
go
OR, if using 2008, use a filtered index
CREATE unique INDEX SalesOrderHeader_Open
on Sales.SalesOrderHeader
(Status, SalesOrderID)
where Status in (1,2,3)
SQL = Scarcely Qualifies as a Language
August 18, 2008 at 12:32 pm
Good point on the SARG.
As an afterthought, if Date_Order_Placed is a datetime and delivery date just a date it should be:
O.Delivery_Date <= DATEADD(dd,1,O.Date_Order_Placed)
August 18, 2008 at 1:20 pm
If both columns are datetimes containing only dates (time part = 00:00:00.000), then this will do it.
where O.Delivery_Date-O.Date_Order_Placed >= convert(datetime,'19000102')
August 19, 2008 at 4:12 am
And if i would like to find out orders placed by the a customer within a 7 day period will i use:
DATEDIFF(dd,T_OrderHeader.OrderDate,GETDATE()) BETWEEN 1 AND 7)
August 19, 2008 at 4:40 am
I'd use
O.Delivery_Date <= DATEADD(dd,7,O.Date_Order_Placed)
But both would work.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy