Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 19th, 2007, 10:34 AM
Authorized User
 
Join Date: Jul 2006
Location: , , United Kingdom.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default This Stored Procedure

Hi all,

I have a table contains the following columns: ID, CustomerName, ProductID,PurchaseDate.

A customer might have purchase the same product in different dates, so I need to query the table to display ID, CutomerName, ProductID, number of times each Product have been bought by the same customer, and the date of last purchase of each product by the customer. This is my stored procedure but it is not return the right resulty.This is my table

ID ProductID CustomerName PurchaseDate
1 1 John Smith 10/3/2000
2 1 John Smith 20/5/2006
3 1 Sam 24/7/2003
4 1 Sam 20/10/2000
5 2 Sam 11/11/1985
4 2 Sarah 26/8/2001
5 2 John Smith 11/11/1998
6 2 John Smith 01/10/1980

and this is how I want to see the query result:

ProductID CustomerName NumberOfPurchaseTime LastPurchaseDate
1 John Smith 2 20/5/2006
2 John Smith 2 11/11/1998
1 Sam 2 24/7/2003
2 Sam 1 11/11/1985
2 Sarah 1 26/8/2001


This is my stored procedure
CREATE PROCEDURE [dbo].[CustomerHistry]

AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT MAX (PurchaseDate) As "Latest purchase date:" ,COUNT( ProductID) AS " No. of this item been purchased ", CustomerName


FROM
    [ CustomerHistry]


group by CustomerName

GO


I will be grateful if any of you kindly help me to fix this and it is very urgent.

regards rao
Reply With Quote
  #2 (permalink)  
Old June 24th, 2007, 02:39 PM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You must GROUP BY both CustomerName and ProductID

Reply With Quote
  #3 (permalink)  
Old July 2nd, 2007, 07:21 PM
Friend of Wrox
Points: 2,101, Level: 18
Points: 2,101, Level: 18 Points: 2,101, Level: 18 Points: 2,101, Level: 18
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2003
Location: , , .
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Rao,

Did that work?



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stored procedure prashant_telkar SQL Server 2000 1 July 9th, 2007 07:57 AM
Stored Procedure jezywrap SQL Server ASP 1 January 3rd, 2007 12:29 AM
stored procedure kdm260 SQL Server 2000 2 June 19th, 2006 04:45 PM
Stored Procedure rajanikrishna SQL Server 2000 0 July 18th, 2005 05:01 AM
Help About Stored Procedure zhuge6 BOOK: ASP.NET Website Programming Problem-Design-Solution 3 May 20th, 2005 09:27 AM



All times are GMT -4. The time now is 08:02 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.