Search 85,791 posts and 651 resources contributed by 43,443 members or post a topic.

Already Joined? Sign in
Report Writer - Custom SQL

Page 1 of 2 (20 items) 1 2 Next > | RSS

rated by 0 users
Answered (Not Verified) This post has 0 verified answers | 19 Replies | 5 Followers | 2,131 Views


22 Posts
Points 58
pchaves posted on Wed, Jan 7 2009 3:40 PM
rated by 0 users

I'm looking for some help with a custom sql report I'm trying to setup.

I wanted to run my custom sql report on the 1st of every month and have it compile data from the previous month (or even previous 30 days). Does anyone happen to know how I could call out the previous month in the code?

I know there is an option for this in the basic option but I had to customize a few things for this report.

Thanks in advance! 

  • | Post Points: 5

All Replies


2,318 Posts
Points 7,773
Moderator
SolarWinds Employee
Mark Wiggans replied on Wed, Jan 7 2009 3:49 PM
rated by 0 users

Here is one from the Content Sharing Zone that addresses the last 30 days...

 

http://thwack.com/media/p/28301.aspx

 

Mark Wiggans Information Development-

  • | Post Points: 3

10 Posts
Points 500
robguent replied on Thu, Jan 8 2009 5:13 AM
rated by 0 users

Here is a good source for the different time frame selects:

http://databaseadvisors.com/pipermail/dba-sqlserver/2003-September/001109.html

***snippet of my SQL report, which calculates 2hrs Utilizations for the Last Month

-- Reporting time frame - Last Month
-- ReportPeriod specifies for which month  the report should be generated, 1 = Last Month, 2 = Month before Last Month
DECLARE @ReportPeriod int
     Set @ReportPeriod=1

SELECT 
 
Bandwidth='1.) Utilization/120min >= 80% for Bandwidth <=2Mbit/s', 
A_BWpre.Region,
A_BWpre.SPID,
A_BWpre.NodeName,
A_BWpre.VRF,
A_BWpre.Recv_Bandwidth,
A_BWpre.SummaryDateTime,
A_BWpre.AVERAGE_of_Recv_Percent_Utilization
 
FROM
 
 (SELECT
 
  Nodes.Caption AS NodeName,
  Interfaces.VRF AS VRF,
  AVG(Case InBandwidth When 0 Then 0
   Else (In_Averagebps/InBandwidth) * 100
   End) AS AVERAGE_of_Recv_Percent_Utilization,
  Nodes.Region AS Region,
  Nodes.SPID AS SPID,
  Interfaces.InBandwidth AS Recv_Bandwidth,
  ---- Average over 2hrs ....*12)/12
  Convert(DateTime,Floor(Cast(DateTime as Float)*12)/12,0) AS SummaryDateTime
 
 FROM
  (Nodes INNER JOIN Interfaces ON (Nodes.NodeID = Interfaces.NodeID))
  INNER JOIN InterfaceTraffic ON (Interfaces.InterfaceID = InterfaceTraffic.InterfaceID)
 
 WHERE
  -- Last Month
  DatePart(m, DateTime) = DatePart(m, DateAdd(m, -@ReportPeriod, getdate())) AND
  DatePart(yyyy, DateTime) = DatePart(yyyy, DateAdd(m,
-@ReportPeriod, getdate()))

  • | Post Points: 1

10 Posts
Points 500
Answered (Not Verified) robguent replied on Thu, Jan 8 2009 5:15 AM
rated by 0 users
Suggested by Mark Wiggans

Here an example for Last XX days

 

--------- TRANSMIT ---------------

--VAR Definition

-- Reporting time frame - Last XX days
-- ReportPeriod specifies for how many days back the report should be generated
DECLARE @ReportPeriod int
     Set @ReportPeriod=7

SELECT 
 
Bandwidth='1.) Utilization/120min >= 80% for Bandwidth <=2Mbit/s', 
A_BWpre.Region,
A_BWpre.SPID,
A_BWpre.NodeName,
A_BWpre.VRF,
A_BWpre.Xmit_Bandwidth,
A_BWpre.SummaryDateTime,
A_BWpre.AVERAGE_of_Xmit_Percent_Utilization
 
