August 12, 2004 at 5:24 am
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:
|
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
August 12, 2004 at 7:01 am
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.
August 12, 2004 at 8:17 am
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
Anders Dæmroen
epsilon.no
August 12, 2004 at 10:05 am
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