Need SQL Query Help from Gurus

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Need SQL Query Help from Gurus

    I have a table that stores ratings information (column called PPMSBRTotal)
    for vendors on a monthly basis. The ratings data is stored along with the
    date period it's relevant to (column called MonthYear) and other vendor
    data.

    I need to be produce a result set that calculates the average rating for
    each quarter as well as the annual average rating. So what I have come up
    with are the following queries:

    SELECT AVG(PPMSBRTotal) AS Q1_SBRTotal FROM CompanyRatings WHERE
    (DATEPART(mm, MonthYear) BETWEEN 1 AND 3) AND (DATEPART(yy, MonthYear) =
    2004) AND (VendorID = '#rsVendors.vendorID#')

    SELECT AVG(PPMSBRTotal) AS Q2_SBRTotal FROM CompanyRatings WHERE
    (DATEPART(mm, MonthYear) BETWEEN 4 AND 6) AND (DATEPART(yy, MonthYear) =
    2004) AND (VendorID = '#rsVendors.vendorID#')

    SELECT AVG(PPMSBRTotal) AS Q3_SBRTotal FROM CompanyRatings WHERE
    (DATEPART(mm, MonthYear) BETWEEN 7 AND 9) AND (DATEPART(yy, MonthYear) =
    2004) AND (VendorID = '#rsVendors.vendorID#')

    SELECT AVG(PPMSBRTotal) AS Q4_SBRTotal FROM CompanyRatings WHERE
    (DATEPART(mm, MonthYear) BETWEEN 10 AND 12) AND (DATEPART(yy, MonthYear) =
    2004) AND (VendorID = '#rsVendors.vendorID#')

    SELECT AVG(PPMSBRTotal) AS Annual_SBRTotal FROM CompanyRatings WHERE
    (DATEPART(mm, MonthYear) BETWEEN 1 AND 12) AND (DATEPART(yy, MonthYear) =
    2004) AND (VendorID = '#rsVendors.vendorID#')

    My problem is that I have to compile all of this data into one result set
    for all of the vendors so it can be output into an Excel spreadsheet.
    (Generating the Excel file is no trouble.)

    Currently, I have a loop setup that runs each one of the above queries and
    writes the output to a table cell for each vendor. That equates to 5 queries
    times 156 vendors = way too many seperate queries for the old dual PIII
    600MHz SQL server! It takes about 40 seconds to process and output into the
    Excel table.

    Is there a way to combine all of the db activity into one query or stored
    procedure?


    ctrl+alt+delete Guest

  2. Similar Questions and Discussions

    1. Calling PHP gurus
      It's nice to see all the support in this newsgroup. Hopefully I'll be a contributor soon. For now I'm hoping you could answer a question about...
    2. Question to all ASP GURUS
      Hi all Is there any way I can copy one folder from one place to another place over the internet.... Kind regards
    3. Hey layers gurus-need an eye here
      This is my first "real" attempt at using layers. www.billraydrums.com/7_19_03/new.htm Please take a look-see and give me some hints as to how...
    4. Any Javascript gurus out there?
      Hello, I'm not a javascript guru, and am having a few problems changing the font color between different items in a dropdown menu created in...
    5. Kudos to the gurus
      Simply wanted to say thanks in general to all the experienced people that post to this newsgroup. I am constantly amazed at the responsiveness of...
  3. #2

    Default Re: Need SQL Query Help from Gurus

    First, how often do you have to do this? 40 secs isn't that bad.

    Second, as a best practice you'd be better off compilting the querries in
    one stored proc. The proc can take parameters such as beginQtr and endQtr,
    forYear, forVendor.
    This can be run using a cf app where you'd still have the loop for each
    quarter and vendor.

    Lastly, read this article authored by Samuel Neff
    [url]http://www.macromedia.com/devnet/mx/coldfusion/articles/stored_procs.html[/url]

    "ctrl+alt+delete" <thenetwerx@REMOVEmsn.com> wrote in message
    news:cv5bvp$p18$1@forums.macromedia.com...
    >I have a table that stores ratings information (column called PPMSBRTotal)
    >for vendors on a monthly basis. The ratings data is stored along with the
    >date period it's relevant to (column called MonthYear) and other vendor
    >data.
    >
    > I need to be produce a result set that calculates the average rating for
    > each quarter as well as the annual average rating. So what I have come up
    > with are the following queries:
    >
    > SELECT AVG(PPMSBRTotal) AS Q1_SBRTotal FROM CompanyRatings WHERE
    > (DATEPART(mm, MonthYear) BETWEEN 1 AND 3) AND (DATEPART(yy, MonthYear) =
    > 2004) AND (VendorID = '#rsVendors.vendorID#')
    >
    > SELECT AVG(PPMSBRTotal) AS Q2_SBRTotal FROM CompanyRatings WHERE
    > (DATEPART(mm, MonthYear) BETWEEN 4 AND 6) AND (DATEPART(yy, MonthYear) =
    > 2004) AND (VendorID = '#rsVendors.vendorID#')
    >
    > SELECT AVG(PPMSBRTotal) AS Q3_SBRTotal FROM CompanyRatings WHERE
    > (DATEPART(mm, MonthYear) BETWEEN 7 AND 9) AND (DATEPART(yy, MonthYear) =
    > 2004) AND (VendorID = '#rsVendors.vendorID#')
    >
    > SELECT AVG(PPMSBRTotal) AS Q4_SBRTotal FROM CompanyRatings WHERE
    > (DATEPART(mm, MonthYear) BETWEEN 10 AND 12) AND (DATEPART(yy, MonthYear) =
    > 2004) AND (VendorID = '#rsVendors.vendorID#')
    >
    > SELECT AVG(PPMSBRTotal) AS Annual_SBRTotal FROM CompanyRatings WHERE
    > (DATEPART(mm, MonthYear) BETWEEN 1 AND 12) AND (DATEPART(yy, MonthYear) =
    > 2004) AND (VendorID = '#rsVendors.vendorID#')
    >
    > My problem is that I have to compile all of this data into one result set
    > for all of the vendors so it can be output into an Excel spreadsheet.
    > (Generating the Excel file is no trouble.)
    >
    > Currently, I have a loop setup that runs each one of the above queries and
    > writes the output to a table cell for each vendor. That equates to 5
    > queries times 156 vendors = way too many seperate queries for the old dual
    > PIII 600MHz SQL server! It takes about 40 seconds to process and output
    > into the Excel table.
    >
    > Is there a way to combine all of the db activity into one query or stored
    > procedure?
    >

    Bill Sahlas Guest

  4. #3

    Default Re: Need SQL Query Help from Gurus

    One more thing that's important to understand is that once a stored
    procedure is comile it has with it execution plan and statistics about how
    best to get the data. When you have this information storted at the db
    level then you performance improves dramatically.


    "Bill Sahlas" <bsahlas@macromedia.com> wrote in message
    news:cv5l4v$8e6$1@forums.macromedia.com...
    > First, how often do you have to do this? 40 secs isn't that bad.
    >
    > Second, as a best practice you'd be better off compilting the querries in
    > one stored proc. The proc can take parameters such as beginQtr and
    > endQtr, forYear, forVendor.
    > This can be run using a cf app where you'd still have the loop for each
    > quarter and vendor.
    >
    > Lastly, read this article authored by Samuel Neff
    > [url]http://www.macromedia.com/devnet/mx/coldfusion/articles/stored_procs.html[/url]
    >
    > "ctrl+alt+delete" <thenetwerx@REMOVEmsn.com> wrote in message
    > news:cv5bvp$p18$1@forums.macromedia.com...
    >>I have a table that stores ratings information (column called PPMSBRTotal)
    >>for vendors on a monthly basis. The ratings data is stored along with the
    >>date period it's relevant to (column called MonthYear) and other vendor
    >>data.
    >>
    >> I need to be produce a result set that calculates the average rating for
    >> each quarter as well as the annual average rating. So what I have come up
    >> with are the following queries:
    >>
    >> SELECT AVG(PPMSBRTotal) AS Q1_SBRTotal FROM CompanyRatings WHERE
    >> (DATEPART(mm, MonthYear) BETWEEN 1 AND 3) AND (DATEPART(yy, MonthYear) =
    >> 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>
    >> SELECT AVG(PPMSBRTotal) AS Q2_SBRTotal FROM CompanyRatings WHERE
    >> (DATEPART(mm, MonthYear) BETWEEN 4 AND 6) AND (DATEPART(yy, MonthYear) =
    >> 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>
    >> SELECT AVG(PPMSBRTotal) AS Q3_SBRTotal FROM CompanyRatings WHERE
    >> (DATEPART(mm, MonthYear) BETWEEN 7 AND 9) AND (DATEPART(yy, MonthYear) =
    >> 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>
    >> SELECT AVG(PPMSBRTotal) AS Q4_SBRTotal FROM CompanyRatings WHERE
    >> (DATEPART(mm, MonthYear) BETWEEN 10 AND 12) AND (DATEPART(yy, MonthYear)
    >> = 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>
    >> SELECT AVG(PPMSBRTotal) AS Annual_SBRTotal FROM CompanyRatings WHERE
    >> (DATEPART(mm, MonthYear) BETWEEN 1 AND 12) AND (DATEPART(yy, MonthYear) =
    >> 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>
    >> My problem is that I have to compile all of this data into one result set
    >> for all of the vendors so it can be output into an Excel spreadsheet.
    >> (Generating the Excel file is no trouble.)
    >>
    >> Currently, I have a loop setup that runs each one of the above queries
    >> and writes the output to a table cell for each vendor. That equates to 5
    >> queries times 156 vendors = way too many seperate queries for the old
    >> dual PIII 600MHz SQL server! It takes about 40 seconds to process and
    >> output into the Excel table.
    >>
    >> Is there a way to combine all of the db activity into one query or stored
    >> procedure?
    >>
    >
    >

    Bill Sahlas Guest

  5. #4

    Default Re: Need SQL Query Help from Gurus

    I came up with a better query and will roll it into a stored proc as well.

    What I did was add each of averages queries into the vendorID query (not
    shown in my earlier post) as sub-queries. Now SQL Server rips through it in
    less than a second and the web server doesn't have to loop over it anymore.

    "Bill Sahlas" <bsahlas@macromedia.com> wrote in message
    news:cv5lbo$8k8$1@forums.macromedia.com...
    > One more thing that's important to understand is that once a stored
    > procedure is comile it has with it execution plan and statistics about how
    > best to get the data. When you have this information storted at the db
    > level then you performance improves dramatically.
    >
    >
    > "Bill Sahlas" <bsahlas@macromedia.com> wrote in message
    > news:cv5l4v$8e6$1@forums.macromedia.com...
    >> First, how often do you have to do this? 40 secs isn't that bad.
    >>
    >> Second, as a best practice you'd be better off compilting the querries in
    >> one stored proc. The proc can take parameters such as beginQtr and
    >> endQtr, forYear, forVendor.
    >> This can be run using a cf app where you'd still have the loop for each
    >> quarter and vendor.
    >>
    >> Lastly, read this article authored by Samuel Neff
    >> [url]http://www.macromedia.com/devnet/mx/coldfusion/articles/stored_procs.html[/url]
    >>
    >> "ctrl+alt+delete" <thenetwerx@REMOVEmsn.com> wrote in message
    >> news:cv5bvp$p18$1@forums.macromedia.com...
    >>>I have a table that stores ratings information (column called
    >>>PPMSBRTotal) for vendors on a monthly basis. The ratings data is stored
    >>>along with the date period it's relevant to (column called MonthYear) and
    >>>other vendor data.
    >>>
    >>> I need to be produce a result set that calculates the average rating for
    >>> each quarter as well as the annual average rating. So what I have come
    >>> up with are the following queries:
    >>>
    >>> SELECT AVG(PPMSBRTotal) AS Q1_SBRTotal FROM CompanyRatings WHERE
    >>> (DATEPART(mm, MonthYear) BETWEEN 1 AND 3) AND (DATEPART(yy, MonthYear) =
    >>> 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>>
    >>> SELECT AVG(PPMSBRTotal) AS Q2_SBRTotal FROM CompanyRatings WHERE
    >>> (DATEPART(mm, MonthYear) BETWEEN 4 AND 6) AND (DATEPART(yy, MonthYear) =
    >>> 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>>
    >>> SELECT AVG(PPMSBRTotal) AS Q3_SBRTotal FROM CompanyRatings WHERE
    >>> (DATEPART(mm, MonthYear) BETWEEN 7 AND 9) AND (DATEPART(yy, MonthYear) =
    >>> 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>>
    >>> SELECT AVG(PPMSBRTotal) AS Q4_SBRTotal FROM CompanyRatings WHERE
    >>> (DATEPART(mm, MonthYear) BETWEEN 10 AND 12) AND (DATEPART(yy, MonthYear)
    >>> = 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>>
    >>> SELECT AVG(PPMSBRTotal) AS Annual_SBRTotal FROM CompanyRatings WHERE
    >>> (DATEPART(mm, MonthYear) BETWEEN 1 AND 12) AND (DATEPART(yy, MonthYear)
    >>> = 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>>
    >>> My problem is that I have to compile all of this data into one result
    >>> set for all of the vendors so it can be output into an Excel
    >>> spreadsheet. (Generating the Excel file is no trouble.)
    >>>
    >>> Currently, I have a loop setup that runs each one of the above queries
    >>> and writes the output to a table cell for each vendor. That equates to 5
    >>> queries times 156 vendors = way too many seperate queries for the old
    >>> dual PIII 600MHz SQL server! It takes about 40 seconds to process and
    >>> output into the Excel table.
    >>>
    >>> Is there a way to combine all of the db activity into one query or
    >>> stored procedure?
    >>>
    >>
    >>
    >
    >

    ctrl+alt+delete Guest

  6. #5

    Default Re: Need SQL Query Help from Gurus

    nice, glad you've got it going. good idea -

    "ctrl+alt+delete" <thenetwerx@REMOVEmsn.com> wrote in message
    news:cv5li1$90c$1@forums.macromedia.com...
    >I came up with a better query and will roll it into a stored proc as well.
    >
    > What I did was add each of averages queries into the vendorID query (not
    > shown in my earlier post) as sub-queries. Now SQL Server rips through it
    > in less than a second and the web server doesn't have to loop over it
    > anymore.
    >
    > "Bill Sahlas" <bsahlas@macromedia.com> wrote in message
    > news:cv5lbo$8k8$1@forums.macromedia.com...
    >> One more thing that's important to understand is that once a stored
    >> procedure is comile it has with it execution plan and statistics about
    >> how best to get the data. When you have this information storted at the
    >> db level then you performance improves dramatically.
    >>
    >>
    >> "Bill Sahlas" <bsahlas@macromedia.com> wrote in message
    >> news:cv5l4v$8e6$1@forums.macromedia.com...
    >>> First, how often do you have to do this? 40 secs isn't that bad.
    >>>
    >>> Second, as a best practice you'd be better off compilting the querries
    >>> in one stored proc. The proc can take parameters such as beginQtr and
    >>> endQtr, forYear, forVendor.
    >>> This can be run using a cf app where you'd still have the loop for each
    >>> quarter and vendor.
    >>>
    >>> Lastly, read this article authored by Samuel Neff
    >>> [url]http://www.macromedia.com/devnet/mx/coldfusion/articles/stored_procs.html[/url]
    >>>
    >>> "ctrl+alt+delete" <thenetwerx@REMOVEmsn.com> wrote in message
    >>> news:cv5bvp$p18$1@forums.macromedia.com...
    >>>>I have a table that stores ratings information (column called
    >>>>PPMSBRTotal) for vendors on a monthly basis. The ratings data is stored
    >>>>along with the date period it's relevant to (column called MonthYear)
    >>>>and other vendor data.
    >>>>
    >>>> I need to be produce a result set that calculates the average rating
    >>>> for each quarter as well as the annual average rating. So what I have
    >>>> come up with are the following queries:
    >>>>
    >>>> SELECT AVG(PPMSBRTotal) AS Q1_SBRTotal FROM CompanyRatings WHERE
    >>>> (DATEPART(mm, MonthYear) BETWEEN 1 AND 3) AND (DATEPART(yy, MonthYear)
    >>>> = 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>>>
    >>>> SELECT AVG(PPMSBRTotal) AS Q2_SBRTotal FROM CompanyRatings WHERE
    >>>> (DATEPART(mm, MonthYear) BETWEEN 4 AND 6) AND (DATEPART(yy, MonthYear)
    >>>> = 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>>>
    >>>> SELECT AVG(PPMSBRTotal) AS Q3_SBRTotal FROM CompanyRatings WHERE
    >>>> (DATEPART(mm, MonthYear) BETWEEN 7 AND 9) AND (DATEPART(yy, MonthYear)
    >>>> = 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>>>
    >>>> SELECT AVG(PPMSBRTotal) AS Q4_SBRTotal FROM CompanyRatings WHERE
    >>>> (DATEPART(mm, MonthYear) BETWEEN 10 AND 12) AND (DATEPART(yy,
    >>>> MonthYear) = 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>>>
    >>>> SELECT AVG(PPMSBRTotal) AS Annual_SBRTotal FROM CompanyRatings WHERE
    >>>> (DATEPART(mm, MonthYear) BETWEEN 1 AND 12) AND (DATEPART(yy, MonthYear)
    >>>> = 2004) AND (VendorID = '#rsVendors.vendorID#')
    >>>>
    >>>> My problem is that I have to compile all of this data into one result
    >>>> set for all of the vendors so it can be output into an Excel
    >>>> spreadsheet. (Generating the Excel file is no trouble.)
    >>>>
    >>>> Currently, I have a loop setup that runs each one of the above queries
    >>>> and writes the output to a table cell for each vendor. That equates to
    >>>> 5 queries times 156 vendors = way too many seperate queries for the old
    >>>> dual PIII 600MHz SQL server! It takes about 40 seconds to process and
    >>>> output into the Excel table.
    >>>>
    >>>> Is there a way to combine all of the db activity into one query or
    >>>> stored procedure?
    >>>>
    >>>
    >>>
    >>
    >>
    >
    >

    Bill Sahlas Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139