FROM
 
 (SELECT
 
  Nodes.Caption AS NodeName,
  Interfaces.VRF AS VRF,
  AVG(Case OutBandwidth When 0 Then 0
   Else (Out_Averagebps/OutBandwidth) * 100
   End) AS AVERAGE_of_Xmit_Percent_Utilization,
  Nodes.Region AS Region,
  Nodes.SPID AS SPID,
  Interfaces.OutBandwidth AS Xmit_Bandwidth,
  ---- Average over 2hrs ....*12)/12
  Convert(DateTime,Floor(Cast(DateTime as Float)*12)/12,0) AS SummaryDateTime

 
 FROM
  (Nodes INNER JOIN Interfaces ON (Nodes.NodeID = Interfaces.NodeID))
  INNER JOIN InterfaceTraffic ON (Interfaces.InterfaceID = InterfaceTraffic.InterfaceID)
 
 WHERE
  -- last XX days
  ( DateTime >= DATEADD(DAY,DATEDIFF(DAY,0,GETDATE())- @ReportPeriod,0)  ) AND
  ( DateTime < DATEADD(DAY,DATEDIFF(DAY,0,GETDATE()),0)   ) AND
 

  • Post Points: 3

407 Posts
Points 12,429
SolarWinds Certified Professional
Thwack MVP
savell replied on Thu, Jan 8 2009 8:17 AM
rated by 0 users

Here's an SQL where statement I use for some of our monthly reports (returns date range for previous month)..

WHERE
( DateTime >= dateadd(mm,datediff(mm,0,getdate())-1,0) AND DateTime
<dateadd(mm,datediff(mm,0,getdate()),0) )

Dave.

Edit: very similar to the method in the above post now I look at it!

  • | Post Points: 1

22 Posts
Points 58
pchaves replied on Thu, Jan 8 2009 8:56 AM
rated by 0 users

Thanks Everyone for your help. I was able to get this working...finally.

 

THANKS AGAIN!

  • | Post Points: 3

136 Posts
Points 449
SolarWinds Certified Professional
cnorborg replied on Thu, Jan 8 2009 2:50 PM
rated by 0 users

I hate replying to a post that is already solved, but I didn't see the method I use to try and figure out how the Solarwinds guys accomplish things in reports.

For example, in this case you want to know how to do a report for the previous month.  There are a ton of Orion reports that are for "Last Month" already and they work quite well, wouldn't it be great to know how they did it?  You can quite easily!!

Just open any report in Report Writer that has the feature you want in it, in this case one of the reports that has a timeframe of "Last Month".   Lets say the Availability report "Availability: Last Month" for instance.   Once you have the report opened, go to the "Report" menu and choose "Show SQL".  

At this point, another tab will open in the report you were just looking at that will show the exact SQL that is used to produce that report.

Makes it easy to learn off the professionals, a great way to get ideas on how to accomplish something!!

Craig Norborg Senior Network Analyst Robert Bosch Corporation

  • | Post Points: 3

407 Posts
Points 12,429
SolarWinds Certified Professional
Thwack MVP
savell replied on Thu, Jan 8 2009 5:03 PM
rated by 0 users

It's a good tip and something I also use. However it's unfortunately not quite that simple in some instances.

Have a look at how the SQL looks when Report Writer generates the report - any datetime ranges are generated at the time the report is run (i.e. an absolute date range is generated rather than a relative one).

So you end up with something like the following for yesterday's data....

WHERE
( DateTime BETWEEN 39819 AND 39820 )

This is fine if the SQL is run today - but if the same statement is run tomorrow, you may be disappointed with the result...

Dave.

  • | Post Points: 3

335 Posts
Points 894
rgward replied on Thu, Jan 8 2009 8:22 PM
rated by 0 users

Try these.  I got them from Support a while back.

Replace the generated absolute statement like...

WHERE
( DateTime BETWEEN 39819 AND 39820 ) 

with...

For 'Last 24 Hours':

WHERE ColumnName >= GetDate () -1

For 'Last 7 Days':

WHERE DateTime >= GetDate () -7

For 'Last 30 Days':

WHERE DateTime >= GetDate () -30

For 'Last Month':

WHERE MONTH(ColumnName) = (MONTH(GETDATE()-DAY(GETDATE())))
AND YEAR(ColumnName) = YEAR(GETDATE()-DAY(GETDATE()))

For 'This Month':

WHERE MONTH(ColumnName) = (MONTH(GETDATE())) AND YEAR(ColumnName) = YEAR(GETDATE())


