In this lesson, we will create a report tracking the date of customer's first visit to the store, last date of visit to the store, and the duration in between.
Customer's date of first visit, last visit, and the durationTutorial
: |
The first visit date is where the customer first came to the store in the data, and the last visit date is the lastest date that he / she visited in the data. The duration is therefore the difference between the first visit date and the last visit date.
Although there is only one year data in this data set, we can still observe cases of new customers starting their purchase, and stopped coming to the store at some point. Thereby, we will be able to tell the first and last visit date to the store, and identify customers with short visits.
Most commands to create the report is covered in Basic commands. Below is the process flow to generate the report, think through each step carefully before proceeding.
Answer: The process flow
When creating more complicated scripts, it is more efficient and clear to create each section separately and join the results at the end.
After we have prepared the work flow, let's rethink each step and start creating the script.
#/bin/bash #=============================================================== # MUSASHI bash script #=============================================================== #---- Title title="Customer's first visit, last visit, and duration #---- Comment comment="Basic Reports" #---- Variables inPath="/home/public/tutorial" #--------------------------------------------------------------- # Command #--------------------------------------------------------------- #Customer's first visit to the store xtcat -i $inPath/dat.xt | xtdelnul -f customer | xtcut -f customer,date | xtagg -k customer -f date:firstvisit -c min -o xxfirst #Customer's last visit to the store xtcat -i $inPath/dat.xt | xtdelnul -f customer | xtcut -f customer,date | xtagg -k customer -f date:lastvisit -c max -o xxlast #Total duration of visit xtjoin -k customer -m xxlast -f lastvisit -i xxfirst | xtcal -c 'day($lastvisit,$firstvisit)' -a duration | xtheader -l "$title" -c "$comment" -o visitdate.xt rm xx* #=============================================================== |
Check your results to make sure the commands are properly executed.
The following are additional exercises for this lesson, try them out and check your results with the scripts and output given below.
Report Name | Script Name | Result (xt) | Result (html) |
The number of new customers to the store by Year and Month | visitdate1.sh | visitdate1.xt | visitdate1.html |
The number of customers leaving the store by Year and Month | visitdate2.sh | visitdate2.xt | visitdate2.html |