Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 7th, 2008, 10:39 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default VBScript or SQL solution to problem

Good day. What seemed like a small issue has me SO stuck. I have the following SQL:

SELECT DATENAME(MONTH,osirSignalDateTime) AS Expr1, COUNT(distinct osirReport.osirId) AS Numincidents, MONTH(osirSignalDateTime),YEAR(osirSignalDateTime) FROM ((((((((((((( osirReport.......

NOTE - Many tables joined here due to the query being feed from a bar chart wizard in one of my apps.

The sql output is as follows:

August 3 8 2007
September 1 9 2007
October 1 10 2007
November 4 11 2007
February 10 2 2008
March 3 3 2008
April 5 4 2008
May 4 5 2008
June 7 6 2008
July 7 7 2008
August 5 8 2008

FYI - The record set gets used in the following loop to feed the chart object:

   if not values.eof then
      do until values.eof
         Chart.AddGroupedData left(uCase(values(0)),3) & " " & right(values(3),2), "All Incidents", values(1), "FFFF00"
         values.moveNext
      loop
   end if

The Chart:
The user has asked for a chart which has months on the X-axis (first record in query). Each bar indicates numberOfIncidents (second record in query).

My Issue:
A date range of August 07 to August 08 was asked for however there were no incidents in the months of December 07 and January 08 (as you can see in the output above). How can I make the record set include these months indicating a 0 for the number of incidents?? A bar with 0 is required on the graph for any month within the date range that has no incidents.

My objective is to have these two records included in the above record set:

December 0 12 2007
January 0 1 2008

I thought the ISNULL function should do this. EG:

SELECT DATENAME(MONTH,osirSignalDateTime) AS Expr1, isNull(COUNT(distinct osirReport.osirId),0) AS Numincidents, MONTH(osirSignalDateTime),YEAR(osirSignalDateTime) FROM....

This makes no difference to the output. Should I be looking to solve this while looping through the query instead of tring to tweak the SQL??

TYIA



Wind is your friend
Matt
www.elitemarquees.com.au
__________________
Wind is your friend
Matt
Reply With Quote
  #2 (permalink)  
Old October 7th, 2008, 11:14 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

I would much prefer to tweak the SQL rather than doing it while loopinf through the record set!! This is a stacked bar chart so the code below would have to be repeated three times...messy....

Anyhow this is what I was geting at when talking about solving it outside of the SQL. it works however far from a sooth solution:

NOTE - I have hard coded the start and finish dates for testing

    dim startMNum,finishMNum,startYear,finishYear,fromVar, tovar,runningVar,numMonths
    dim count
    numMonths = 0
    startMNum = month("01/08/2007")
    finishMNum = month("31/08/2008")
    startYear = year("01/08/2007")
    finishYear = year("31/08/2008")
    fromVar = startYear * 100 + startMnum
    toVar = finishYear * 100 + finishMnum
    runningVar = fromvar
    do while runningVar <= toVar
       numMonths = numMonths + 1
       if runningVar = startYear * 100 + 12 then
          runningVar = (startYear + 1) *100 +1
       else
          runningVar = runningVar + 1
       end if
    loop

    dim runningMonth,runningYear
    runningMonth = startMNum
    runningYear = startYear
    count = 0


        for count = 1 to numMonths
        if not values.Eof then
           if values(2) = runningMonth AND values(3) = runningYear then
              Chart.AddGroupedData left(uCase(values(0)),3) & " " & right(values(3),2), "All Incidents",values(1) , "FFFF00"
              values.moveNext
           else
              Chart.AddGroupedData left(uCase(monthName(runningMonth,false)),3) & " " & right(runningYear,2), "All Incidents", 0 , "FFFF00"
           end if
        end if
        if runningMonth = 12 then
           runningMonth = 1
           runningYear = runningYear + 1
        else
           runningMonth = runningMonth + 1
        end if
     next

Wind is your friend
Matt
www.elitemarquees.com.au
Reply With Quote
  #3 (permalink)  
Old October 8th, 2008, 03:55 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

The easy way to do this is to create an ALLMONTHS table in your DB.

That table has just one field: monthStart (with a type of DATETIME, of course)

And that table looks something like this:
Code:
monthStart (showing DD/MM/YYYY format for Aussie convenience)
1/1/2007
1/2/2007
1/3/2007
...
1/12/2007
1/1/2008
...
1/12/2008
...
1/12/2030
And now you simply LEFT JOIN from that table to your existing query:
Code:
SELECT Monthname(allmonths.monthStart), ...other fields...
FROM allmonths LEFT JOIN ( other tables all joined together )
ON Month(allmonths) = Month(osirSignalDateTime) AND Year(allmonths) = Year(osirSignalDateTime)
WHERE allmonths.monthStart BETWEEN '1/8/2007' AND '1/8/2008'
ORDER BY allmonths.monthStart
Presto. You are done.

What DB is this???

Why aren't you doing this (or the equivalent) in the SQL:
    left(uCase(values(0)),3) & " " & right(values(3),2)
???
Reply With Quote
  #4 (permalink)  
Old October 8th, 2008, 04:04 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Here's a simpler form of the VBS solution:
Code:
SQL = "SELECT YEAR(osirSignalDateTime) AS yr, MONTH(osirSignalDateTime) AS mn, COUNT(distinct osirReport.osirId) AS Numincidents FROM ..."
Set rs = conn.Execute( SQL )

curMonth = #2007/7/1#
Do While curMonth <= #2008/7/1#
    dbYr = RS("yr")
    dbMn = RS("mn")
    count = 0
    If Not RS.EOF Then
        If dbYr = Year(curMonth) AND dbMn = Month(curMonth) Then 
            count = RS("Numincidents")
            RS.MoveNext
        End If
    End If
    mName = UCase( Monthname(Month(curMonth),True) & " " & Right(CStr(Year(curMonth)),2)
    Chart.AddGroupedData mName, "All Incidents", count, "FFFF00"
    curMonth = DateAdd("m", 1, curMonth)
Loop
Reply With Quote
  #5 (permalink)  
Old October 8th, 2008, 04:46 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Ah I see this months table would ensure null values are returned for the months missing incidents. EG:

isNull(COUNT(distinct osirReport.osirId),0) AS Numincidents

;;;What DB is this???
SQL Server

;;;Why aren't you doing this (or the equivalent) in the SQL
The output illustrated above:
August 3 8 2007
September 1 9 2007
is requird for other reasons on the page therefore I manupulate the RS values accordingly.

Thank you very much for your time. I think I will try your first option. Have a good day :o)

Wind is your friend
Matt
www.elitemarquees.com.au
Reply With Quote
  #6 (permalink)  
Old October 8th, 2008, 05:13 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Since you are using SQL Server, I'd be tempted to take your existing query and turn it into a VIEW.

Then you can just LEFT JOIN from the allmonths table to that VIEW.

The reason I recommend that is because you can't use any field from a table where records might not exist in the WHERE clause (it nullifies the OUTER join and turns it into an INNER join).

I will say that for this particular case, where you only need a max of 13 records, I don't think the VBS solution is all that bad.

Now, if you were doing a calendar...
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting an average using SQL vs VBScript mat41 Classic ASP Professional 1 November 1st, 2007 06:33 PM
XML from SQL through VBScript raj_alapati XML 10 May 30th, 2007 03:35 AM
vbscript version of SQL 'SELECT DISTINCT' SoC Classic ASP Basics 1 February 8th, 2006 03:16 AM
ConnectionString Problem when load Solution bekim BOOK: ASP.NET Website Programming Problem-Design-Solution 12 July 6th, 2004 10:29 AM



All times are GMT -4. The time now is 09:51 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.