Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to display stores that haven't updated their merchandise inventory in x number of days.

The District Manager wants to enter a number (like 90 or 365) and get a list of all the stores AND the last inventory they ordered. So, if 90 stores haven't ordered anything in the last 90 days, the store name and all the items they last ordered need to be displayed.

If I use this:
WHERE MerchLastShippedDate = DATEADD("d", -@Days, CONVERT(VARCHAR(10),Getdate(),101)) 
my query runs in an ok amount of time. But, that isn't showing me everything I need. It's when I use the below to show me everything that is older than the number of days I enter that the query takes too long to run.

How can I correct this and make my query run faster?

Here is my code:
SQL
IF object_id('tempdb..#ActiveStores') is not null
BEGIN
    DROP TABLE #ActiveStores
END

SELECT DISTINCT
	Storename,
	StoreId
	INTO #ActiveStores
FROM 
	StoresTable
Where Storestatus = 1
	and Storename <> '  '
Order by
	StoreID


IF object_id('tempdb..#MerchDays') is not null
BEGIN
    DROP TABLE #MerchDays
END
  

Create Table #MerchDays
(StoreID varchar(100),
StoreName varchar(100),
MerchID varchar(50),
Manager  varchar(100),
MerchLastShippedDate datetime,
MerchNumber varchar(100),
QtyMerchShipped float,
AmtMerchShipped float

)

INSERT INTO #MerchDays
(StoreID, StoreName, MerchId, Manager, MerchLastShippedDate, MerchNumber, QtyMerchShipped, AmtMerchShipped)
SELECT StoreID, StoreName, MerchId, Manager, MerchLastShippedDate, MerchNumber, QtyMerchShipped, AmtMerchShipped
FROM MainStoredInventoryMerchTbl 
 
WHERE MerchLastShippedDate <= DATEADD("d", -@Days, CONVERT(VARCHAR(10),Getdate(),101)) 
 GROUP BY StoreID, StoreName, MerchId, Manager, MerchLastShippedDate, MerchNumber, QtyMerchShipped, AmtMerchShipped
ORDER BY StoreID

</<pre lang="SQL">

What I have tried:

MSDN, Google search, stackoverflow
Posted
Updated 23-Feb-17 11:44am
Comments
RossMW 23-Feb-17 17:52pm    
Dateadd works over date type fields why are converting it to a varchar.

Also one query has "=" and the other has "<=". If the MerchLastShippeddate is a datetime field, the chances of it equaling is very remote and probably why no records are returned
Member 10379103 23-Feb-17 18:02pm    
In this case, there are records returned when the "=" is used. That field is formatted like the other field. I need to capture everything that doesn't fall within those number of days.

1 solution

My SQL is a bit rusty but it looks like you are calculating a fixed date inside a loop instead of outside. ie: calculate once rather than for every record?
 
Share this answer
 
Comments
Valery Possoz 23-Feb-17 18:46pm    
That would help.
Also, temp tables are notoriously slow... Avoid if performance is a concern! maybe try replacing by variable tables or even better don't use temp tables at all.
In this quite simple piece of code, there does not seem to be any reason to use temp tables, you can get the same result directly.
Member 10379103 23-Feb-17 19:57pm    
I was instructed to use temp tables. Go figure, right?

How could the query be re-written without temp tables? Using a join?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900