Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

T-SQL Aging style report

Posted on 2009-04-21
2
Medium Priority
?
431 Views
Last Modified: 2012-05-06
Hello all,

I am trying to write a simple aging style view in SQL Server Express 2005 and am running into issues.

I have a table with a list of 45k + people each person has what is called a trigger date. What need to do is create a view that would convert this table into a pivot table with 8 columns:
90+future | 60-90future | 30-60future | 0-30future | 0-30Past | 30-60past | 60-90past | 90+past

Each column would contain the count of records where the trigger date falls into the date range as realted to todays date.

I am currently using 8 views to get the count and know this is the wrong way to go about doing this so I want to make it correct.

Any help would be appreciated.
0
Comment
Question by:panhead802
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 24194407
this should do:
select sum(case when yourdatefield > dateadd(day, 90, getdate()) then 1 else 0 end) as [90+future]
, sum(case when yourdatefield <= dateadd(day, 90, getdate()) and yourdatefield > dateadd(day, 60, getdate()) then 1 else 0 end) as [60-90future]
, sum(case when yourdatefield <= dateadd(day, 60, getdate()) and yourdatefield > dateadd(day, 30, getdate()) then 1 else 0 end) as [30-60future]
, sum(case when yourdatefield <= dateadd(day, 30, getdate()) and yourdatefield > dateadd(day, 0, getdate()) then 1 else 0 end) as [0-30future]
, sum(case when yourdatefield <= dateadd(day, 0, getdate()) and yourdatefield > dateadd(day, -30, getdate()) then 1 else 0 end) as [0-30past]
, sum(case when yourdatefield <= dateadd(day, -30, getdate()) and yourdatefield > dateadd(day, -60, getdate()) then 1 else 0 end) as [30-60past]
, sum(case when yourdatefield <= dateadd(day, -60, getdate()) and yourdatefield > dateadd(day, -90, getdate()) then 1 else 0 end) as [60-90past]
, sum(case when yourdatefield <= dateadd(day, -90, getdate()) then 1 else 0 end) as [90+past]
from yourtable

Open in new window

0
 

Author Closing Comment

by:panhead802
ID: 31572724
Perfect thanks very much.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.
Suggested Courses

580 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