473,394 Members | 1,813 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,394 software developers and data experts.

Need to find when the customers next vehicle purchase occured in less than 1 yr

Example:

Customer Purchase VEHS

C1 20000229 VIN1
C1 20000506 VIN2
C1 20001227 VIN3
C1 20011011 VIN4
C1 20031024 VIN5

Need to determine when the customers next purchase occured in number of years other than the first vehicle in that particular Year.

Need to populate the following counts:

Total P <12 mon 12 -23 24-35 36-47
2000 3 2 1 0 1
2001 1 0 0 0 0
2002 0 0 0 0 0
2003 1 0 0 0 0

Please help me in this.

Thanks in Advance.
Sunny.
Apr 9 '10 #1
1 1365
Something like this should do the trick:
Expand|Select|Wrap|Line Numbers
  1. create table customer_purchases(customer_id   char(3) not null,
  2.                                 purchase_date date    not null,
  3.                                 vehicle_id    char(4) not null);
  4. commit;
  5. insert into customer_purchases values('C1', '2000-02-29', 'VIN1');
  6. insert into customer_purchases values('C1', '2000-05-26', 'VIN2');
  7. insert into customer_purchases values('C1', '2000-12-27', 'VIN3');
  8. insert into customer_purchases values('C1', '2001-10-11', 'VIN4');
  9. insert into customer_purchases values('C1', '2003-10-24', 'VIN5');
  10. commit;
  11.  
  12. with initial(yr) as (values(year(current date)-10)),
  13.        years(yr) as (select I.yr
  14.                        from INITIAL I
  15.                      union all
  16.                      select Y.yr+1
  17.                        from YEARS Y
  18.                       where Y.yr < year(current date)),
  19.       first_purchase(total, p, first_purchase_date) as
  20.                     (select Y.yr                                   as Total,
  21.                             sum(case year(CP.purchase_date)
  22.                                    when Y.yr then 1
  23.                                    else           0
  24.                                 end)                               as p,
  25.                             min(case year(CP.purchase_date)
  26.                                    when Y.yr then CP.purchase_date
  27.                                    else           '9999-12-31'
  28.                                 end)                               as first_purchase_date 
  29.                        from customer_purchases CP,
  30.                             years               Y
  31.                       group by Y.yr)
  32. select FP.total,
  33.        FP.p,
  34.        sum(case
  35.               when CP.purchase_date - FP.first_purchase_date <=     0 then 0
  36.               when CP.purchase_date - FP.first_purchase_date <= 10000 then 1
  37.               else                                                         0
  38.            end)                                                    as this_year,
  39.        sum(case
  40.               when CP.purchase_date - FP.first_purchase_date <= 10000 then 0
  41.               when CP.purchase_date - FP.first_purchase_date <= 20000 then 1
  42.               else                                                         0
  43.            end)                                                    as next_year,
  44.        sum(case
  45.               when CP.purchase_date - FP.first_purchase_date <= 20000 then 0
  46.               when CP.purchase_date - FP.first_purchase_date <= 30000 then 1
  47.               else                                                         0
  48.            end)                                                    as two_years,
  49.        sum(case
  50.               when CP.purchase_date - FP.first_purchase_date <= 30000 then 0
  51.               when CP.purchase_date - FP.first_purchase_date <= 40000 then 1
  52.               else                                                         0
  53.            end)                                                    as three_years
  54.   from first_purchase     FP,
  55.        customer_purchases CP
  56.  group by FP.total,
  57.           FP.p
  58.  order by 1;
  59. commit;
  60. drop table customer_purchases;
  61. commit;
Apr 27 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

55
by: Alex | last post by:
Hello people, The following is not a troll but a serious request. I found myself in a position where I have to present a Pro/Con list to management and architects in our company with regard to...
3
by: Tony Johansson | last post by:
Hello!! Assume we have one base class called Vehicle and two derived classes called Car and Bus. I would be able to call method getName on an object of class Car or Bus and return back the name...
1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here....
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
3
by: vijaykokate | last post by:
Our company http://www.softnmation.com/ offers its customers a great variety of products. Everything you need can be found in this site. Web Template, CSS Template, Logo Template, Corporate...
3
by: Learner | last post by:
Hello, I have two buttons on one of my VehicleDetails.aspx page. Obiviously these two buttons takes the user to two different pages. Now my client is interested in having a linkbutton instead of...
35
by: javelin | last post by:
I posted an answer to someone's question, and realized I have more questions than answers. Thus, I am going to post my scenario to get to the question that I have: I have a challenge, to figure...
3
by: kirke | last post by:
Hi, I have a tricky problem.. I printed out several numbers. For example, for ($i=0:$i<$k;$i++) printf("%s",$number); Then, numbers are printed out. e.g.,
21
by: nihad.nasim | last post by:
Hi there, I have a database in Access that I need on the web. The web page should connect to the database and write records for certain tables and view records for others. I want to know a...
4
by: wellscrambled | last post by:
Folks, Probably don't have the right forum here, but this is all a bit new to me. I have a web site that sends me a daily email with an excel spreadsheet attachment that contains some customer...
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
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
Oralloy
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.