ASP Classic - Count for Report

I need to do an audit of my inventory.  I want to write a report that shows how much is in each box (b408 used in the example below). That will give us an idea where there is free space to add new inventory to.

I know how to do a quick check of what is in each box, like below:
conn.open "SELECT * FROM [DAL.ProductContext].[dbo].[Products] WHERE QuantityAvailable > '0' AND (Location LIKE 'b408%') ORDER BY Created ASC;", "DSN=dashboard"
do while not conn.eof
quantitycounter = quantitycounter+ 1
Location = 	conn("Location")
conn.moveNext
loop
<% = (TotalQuantity) %> Items / <% = (quantitycounter) %> SKUs

Open in new window

But if I run through that same code for each box of inventory, on the same ASP page, the code is going to get really messy, and LONG.

Can one of the experts point me in a better direction to start?
LVL 1
bschwartingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DBAduck - Ben MillerPrincipal ConsultantCommented:
Couldn't you just run a query that has a Group By for Location and then just iterate over the entire list one time instead of switching on specific locations?

SELECT SUM(QuantityAvailable) as QuantityAvailable, Location
FROM [DAL.ProductContext].dbo.[Products]
WHERE QuantityAvailable > 0
GROUP BY Location

Open in new window

bschwartingAuthor Commented:
How would I see the results of each location group?
DBAduck - Ben MillerPrincipal ConsultantCommented:
You would order by Location or not, but you would iterate with them.  Your code wasn't really clear about where you get the values because you basically increment quantityavailable by 1 for each one and then gather the location without a real output. Without more information on the source data, it is harder to give you any prescriptive guidance.  But the code below can give you something close I think.

conn.open "SELECT QuantityAvailable, Location FROM [DAL.ProductContext].dbo.[Products] WHERE QuantityAvailable > 0 ORDER BY Location, Created ASC;", "DSN=dashboard"
TotalQuantity = 0
QuantityCounter = 0

if not conn.eof Then
       Location = conn("Location")
End If
do while not conn.eof
        if Location <> conn("Location") Then
             Response.Write "" + Location + "  " + TotalQuantity + " Items / " + QuantityCounter + " SKUs"
             Location = conn("Location")
             TotalQuantity = 0
             QuantityCounter = 0
        end if
        TotalQuantity = TotalQuantity + conn("QuantityAvailable")
        QuantityCounter = QuantityCounter + 1
        conn.moveNext
loop

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

DBAduck - Ben MillerPrincipal ConsultantCommented:
The other thing you could do as well is to make the Query give you what you want.  Run that and then look at the results and see if that is what you need, and if it is then you just output the numbers and location as you loop through instead of adding them up in code. Let SQL do the heavy work.

SELECT 
           Location, 
           SUM(QuantityAvailable) as QuantityAvailable,
           COUNT(*) as QuantityCounter
FROM [DAL.ProductContext].dbo.[Products]
WHERE QuantityAvailable > 0
GROUP BY Location
ORDER BY Location

Open in new window

bschwartingAuthor Commented:
Ok, I tried the 1st set of code, it runs with no errors, just didn't output anything.
DBAduck - Ben MillerPrincipal ConsultantCommented:
If you run the query in the 1st set of code, does it return any data?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.