Solved

Daily,Weekly, Monthly Report

Posted on 2006-06-19
13
5,823 Views
Last Modified: 2012-06-27
Greetings,

I am trying to create a report from a T-SQL query that show the following:


Daily Report
Details                                                            

Date      Opened      Resolved                                                
1/3/2006      26      20                                                
1/4/2006      87      90                                                
1/5/2006      2      2                                                
1/6/2006      26      24                                                
1/7/2006      15      15                                                
1/8/2006      19      10                                                
1/9/2006      10      9                                                
1/10/2006      7      4      

Weekly Report
Week Ending      Opened      Resolved      
3/5/2006                      22      21      
3/12/2006                      64      50      
3/19/2006                      27            26      
3/26/2006                       50            30      
4/2/2006                       50            25      

Monthly
            
Month      Opened      Resolved      
February      150      145      
March      526      500      
April      89      88      
                              
All three of these reports are based on the entered(date) field and the closed(date) field in a table called calls.
0
Comment
Question by:Omega002
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 16935341
how do you determine opened/resolved?  you're going to have to provide some information on your table schema i left my crystal ball at home.
0
 

Author Comment

by:Omega002
ID: 16936039
the resolved field is just an alias of a count in the query. Plus I already gave you the primary fields to base this query on which is the entered field and the closed date fileds. Any other fields in this table are not necessary.
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 16936208
SELECT CONVERT(datetime, CONVERT(varchar, entered, 101), 101) AS [Date], ...
FROM sometable
GROUP BY CONVERT(datetime, CONVERT(varchar, entered, 101), 101)

SELECT DATEADD(day, 7 - DATEPART(weekday, entered), entered) AS [Week Ending], ...
FROM sometable
GROUP BY DATEPART(week, entered)

SELECT DATENAME(month, entered) AS [Month], ...
FROM sometable
GROUP BY MONTH(entered)
0
 
LVL 19

Expert Comment

by:folderol
ID: 16936295
T-SQL does not allow formatting of headers / footers, and to attempt it means you have to resort to tricks.  It's best to handle this in a higher apps, like Excel.

Your query can be

select
'Daily' as Section, Date, Opened, Resolved from Calls

union all

select
'Weekly' as Section,  dateadd(day, - datepart(day,Date) +1 , Date) as 'Week Ending',
Sum(Opened) as Opened, Sum(Resolved) as Resolved from Calls
Group By dateadd(day, - datepart(weekday,Date) +1 , Date)

union all

select
'Monthly' as Section,  dateadd(day, - datepart(day,Date) +1 , Date) as 'Month Starts On',
Sum(Opened) as Opened, Sum(Resolved) as Resolved from Calls
Group By dateadd(day, - datepart(day,Date) +1 , Date)

---
This supports sorting.  If you want the name of the month, it's datename(month,Date) but this conflicts with the UNION ALL, since the first two selects return a datetime as the column, and the last is varchar, plus you can't sort on datename.

Tom


0
 

Author Comment

by:Omega002
ID: 16992239
Have you given up on this topic?
0
 
LVL 19

Expert Comment

by:folderol
ID: 17010186
Do you use Excel?  Are you returning this query's results to an ASP page or something?

In Excel, you can use Data -> Pivottable and Pivotchart report -> External data source
to add the query using a wizard.  This will return the data to a pivottable wizard, which will allow you to format the data very similar to your example.

Let us know how it works out.

Tom.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Omega002
ID: 17010271
This report is used in SQL Server Reporting Services. I need assistance on building the query itself. Can you assist?
0
 
LVL 19

Expert Comment

by:folderol
ID: 17010646
Probably not.  I don't work in Report Services at the moment.  You can post back specific error msgs with the query and I can suggest a fix.
0
 

Author Comment

by:Omega002
ID: 17010675
What about just building queries? Can you assist in that area?
0
 
LVL 19

Expert Comment

by:folderol
ID: 17011478
What sort of assistance do you need?
0
 

Author Comment

by:Omega002
ID: 17012381
I am trying to create a report from a T-SQL query that will show the following:


Daily Report
Details                                                  

Date     Opened     Resolved                                        
1/3/2006     26     20                                        
1/4/2006     87     90                                        
1/5/2006     2     2                                        
1/6/2006     26     24                                        
1/7/2006     15     15                                        
1/8/2006     19     10                                        
1/9/2006     10     9                                        
1/10/2006     7     4    

Weekly Report
Week Ending     Opened     Resolved    
3/5/2006                     22     21    
3/12/2006                     64     50    
3/19/2006                     27            26    
3/26/2006                      50            30    
4/2/2006                      50            25    

Monthly
         
Month     Opened     Resolved    
February     150     145    
March     526     500    
April     89     88    
                         
All three of these reports are based on the entered(date) field and the closed(date) field in a table called ticket
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 17026485
SELECT
      T.Entered,
      SUM(1) AS Opened,
      SUM( CASE WHEN T.Entered = CONVERT(varchar(10),Ticket.Closed,101) THEN 1 ELSE 0  END) CLOSED
FROM Ticket
INNER JOIN ( SELECT DISTINCT CONVERT(varchar(10),Entered,101)Entered FROM Ticket ) T
ON T.Entered = CONVERT(varchar(10),Ticket.Entered,101)
GROUP BY T.Entered

SELECT
      T.Entered[Month],
      SUM(1) AS Opened,
      SUM( CASE WHEN T.Entered = DATENAME(mm,Ticket.Closed) THEN 1 ELSE 0  END) CLOSED
FROM Ticket
INNER JOIN ( SELECT DISTINCT  DATENAME(mm,Ticket.Entered)Entered FROM Ticket ) T
ON T.Entered = DATENAME(mm,Ticket.Entered)
GROUP BY T.Entered
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

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

707 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

18 Experts available now in Live!

Get 1:1 Help Now