473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

need help with 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
Jul 20 '05 #1
3 2947
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********************@twister.nyc.rr.com>.. .
"Mike Blanchard" <ex****@thelair.com> wrote in message
news:cc**************************@posting.google.c om...
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

Jul 20 '05 #2
"WangKhar" <Wa******@yahoo.com> wrote in message
news:bb**************************@posting.google.c om...
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********************@twister.nyc.rr.com>.. .
"Mike Blanchard" <ex****@thelair.com> wrote in message
news:cc**************************@posting.google.c om...
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

Jul 20 '05 #3
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********************@twister.nyc.rr.com>.. .
"WangKhar" <Wa******@yahoo.com> wrote in message
news:bb**************************@posting.google.c om...
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********************@twister.nyc.rr.com>.. .
"Mike Blanchard" <ex****@thelair.com> wrote in message
news:cc**************************@posting.google.c om...
> 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

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
9
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...
6
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....
3
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,...
7
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...
3
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...
0
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...
10
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...
7
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...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.