Replace 'ColumnName' with the name of the column that contains the DateTime information.  In most cases, it is DateTime, but some may vary.  You would want to check the table columns to get the appropriate column name.

(1) Orion v9.1 SP5 SLX running Web Site
(2) Orion v9.1 SP5 SLX polling engine
(1) Orion v9.1 SP5 SLX Hot-Standby
(1) MS SQL2000 EE
APM v2.5 ALX
VoIP Monitor v2 SP3
Wireless Network Monitor v8
IPAM v1.5 IPX

(1) Orion NPM v9.5.1 SLX running Web Site (Dev)
(1) APM 3.1 ALX (Dev)
(1) IPSLA 3.0 SLAX (Dev)
(1) NCM v5 DL500 (Dev)
(1) Lansurveyor v10 (Dev)

  • | Post Points: 3

22 Posts
Points 58
pchaves replied on Fri, Jan 9 2009 12:36 PM
rated by 0 users

Thanks for the suggestions but looks like I'm on another stumbling block...

I am trying to pull data from the previous month but only during the hours of 8am to 5pm, Monday thru Friday.

Here is what I have but no matter what I put in the DatePart section I get the same results.

________________________________________________________________

WHERE
DateTime between  (DATEADD(m, DATEDIFF(m, 0, getdate()) -1 , 0)) AND (DATEADD(m, DATEDIFF(m, 0, getdate()) , 0))
 AND 
(
  (Interfaces.Caption LIKE '%Qwest%') OR
  (Interfaces.Caption LIKE '%COX-20MB%') OR
  (Interfaces.Caption LIKE '%One-Communications%') OR
  (Interfaces.Caption LIKE '%Verizon-ISP%')

 AND
(
   (DatePart(Hour,DateTime) >= 8) AND
   (DatePart(Hour,DateTime) <= 5) AND
   (DATEPART(weekday, DateTime) <> 1) AND
   (DATEPART(weekday, DateTime) = 7))
)

_________________________________________________________________

Anyone have any suggestions?

Thanks.

 

  • | Post Points: 3

250 Posts
Points 1,531
Alert Guru
sedmo replied on Fri, Jan 9 2009 2:50 PM
rated by 0 users

I think the last line of your query should say <> 7 instead of = 7.

  • | Post Points: 3

22 Posts
Points 58
pchaves replied on Fri, Jan 9 2009 3:14 PM
rated by 0 users

Yes, you are right. I made the change and it didn't make a difference.... It seems like the entire DATEPART section is being ignored. If I remove it the results are the same.

  • | Post Points: 3

250 Posts
Points 1,531
Alert Guru
sedmo replied on Fri, Jan 9 2009 4:33 PM
rated by 0 users

Here is how I have specified this sort of thing in my queries.

 

          (DATEPART(weekday, DateTime) <> 7)AND

          (DATEPART(weekday, DateTime) <> 1) AND

          (CONVERT(Char, DateTime, 108) > '0800') AND

          (CONVERT(Char, DateTime, 108) < '1700')

  • | Post Points: 3

22 Posts
Points 58
pchaves replied on Tue, Jan 13 2009 10:07 AM
rated by 0 users

Hi Sedmo,

Thanks for the suggestion, I seem to be getting different results with this but I don't believe it's correct.

What does the "108" relate to in the Convert lines?

Thanks

  • | Post Points: 3

250 Posts
Points 1,531
Alert Guru
sedmo replied on Tue, Jan 13 2009 10:30 AM
rated by 0 users

108 is a type code that determines the format of the time.  In this case it returns HH:MM:SS.  Would it be possible for you to post your complete query?

  • | Post Points: 3
Page 1 of 2 (20 items) 1 2 Next > | RSS

© 2003 - 2010 SolarWinds, Inc. All Rights Reserved.

Who is SolarWinds?

SolarWinds is rewriting the rules for how companies manage their networks. Guided by a global community of network engineers, SolarWinds develops simple and powerful network management software and network monitoring software for networks of all sizes. SolarWinds also offers a network certification program to become a SolarWinds Certified Professional (SCP).

What is thwack?

thwack, SolarWinds online community site, was designed by network engineers, for network engineers. thwack is a vibrant, growing community of more than 30,000 IT pros who share a passion for technology.

Explore Resources, Answers, Templates, and Advice

Download Free Networking Tools


Learn More About SolarWinds Products