Top 10 items?

Top 10 items?

am 06.03.2006 23:22:55 von TRB_NV

I'm trying to write a little ASP routine to display the top 10 items sold.
Following is a sample database:

Sale #: Name: Quantity:
1 Vodka 2
2 Beer 3
3 Beer 1
4 Vodka 1

Rough sketch of what I want to do:

Do While Not MyRS.EOF
total(NAME) = total(Name) + MyRS("Quantity")
MyRS.MoveNext
Loop

The array values would look like this:
total(Vodka) = 3
total(Beer) = 4

Then if I sort it by most amount sold, I'd get
Beer = 4
Vodka = 3

My production database has over 10,000 sales in it and I'm trying to get the
top 10.

Re: Top 10 items?

am 07.03.2006 01:59:40 von reb01501

TRB_NV wrote:
> I'm trying to write a little ASP routine to display the top 10 items
> sold. Following is a sample database:
>
> Sale #: Name: Quantity:
> 1 Vodka 2
> 2 Beer 3
> 3 Beer 1
> 4 Vodka 1
>
> Rough sketch of what I want to do:
>
> Do While Not MyRS.EOF
> total(NAME) = total(Name) + MyRS("Quantity")
> MyRS.MoveNext
> Loop
>
> The array values would look like this:
> total(Vodka) = 3
> total(Beer) = 4
>
> Then if I sort it by most amount sold, I'd get
> Beer = 4
> Vodka = 3
>
> My production database has over 10,000 sales in it and I'm trying to
> get the top 10.

Never ask a database-related question without telling us the type and
version of database you are using.

If your database supports standard sql, you can do this with a query:

select name, sum(quantity) as TotalSold
from yourtable
group by name
order by sum(quantity) desc

And if your database supports the TOP keyword, you can do this:

select top 10 name, ...


HTH,
Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Top 10 items?

am 07.03.2006 08:16:35 von TRB_NV

Perfect!! Worked the first time.

The database I'm querying is in Access 2000, which my client uploads via FTP
to the webserver from his point of sale system.

Final code looks like this:

<%@ LANGUAGE="VBSCRIPT" %>

<%
Dim MyConn, MyRS, MySQL

response.write "

"
set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.ConnectionTimeout = 15
MyConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="&Server.MapPath("database.mdb")&""

MySQL = "SELECT ItemNum, DiffItemName, sum(quantity) as TotalSold FROM
Invoice_Itemized group by ItemNum, DiffItemName order by sum(quantity) desc"

Set MyRS = Server.CreateObject("ADODB.Recordset")
MyRS.Open MySQL, MyConn

for count = 1 to 10
response.write "" & vbCrLf
response.write "" & vbCrLf
response.write "" & vbCrLf
response.write "" & vbCrLf

MyRS.MoveNext
next

MyRS.close
set MyRS = nothing

MyConn.close
set MyConn = nothing
%>

"Bob Barrows [MVP]" wrote in message
news:%23EwDrJYQGHA.3924@TK2MSFTNGP14.phx.gbl...
> TRB_NV wrote:
>> I'm trying to write a little ASP routine to display the top 10 items
>> sold. Following is a sample database:
>>
>> Sale #: Name: Quantity:
>> 1 Vodka 2
>> 2 Beer 3
>> 3 Beer 1
>> 4 Vodka 1
>>
>> Rough sketch of what I want to do:
>>
>> Do While Not MyRS.EOF
>> total(NAME) = total(Name) + MyRS("Quantity")
>> MyRS.MoveNext
>> Loop
>>
>> The array values would look like this:
>> total(Vodka) = 3
>> total(Beer) = 4
>>
>> Then if I sort it by most amount sold, I'd get
>> Beer = 4
>> Vodka = 3
>>
>> My production database has over 10,000 sales in it and I'm trying to
>> get the top 10.
>
> Never ask a database-related question without telling us the type and
> version of database you are using.
>
> If your database supports standard sql, you can do this with a query:
>
> select name, sum(quantity) as TotalSold
> from yourtable
> group by name
> order by sum(quantity) desc
>
> And if your database supports the TOP keyword, you can do this:
>
> select top 10 name, ...
>
>
> HTH,
> Bob Barrows
>
> --
> Microsoft MVP - ASP/ASP.NET
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"
>

Re: Top 10 items?

am 07.03.2006 08:30:02 von TRB_NV

For the heck of it and because it's a little more elegant, I used the TOP
keyword. Thanks for your help. It's always nice to produce neat and clean
code. Following is the production code:

<%@ LANGUAGE="VBSCRIPT" %>

<%
Dim MyConn, MyRS, MySQL

response.write "

" & MyRS("DiffItemName") & "" & MyRS("TotalSold") & "
"
set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.ConnectionTimeout = 15
MyConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="&Server.MapPath("access_db/database.mdb")&""

MySQL = "SELECT TOP 10 ItemNum, DiffItemName, sum(quantity) as TotalSold
FROM Invoice_Itemized group by ItemNum, DiffItemName order by sum(quantity)
desc"

Set MyRS = Server.CreateObject("ADODB.Recordset")
MyRS.Open MySQL, MyConn

