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.
1 1365
Something like this should do the trick: - create table customer_purchases(customer_id char(3) not null,
-
purchase_date date not null,
-
vehicle_id char(4) not null);
-
commit;
-
insert into customer_purchases values('C1', '2000-02-29', 'VIN1');
-
insert into customer_purchases values('C1', '2000-05-26', 'VIN2');
-
insert into customer_purchases values('C1', '2000-12-27', 'VIN3');
-
insert into customer_purchases values('C1', '2001-10-11', 'VIN4');
-
insert into customer_purchases values('C1', '2003-10-24', 'VIN5');
-
commit;
-
-
with initial(yr) as (values(year(current date)-10)),
-
years(yr) as (select I.yr
-
from INITIAL I
-
union all
-
select Y.yr+1
-
from YEARS Y
-
where Y.yr < year(current date)),
-
first_purchase(total, p, first_purchase_date) as
-
(select Y.yr as Total,
-
sum(case year(CP.purchase_date)
-
when Y.yr then 1
-
else 0
-
end) as p,
-
min(case year(CP.purchase_date)
-
when Y.yr then CP.purchase_date
-
else '9999-12-31'
-
end) as first_purchase_date
-
from customer_purchases CP,
-
years Y
-
group by Y.yr)
-
select FP.total,
-
FP.p,
-
sum(case
-
when CP.purchase_date - FP.first_purchase_date <= 0 then 0
-
when CP.purchase_date - FP.first_purchase_date <= 10000 then 1
-
else 0
-
end) as this_year,
-
sum(case
-
when CP.purchase_date - FP.first_purchase_date <= 10000 then 0
-
when CP.purchase_date - FP.first_purchase_date <= 20000 then 1
-
else 0
-
end) as next_year,
-
sum(case
-
when CP.purchase_date - FP.first_purchase_date <= 20000 then 0
-
when CP.purchase_date - FP.first_purchase_date <= 30000 then 1
-
else 0
-
end) as two_years,
-
sum(case
-
when CP.purchase_date - FP.first_purchase_date <= 30000 then 0
-
when CP.purchase_date - FP.first_purchase_date <= 40000 then 1
-
else 0
-
end) as three_years
-
from first_purchase FP,
-
customer_purchases CP
-
group by FP.total,
-
FP.p
-
order by 1;
-
commit;
-
drop table customer_purchases;
-
commit;
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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.,
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |