adding dupe entries within a SQL query
I'm new to SQL and databases and have a delema which I'm sure is an
easy thing for you experts out there to do. What I have is this: I
have a table similliar to this:
table name: tablename
columns: date, name, purchased
Within that table the following data resides:
date name purchased
1/1/03 Mike 5
2/3/03 John 3
2/3/03 Mike 2
2/4/03 larry 2
etc etc etc.
What I want to do is get the top 5 names, and list how many total
they've purchased for a cirtain date range. I would have to add up
all the dupe purchases for each name for that date range, then produce
the top 5 purchases for that quarter.
I'm stuck on the adding up the dupes part, and kinda shaky on the
rest. I can find the dupes for a date range, but the rest is lost in
a brain cloud for me.
Can anyone help me with this please? I'm using SQL 2000 for the
server, but will be calling the SQL queary within an ASP web page.
thanks all!
Mike B
[1132 byte] By [
Mike B] at [2007-11-9 21:11:30]

# 1 Re: adding dupe entries within a SQL query
how about:
SELECT name
, SUM(purchased) as total_purchased
FROM tablename
WHERE date BETWEEN @date1 AND @date2
GROUP BY name
-- assuming you have variables defined for @date1 and @date2
--
HTH,
David Satz
Principal Web Engineer
Hyperion Solutions
"Mike B" <exibar@thelair.com> wrote in message
news:3f031d2e$1@tnews.web.dev-archive.com...
>
> I'm new to SQL and databases and have a delema which I'm sure is an
> easy thing for you experts out there to do. What I have is this: I
> have a table similliar to this:
>
> table name: tablename
> columns: date, name, purchased
>
> Within that table the following data resides:
>
> date name purchased
> 1/1/03 Mike 5
> 2/3/03 John 3
> 2/3/03 Mike 2
> 2/4/03 larry 2
>
> etc etc etc.
>
> What I want to do is get the top 5 names, and list how many total
> they've purchased for a cirtain date range. I would have to add up
> all the dupe purchases for each name for that date range, then produce
> the top 5 purchases for that quarter.
>
> I'm stuck on the adding up the dupes part, and kinda shaky on the
> rest. I can find the dupes for a date range, but the rest is lost in
> a brain cloud for me.
>
> Can anyone help me with this please? I'm using SQL 2000 for the
> server, but will be calling the SQL queary within an ASP web page.
>
> thanks all!
> Mike B
>