Do While Not MyRS.EOF
response.write "" & vbCrLf
response.write "" & vbCrLf
response.write "" & vbCrLf
response.write "" & vbCrLf

MyRS.MoveNext
Loop

MyRS.close
set MyRS = nothing

MyConn.close
set MyConn = nothing
%>

"Bob Barrows [MVP]" wrote in message
news:%23EwDrJYQGHA.3924@TK2MSFTNGP14.phx.gbl...
> TRB_NV wrote:
>> I'm trying to write a little ASP routine to display the top 10 items
>> sold. Following is a sample database:
>>
>> Sale #: Name: Quantity:
>> 1 Vodka 2
>> 2 Beer 3
>> 3 Beer 1
>> 4 Vodka 1
>>
>> Rough sketch of what I want to do:
>>
>> Do While Not MyRS.EOF
>> total(NAME) = total(Name) + MyRS("Quantity")
>> MyRS.MoveNext
>> Loop
>>
>> The array values would look like this:
>> total(Vodka) = 3
>> total(Beer) = 4
>>
>> Then if I sort it by most amount sold, I'd get
>> Beer = 4
>> Vodka = 3
>>
>> My production database has over 10,000 sales in it and I'm trying to
>> get the top 10.
>
> Never ask a database-related question without telling us the type and
> version of database you are using.
>
> If your database supports standard sql, you can do this with a query:
>
> select name, sum(quantity) as TotalSold
> from yourtable
> group by name
> order by sum(quantity) desc
>
> And if your database supports the TOP keyword, you can do this:
>
> select top 10 name, ...
>
>
> HTH,
> Bob Barrows
>
> --
> Microsoft MVP - ASP/ASP.NET
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"
>

Re: Top 10 items?

am 07.03.2006 12:56:28 von reb01501

TRB_NV wrote:
> For the heck of it and because it's a little more elegant, I used the
> TOP keyword. Thanks for your help. It's always nice to produce neat
> and clean code.

In that case, you might want to look into using GetRows instead of looping
through recordsets:
http://www.aspfaq.com/show.asp?id=2467

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Top 10 items?

am 08.03.2006 09:46:00 von Mike Brind

Bob Barrows [MVP] wrote:
> TRB_NV wrote:
> > For the heck of it and because it's a little more elegant, I used the
> > TOP keyword. Thanks for your help. It's always nice to produce neat
> > and clean code.
>
> In that case, you might want to look into using GetRows instead of looping
> through recordsets:
> http://www.aspfaq.com/show.asp?id=2467
>


And using the execute method of the connection object to create a
recordset:
http://www.aspfaq.com/show.asp?id=2191

:-)

--
Mike Brind

Re: Top 10 items?

am 08.03.2006 13:18:33 von reb01501

Mike Brind wrote:
> Bob Barrows [MVP] wrote:
>> TRB_NV wrote:
>>> For the heck of it and because it's a little more elegant, I used
>>> the TOP keyword. Thanks for your help. It's always nice to
>>> produce neat and clean code.
>>
>> In that case, you might want to look into using GetRows instead of
>> looping through recordsets:
>> http://www.aspfaq.com/show.asp?id=2467
>>
>
>
> And using the execute method of the connection object to create a
> recordset:
> http://www.aspfaq.com/show.asp?id=2191
>

Actually, I consider that to be somewhat irrelevant. The same exact things
occur behind the scenes (perhaps in a somewhat different order, but the
outcome is the same) whether one uses:

1)
Set MyRS = Server.CreateObject("ADODB.Recordset")
MyRS.Open MySQL, MyConn,,,1

or

2)
Set MyRS = MyConn.Execute(MySQL,,1)

Let's compare. In version 1:

1-a recordset object is instantiated
2-its Open method is called without setting the recordset's cursorlocation
property, causing it to remain set to the default (adUseServer, unless the
connection object used to open the recordset had its cursorlocation property
previously set to adUseClient)
3-Since no arguments were supplied for those parameters in the Open
statement, the recordset's cursortype and locktype properties remain set to
the default (this may depend on the OLE DB provider being used)
4-a Command object is instantiated
5-the Command object has its ActiveConnection property set to MyConn, its
CommandText property set to MySQL, and its CommandType property set to 1
(adCmdText).
6-the Command is executed, and the results of the execution are marshaled
into MyRS

In version 2:
1-the connection's Execute method is called
2-a Command object is instantiated
3-the Command object has its ActiveConnection property set to MyConn, its
CommandText property set to MySQL, and its CommandType property set to 1
(adCmdText).
4-the Command object is executed, and a recordset object with default
properties is created to receive the results
5-the recordset object in memory is assigned to MyRS

More important is knowing what type of cursor one is opening, as well as the
consequences of using that cursor type. E.G., some people fail to realize
the consequences of step 5 in version 2, instantiating their own recordset
object, assigning a bunch of non-default properties to the recordset,
calling Execute, and wondering why the resulting recordset does not have the
properties that were assigned to it before Execute was called.

If one wants to be in control of cursortype, etc., one is better off using
the Open method. Sure, the connection properties can be set, but this means
that all recordsets resulting from that connection will have those property
values.

Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

" & MyRS("DiffItemName") & "" & MyRS("TotalSold") & "