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 3 2962
seems a little extreme?
SELECT top 5 purchaser, SUM(quantity) AS Total, count(purchaser ) as Transactions
FROM Purchases
WHERE purchase_date BETWEEN @start_date AND @end_date
GROUP BY purchaser
order by SUM(quantity) desc
would also solve your issue...with that ddl
"John Gilson" <ja*@acm.org> wrote in message news:<PN******* *************@t wister.nyc.rr.c om>... "Mike Blanchard" <ex****@thelair .com> wrote in message news:cc******** *************** ***@posting.goo gle.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
I'm not completely clear as to what you're asking but the following UDF might be helpful.
CREATE TABLE Purchases ( purchase_date SMALLDATETIME NOT NULL, purchaser VARCHAR(25) NOT NULL, quantity INT NOT NULL, PRIMARY KEY (purchase_date, purchaser) )
-- Sample data INSERT INTO Purchases (purchase_date, purchaser, quantity) SELECT '20030101', 'Mike', 5 UNION ALL SELECT '20030203', 'John', 3 UNION ALL SELECT '20030203', 'Mike', 2 UNION ALL SELECT '20030204', 'Larry', 2
CREATE FUNCTION TopPurchasers (@start_date SMALLDATETIME, @end_date SMALLDATETIME, @top_n INT) RETURNS TABLE AS RETURN( SELECT purchaser, total, rank FROM (SELECT P1.purchaser, P1.total, COUNT(*) AS rank FROM (SELECT purchaser, SUM(quantity) AS total FROM Purchases WHERE purchase_date BETWEEN @start_date AND @end_date GROUP BY purchaser) AS P1 INNER JOIN (SELECT purchaser, SUM(quantity) AS total FROM Purchases WHERE purchase_date BETWEEN @start_date AND @end_date GROUP BY purchaser) AS P2 ON P2.total >= P1.total GROUP BY P1.purchaser, P1.total) AS Ranks WHERE rank <= @top_n )
-- Top 2 purchasers in Q1 of 2003 SELECT purchaser, total, rank FROM TopPurchasers(' 20030101', '20030331', 2) ORDER BY rank
purchaser total rank Mike 7 1 John 3 2
Regards, jag
"WangKhar" <Wa******@yahoo .com> wrote in message
news:bb******** *************** ***@posting.goo gle.com... seems a little extreme?
SELECT top 5 purchaser, SUM(quantity) AS Total, count(purchaser ) as Transactions FROM Purchases WHERE purchase_date BETWEEN @start_date AND @end_date GROUP BY purchaser order by SUM(quantity) desc
would also solve your issue...with that ddl
The solution I gave is parameterized not only by date range but by rank.
If one wants the top 5 today, it's fair to assume that the top 10 will pop up
tomorrow. The only additional code comes from having to determine rank
by other than the use of TOP which, of course, requires that the number of
rows returned be specified as a constant. Using a UDF just allows the
solution to be packaged in a modular reusable form for direct use within a
SELECT statement. After all, just because it's SQL doesn't mean good
software engineering practice needs to go totally out the window. :)
Regards,
jag
"John Gilson" <ja*@acm.org> wrote in message news:<PN******* *************@t wister.nyc.rr.c om>... "Mike Blanchard" <ex****@thelair .com> wrote in message news:cc******** *************** ***@posting.goo gle.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
I'm not completely clear as to what you're asking but the following UDF might be helpful.
CREATE TABLE Purchases ( purchase_date SMALLDATETIME NOT NULL, purchaser VARCHAR(25) NOT NULL, quantity INT NOT NULL, PRIMARY KEY (purchase_date, purchaser) )
-- Sample data INSERT INTO Purchases (purchase_date, purchaser, quantity) SELECT '20030101', 'Mike', 5 UNION ALL SELECT '20030203', 'John', 3 UNION ALL SELECT '20030203', 'Mike', 2 UNION ALL SELECT '20030204', 'Larry', 2
CREATE FUNCTION TopPurchasers (@start_date SMALLDATETIME, @end_date SMALLDATETIME, @top_n INT) RETURNS TABLE AS RETURN( SELECT purchaser, total, rank FROM (SELECT P1.purchaser, P1.total, COUNT(*) AS rank FROM (SELECT purchaser, SUM(quantity) AS total FROM Purchases WHERE purchase_date BETWEEN @start_date AND @end_date GROUP BY purchaser) AS P1 INNER JOIN (SELECT purchaser, SUM(quantity) AS total FROM Purchases WHERE purchase_date BETWEEN @start_date AND @end_date GROUP BY purchaser) AS P2 ON P2.total >= P1.total GROUP BY P1.purchaser, P1.total) AS Ranks WHERE rank <= @top_n )
-- Top 2 purchasers in Q1 of 2003 SELECT purchaser, total, rank FROM TopPurchasers(' 20030101', '20030331', 2) ORDER BY rank
purchaser total rank Mike 7 1 John 3 2
Regards, jag
Thank you all for the help. I went with the Sum() As, Group By
solution, which I actually cameacross before my message showed up in
Google :-)
I am going over the code that shows the rankings though, i think
that will be useful in the future for me too!
thank you all again!
Mike B
"John Gilson" <ja*@acm.org> wrote in message news:<SF******* *************@t wister.nyc.rr.c om>... "WangKhar" <Wa******@yahoo .com> wrote in message news:bb******** *************** ***@posting.goo gle.com... seems a little extreme?
SELECT top 5 purchaser, SUM(quantity) AS Total, count(purchaser ) as Transactions FROM Purchases WHERE purchase_date BETWEEN @start_date AND @end_date GROUP BY purchaser order by SUM(quantity) desc
would also solve your issue...with that ddl
The solution I gave is parameterized not only by date range but by rank. If one wants the top 5 today, it's fair to assume that the top 10 will pop up tomorrow. The only additional code comes from having to determine rank by other than the use of TOP which, of course, requires that the number of rows returned be specified as a constant. Using a UDF just allows the solution to be packaged in a modular reusable form for direct use within a SELECT statement. After all, just because it's SQL doesn't mean good software engineering practice needs to go totally out the window. :)
Regards, jag
"John Gilson" <ja*@acm.org> wrote in message news:<PN******* *************@t wister.nyc.rr.c om>... "Mike Blanchard" <ex****@thelair .com> wrote in message news:cc******** *************** ***@posting.goo gle.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
I'm not completely clear as to what you're asking but the following UDF might be helpful.
CREATE TABLE Purchases ( purchase_date SMALLDATETIME NOT NULL, purchaser VARCHAR(25) NOT NULL, quantity INT NOT NULL, PRIMARY KEY (purchase_date, purchaser) )
-- Sample data INSERT INTO Purchases (purchase_date, purchaser, quantity) SELECT '20030101', 'Mike', 5 UNION ALL SELECT '20030203', 'John', 3 UNION ALL SELECT '20030203', 'Mike', 2 UNION ALL SELECT '20030204', 'Larry', 2
CREATE FUNCTION TopPurchasers (@start_date SMALLDATETIME, @end_date SMALLDATETIME, @top_n INT) RETURNS TABLE AS RETURN( SELECT purchaser, total, rank FROM (SELECT P1.purchaser, P1.total, COUNT(*) AS rank FROM (SELECT purchaser, SUM(quantity) AS total FROM Purchases WHERE purchase_date BETWEEN @start_date AND @end_date GROUP BY purchaser) AS P1 INNER JOIN (SELECT purchaser, SUM(quantity) AS total FROM Purchases WHERE purchase_date BETWEEN @start_date AND @end_date GROUP BY purchaser) AS P2 ON P2.total >= P1.total GROUP BY P1.purchaser, P1.total) AS Ranks WHERE rank <= @top_n )
-- Top 2 purchasers in Q1 of 2003 SELECT purchaser, total, rank FROM TopPurchasers(' 20030101', '20030331', 2) ORDER BY rank
purchaser total rank Mike 7 1 John 3 2
Regards, jag This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: lawrence |
last post by:
I've been bad about documentation so far but I'm going to try to be
better. I've mostly worked alone so I'm the only one, so far, who's
suffered from my bad habits. But I'd like other programmers to have an
easier time understanding what I do. Therefore this weekend I'm going
to spend 3 days just writing comments. Before I do it, I thought I'd
ask other programmers what information they find useful.
Below is a typical class I've...
|
by: netpurpose |
last post by:
I need to extract data from this table to find the lowest prices of
each product as of today. The product will be listed/grouped by the
name only, discarding the product code - I use SUBSTRING(ProductName,
1, CHARINDEX('(', ProductName)-2).
I can get this result, but I had to use several views (totally
inefficient). I think this can be done in one efficient/fast query,
but I can't think of one.
In the case that one query is not...
|
by: paii |
last post by:
I have a table that stores job milestone dates. The 2 milestones I am
interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18.
All jobs have TypeID 1 only some jobs have TypeID 18. I need a query that
will return the c date for TypeID 18 if it exist else the date for TypeID 1,
for all jobs.
the table structure is the following
Job
TypeID
|
by: pw |
last post by:
Hi,
I am having a mental block trying to figure out how to code this.
Two tables:
"tblQuestions" (fields = quesnum, questype, question)
"tblAnswers" (fields = clientnum, quesnum, questype, answer)
They are related by quesnum and questype. There are records in
|
by: K. Crothers |
last post by:
I administer a mechanical engineering database. I need to build a
query which uses the results from a subquery as its input or
criterion.
I am attempting to find all of the component parts of which a part may
be composed. I have a table of parts and their subparts. The problem
is that each of those subparts may be composed of smaller component
parts. The subpart would then be listed in the Part field linked to
each of its subparts in...
| |
by: google |
last post by:
I have a database with four table. In one of the tables, I use about
five lookup fields to get populate their dropdown list. I have read
that lookup fields are really bad and may cause problems that are hard
to find. The main problem I am having right now is that I have a report
that is sorted by one of these lookup fields and it only displays the
record's ID number. When I add the source table to the query it makes
several records...
|
by: ward |
last post by:
Greetings.
Ok, I admit it, I bit off a bit more than I can chew.
I need to complete this "Generate Report" page for my employer and I'm
a little over my head. I could use some additional assistance. I say
additional because I've already had help which is greatly appreciated.
I do try to take the time and understand the provided script in hopes
on not having to trouble others on those. But here it goes...
|
by: L. R. Du Broff |
last post by:
I own a small business. Need to track a few hundred pieces of rental
equipment that can be in any of a few dozen locations. I'm an old-time C
language programmer (UNIX environment). If the only tool you know how to
use is a hammer, every problem tends to look like a nail. That said, I
could solve my problem in C, but it's not the right tool. I need to come
into the Windows world, and I need to get this done in Access or
something...
|
by: Rnykster |
last post by:
I know a little about Access and have made several single table
databases. Been struggling for about a month to do a multiple table
database with no success. Help!
There are two tables. First has about 30 fields. Every entry in this
table will be unique. Second table has about 7 fields and is for
reference - strictly a look up type table. I want to use one field,
say FAMILY in the first table to look up any one of the 400 items in
the...
|
by: pbd22 |
last post by:
Hi.
I need some help with structuring my query strings.
I have a form with a search bar and some links. Each
link is a search type (such as "community"). The HREF
for the link's anchor looks like the following:
<a href="?searchtype=2">Community</a>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |