Solved

Return Single Day of Week/Date for an annual query

Posted on 2013-01-03
9
552 Views
Last Modified: 2013-01-13
I am creating a query to return the total of checks by week Saturday-Friday.
Checks may have been written on several dates during the week.
I only want to return the date for Thursday each week.
This did not work but should illustrate what I’m trying to do.


set DateFirst 5 -- Friday

select
    datepart( ww, p.[CHECKDATE]) [Week]
  , sum(p.[INSURANCE]) [Insurance]
  , (select convert( varchar(10), p.[CHECKDATE], 1) where datepart( ww, p.[CHECKDATE]) = 4 ) [CheckDate]
from [PAYROLL] p
where p.[CHECKDATE] >= '01/01/2012'  
and p.[CHECKDATE] < '01/01/2013'
and p.[ INSURANCE] <> 0.00  
group by datepart( ww, p.[CHECKDATE]), p.[CHECKDATE]

set DateFirst 7 -- Always be sure to set it back
0
Comment
Question by:SurferJoe
9 Comments
 
LVL 22

Expert Comment

by:Steve Wales
Comment Utility
Based on this stackoverflow article:

http://stackoverflow.com/questions/3839923/get-date-of-all-saturdays-in-a-given-year-sql-server

The following query would return you all the Thursdays in 2012:

declare @d datetime
declare @d = '20120101'

select dateadd(dd,number,@d)
from master..spt_values
where type = 'P'
and year (dateadd(dd,number,@d) = year(@d)
and datepart(dw,dateadd(dd,number,@d)) = 5

Open in new window


So maybe you could have your base query for your checks and add a final part to it of something like

and CHECKDATE in (the select above)

You'd need to do a little date manipulation to truncate the dates to midnight of the dates returned only or convert to char with just the dates (which is horrible on indexing), but hopefully that gives you a good starting point.
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Not sure you need to add in a dates table in this instance as I believe you just want the dates on Thursday within your data. If I am correct, try the following.

set DateFirst 5 -- Friday

select
    DATEPART(ww, p.[CHECKDATE]) [Week]
  , SUM(p.[INSURANCE]) [Insurance]
  , p.[CHECKDATE] [CheckDate]
from [PAYROLL] p
where p.[CHECKDATE] >= '01/01/2012'  
and p.[CHECKDATE] < '01/01/2013'
and p.[ INSURANCE] <> 0.00
and DATEPART(ww, p.[CHECKDATE]) = 4
group by p.[CHECKDATE]

set DateFirst 7 -- Always be sure to set it back

Open in new window


The performance will suffer because of the function on the p.[CHECKDATE] column, but the above should do what you desire. You could try with the table of dates LEFT OUTER JOIN [PAYROLL] ON p.[CHECKDATE] = [Thursday Date] to see if it performs better as it probably will even though you have to introduce more code.
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
To circumvent the timestamp issue as mentioned by the expert above, I would typically do something like:

select dateadd(dd, number, '2012') as StartDT
     , dateadd(dd, number+1, '2012') as EndDT
from master..spt_values
where type = 'P'
and dateadd(dd, number, '2012') < '2013'
and datename(dw, dateadd(dd, number, '2012')) = 'Thursday'

Open in new window


Then in the JOIN, I would use:
ON p.[CHECKDATE] >= d.StartDT and p.[CHECKDATE] < d.EndDT

You could then group on d.StartDT to ensure you got every record for that day regardless of timestamp.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
It looks to me like your weeks really start on Saturday not Friday, so I guess don't fully understand in which week to add checks for each day.

If a check is written on Friday, does it count in the NEXT Thu's total or the PREVIOUS Thu's?

For example, should a check written on Fri, Dec 21 be included in Dec 20's total or Dec 27's?

If a check is written on Saturday, does it count in the NEXT Thu's total or the PREVIOUS Thu's?

For example, should a check written on Sat, Dec 22 be included in Dec 20's total or Dec 27's?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Author Comment

by:SurferJoe
Comment Utility
Hi Scott,

Our payroll period ends / journals on Thursday at midnight.
These other suggestions would drop the insurance amounts on checks not printed on Thursday.
I’m looking to accumulate all checks insurance for the journal week, only returning the journal date.
Typically things like this I would create a temp table with the weeks then, accumulate the totals on a second pass.
Just looking for a silver bullet. Nice to hear from you, thanx:) Greg
PS this is Obama care making work for us, at least it pays well:)
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Ah, that makes more sense. Then you can generate your dates like this:

select dateadd(dd, number-7, '2012') as StartDT
     , dateadd(dd, number, '2012') as EndDT
from master..spt_values
where type = 'P'
and dateadd(dd, number, '2012') < '2013'
and datename(dw, dateadd(dd, number, '2012')) = 'Friday'

Open in new window


This gives you the date for Friday at midnight as well as the previous week's Friday at midnight. You JOIN to your data as:
ON p.[CHECKDATE] >= d.StartDT and p.[CHECKDATE] < d.EndDT

This will give you all the insurance checks written after last Friday at midnight up to (but not including) this Friday at midnight.
0
 
LVL 4

Accepted Solution

by:
SurferJoe earned 0 total points
Comment Utility
No silver bullet, just good old  fashioned code.

use myDataBase
go

/****** Object:  StoredProcedure [dbo].[sp_pr_insurance_journal_amounts]    Script Date: 01/07/2013 23:34:43 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_pr_insurance_journal_amounts]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_pr_insurance_journal_amounts]
go

/*
  sp_pr_insurance_journal_amounts.sql

  11:22 PM 1/7/2013gr

*/


create proc [dbo].[sp_pr_insurance_journal_amounts] @dStartDate  as DateTime, @dEndDate as DateTime
as
begin

  declare @InsuranceWithHolding table
    (
        [Rec#] [int] IDENTITY (1, 1)
      , [Journal Date] datetime
      , [Insurance Total] decimal(10,2)

    )


  while ( @dStartDate <= @dEndDate) begin  

      if (datename(dw, @dStartDate ) = 'Thursday') begin

        insert into @InsuranceWithHolding (
            [Journal Date]
          , [Insurance Total])
   
          select
            convert (varchar( 12), @dStartDate, 1) -- [Journal Date]
            , sum([SECTION135CAFETERIAAMOUNT])     -- [Insurance]
          from [PAYROLL]
          where [CHECKDATE] >= @dStartDate - 6
          and [CHECKDATE] <= @dStartDate

      end


      set @dStartDate = @dStartDate + 1

  end

  select
      [Rec#]
    , convert( varchar(12), [Journal Date], 1) [Journal Date]  
    , [Insurance Total]
  from @InsuranceWithHolding

end
go

grant  execute  on [dbo].[sp_pr_insurance_journal_amounts] to [BlaBlaBla]
go
0
 
LVL 4

Author Comment

by:SurferJoe
Comment Utility
DECLARE @RC int
DECLARE @dStartDate datetime
DECLARE @dEndDate datetime

-- TODO: Set parameter values here.
set @dStartDate = '01/01/2012'
set @dEndDate = '12/31/2012'

EXECUTE @RC = [myDataBase].[dbo].[sp_pr_insurance_journal_amounts]
   @dStartDate
  ,@dEndDate
0
 
LVL 4

Author Closing Comment

by:SurferJoe
Comment Utility
It works
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now