Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8659717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:04:45+00:00 2026-06-12T16:04:45+00:00

I have a SQL query that takes a date and reports number of pallets

  • 0

I have a SQL query that takes a date and reports number of pallets that have been scanned into a depot over the last 7 days, but not scanned out by the given date. It takes about a second to run. I need to report this number (using an SSRS report) for the last 14 days. However – when I create either a CTE or a temporary table to generate the dates to report on, instead of rising to 10 seconds (for about 10 dates), it rises to over 3 minutes!

Please advise.

Here’s the single date report:

DECLARE @back integer
SET @back = 7
Declare @testDate DateTime
SET @testDate = '2012-10-09'
--opening balance query
select count(p.PalletID) as Opening_Balance
FROM [POSTALE].[dbo].[Pallet] p
join [POSTALE].[dbo].[Tracking] t on t.PalletID = p.PalletID
--scan was before indicated day
where CAST(DateAdd(hour, -6, t.TrackDateTime) as DATE) <= @testDate
--scan was after number of days to look back
and CAST(DateAdd(hour, -6, t.TrackDateTime) as DATE) >= dateadd(day, 0 - @back, @testDate)
--scanned into hub (get only the earliest occurence)
and t.TrackingID = (select top 1 tr.TrackingID from [POSTALE].[dbo].[Tracking] tr 
                where tr.TrackCode in ('SCAN OFF TRUNK HUB', 'DAYS') 
                and tr.PalletID = t.PalletID order by tr.TrackingID)
--not scanned out of hub by the indicated day
and (select count(tr.TrackingID) from [POSTALE].[dbo].[Tracking] tr where t.PalletID = tr.PalletID and 
    CAST(DateAdd(hour, -6, tr.TrackDateTime) as DATE) <= @testDate and tr.TrackCode in 
    ('SCAN ONTO TRUNK DEPOT', 'SCAN OFF TRUNK DEPOT', 'SECURITY SCAN AT DEPOT', 'SCAN OFF TRUNK DEPOT', 'SCANNER BROKEN',
    'SCAN ON DEPOT VEHICLE', 'POD ADDED','NO POD ADDED')) = 0

Here’s the code to join it with a CTE:

DECLARE @back integer
SET @back = 7
declare @start_date as date;
set @start_date = cast(getdate() - 14 as date);
declare @end_date as date;
set @end_date = cast(getdate() as date);
WITH DateList AS
(
    SELECT @start_date AS start_date
    UNION ALL
    SELECT DATEADD(DAY, 1, start_date)
    FROM DateList
    WHERE DATEADD(DAY, 1, start_date) < @end_date
)
SELECT
    (select count(p.PalletID) as Opening_Balance
...
,datediff(DAY, d.start_date, getdate()) as daysBack
from DateList d
order by d.start_date

..and here’s the temp table version:

DECLARE @back integer
SET @back = 7
--hours into day that get pushed back to previous day
DECLARE @dayStart integer
set @dayStart = 12
--today + 12 hours as datetime
Declare @date12 as datetime
set @date12 = DateAdd(hour, @dayStart, cast(cast(getdate() as date) as datetime))
--temporary table to create list of dates to report on
DECLARE @DateList TABLE(
        DayDate DateTime
)
insert into @DateList (DayDate) Select DateAdd(day, - 14, @date12) where datepart(weekday, getdate() - 14) <> 7 and datepart(weekday, getdate() - 14) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 13, @date12) where datepart(weekday, getdate() - 13) <> 7 and datepart(weekday, getdate() - 13) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 12, @date12) where datepart(weekday, getdate() - 12) <> 7 and datepart(weekday, getdate() - 12) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 11, @date12) where datepart(weekday, getdate() - 11) <> 7 and datepart(weekday, getdate() - 11) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 10, @date12) where datepart(weekday, getdate() - 10) <> 7 and datepart(weekday, getdate() - 10) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 9, @date12) where datepart(weekday, getdate() - 9) <> 7 and datepart(weekday, getdate() - 9) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 8, @date12) where datepart(weekday, getdate() - 8) <> 7 and datepart(weekday, getdate() - 8) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 7, @date12) where datepart(weekday, getdate() - 7) <> 7 and datepart(weekday, getdate() - 7) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 6, @date12) where datepart(weekday, getdate() - 6) <> 7 and datepart(weekday, getdate() - 6) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 5, @date12) where datepart(weekday, getdate() - 5) <> 7 and datepart(weekday, getdate() - 5) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 4, @date12) where datepart(weekday, getdate() - 4) <> 7 and datepart(weekday, getdate() - 4) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 3, @date12) where datepart(weekday, getdate() - 3) <> 7 and datepart(weekday, getdate() - 3) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 2, @date12) where datepart(weekday, getdate() - 2) <> 7 and datepart(weekday, getdate() - 2) <> 1
insert into @DateList (DayDate) Select DateAdd(day, - 1, @date12) where datepart(weekday, getdate() - 1) <> 7 and datepart(weekday, getdate() - 1) <> 1
SELECT
    (select count(p.PalletID) as Opening_Balance
...
,datediff(DAY, d.DayDate, getdate()) as daysBack
from @DateList d
order by d.DayDate
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:04:46+00:00Added an answer on June 12, 2026 at 4:04 pm

    You haven’t described your indexing which could obviously be a huge factor, but one thing I noticed is that your WHERE clause has elements that are not sargable and could be.

    For example, try changing:

    where CAST(DateAdd(hour, -6, t.TrackDateTime) as DATE) <= @testDate 
    

    to

    where t.TrackDateTime < dateadd(hour, 6, dateadd(day, 1, @testDate))
    

    You’ll need to do a similar thing with this line:

    and CAST(DateAdd(hour, -6, t.TrackDateTime) as DATE) >= dateadd(day, 0 - @back, @testDate)  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL query (below) that essentially takes a student from tbStudents, and
I have an SQL Query (For SQL Server 2008 R2) that takes a very
I currently have a SQL query that returns a number of fields. I need
I have an sql query with inner joins of four tables that takes more
I have following sql query that take only 1 second to execute: select a.date,
I have this SQL Query that pulls data from 3 tables. I am unable
I have a SQL query that returns a Datatable: var routesTable = _dbhelper.Select(SELECT [RouteId],[UserId],[SourceName],[CreationTime]
I have a SQL query that I'm trying to debug. It works fine for
I have an SQL query that returns all companies records ignore the ones with
I have a SQL query that I'm trying to write, but I'm not quite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.