Inline Query

  • This may be a stupid question but here it goes...

    I am writing a stored procedure and trying to do an inline query on the same table:

    Code:

    ALTER PROCEDURE

    djLink_sp_GetBrowserStats

    AS

    Declare

    @TotalBrowsers int

    Select

    @TotalBrowsers = Count(*) From djLink_tblBrowsers

    Select

    Distinct

    BrowserName,

    (

    Select Count(*) From djLink_tblBrowsers Where BrowserName = djLink_tblBrowsers.BrowserName) As Total,

    ((

    Select Count(*) From djLink_tblBrowsers Where BrowserName = BrowserName) / @TotalBrowsers * 100) As Percentage

    From

    djLink_tblBrowsers

    The problem is on this line (and the line preceeding):

    (Select Count(*) From djLink_tblBrowsers Where BrowserName = djLink_tblBrowsers.BrowserName) As Total

    Basically because I am doing the query in the same table it is getting confused and counting all rows, not just ones with the same browser name.

    Is there anyway to get around this?

    Thanks, Mart

  • You need to name the table and column that you want to count so that sql recognises it. Also, it is quicker to count the primary key than to count all, so should add some speed, too.


    ------------------------------
    The Users are always right - when I'm not wrong!

  • Try this:

    DECLARE @TotalBrowsers Int

    SET @TotalBrowsers = (Select Count(*) From djLink_tblBrowsers)

    Select

     BrowserName,

     COUNT(BrowserName) As Total,

     COUNT(BrowserName)* 100/ @TotalBrowsers As Percentage

    From djLink_tblBrowsers djB

    GROUP BY BrowserName


    Regards,

    Anders Dæmroen
    epsilon.no

  • Thanks loads, worked like a charm

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply