Oracle Query to get - sql

Hi I have a peculiar requirement :
Generate a report calculating retention of customers for each week (last 8 week, week must start on mon and end on sun).
1) report is for 8 iso week (Mon-Sun), if I run report today i.e 10/23/2014 it should take data till 10/19/2014(week#42) back to 08/25/2014(week#35) and calculate retention for each week.So total 8 week...
This part I have completed using TRUNC(sysdate-7,'IW') & group by count for each week
But below point is getting harder for me
2) for each week the total cust count should be :
all the customers active before last day of previous week (i.e Sun of previous week)
for iso week 35
Start End : Calculate all the customers before 08/25/2014 -- A1
8/25/2014 8/31/2014
then calculate all customers who bought product in last 30 days i.e from 07/25/2014 to 08/24/2014. --A2
Retention % = A2/A1
the total for week 35 will be different
Start End : Calculate all the customers before 09/01/2014 --B1
9/1/2014 9/7/2014
then calculate all customers who bought product in last 30 days i.e from 08/01/2014 to 8/31/2014. --B2
Retention % = B2/B1

Related

Extract information based on a week number field

I am using Orcle sql developer to access an MSsql database. I have week number field. and I want to extract all information that the week no is equal to the week number of today's date.
Sample Data
DutyStaff Address WeekNo Contact
John 35thd 15 670956
Peter 25 str 19 706346
Chris 40 odr 20 856294
Adam 7thstr 34 567251
Mary 6thstr 22 702457
I want to extract all rows that have the week number of today. Select * where weekno = (today's week number). The week number of today 12/05/2022 is 19 i.e current day week number
select * from <table_name> as t where t.week_no = ;
will give you all the info specific to the chosen week.
Also;
select * from <table_name> as t groupby t.week_no;
will group all info pertaining to a particular week as one.
From my understanding, this is what you were asking for, I think.

Calculate actual working days for each month

I am trying to calculate a factor for each employee and month. The factor depends on the actual working days.
For example to calculate it for April (4/1/2019 to 4/30/2019) one employee is on vacation from 4/8/2019 to 4/12/2019 and he will leave the company on the 4/26/2019.
April has 30 days and the employee has 5 days off and 4 days without contract (so hasn't the possibility to work for 9 days). In this case the factor would be 0.7 = (1/30)*(30-9).
How is it possible to count the clashing days for each month? It is also possible that an employee is on vacation from 3/25/2019 to 4/5/2019 etc.

How to find the leave balance month wise in teradata

I have sample data like this
EmployeeID Begin Date End Date Duration
168835 28/11/2017 28/11/2017 6
168835 31/10/2017 9/11/2017 32
and I want to find the leave taken by this employee month wise,so i have joined with calendar date and i want to summarise this data monthly.For e.g, he has taken leave from 31/10/2017 to 9/11/2017 for 32 hours and 6 hours in 28/11/2017. I want to exclude weekend from 31/10/2017 to 9/11/2017 period. I have used "day of week" flag to exclude the weekends, but now problem is when i roll up the data its giving me incorrect result. basically, i want to data like duration (32+6)=38 and this should be divided no of working days that is 9 in this case, so in oct month my data should be 4.22 and in nov month it should 33.77. can anyone please help me on this.Thanks in advance.

SQLite - Determine average sales made for each day of week

I am trying to produce a query in SQLite where I can determine the average sales made each weekday in the year.
As an example, I'd say like to say
"The average sales for Monday are $400.50 in 2017"
I have a sales table - each row represents a sale you made. You can have multiple sales for the same day. Columns that would be of interest here:
Id, SalesTotal, DayCreated, MonthCreated, YearCreated, CreationDate, PeriodOfTheDay
Day/Month/Year are integers that represent the day/month/year of the week. DateCreated is a unix timestamp that represents the date/time it was created too (and is obviously equal to day/month/year).
PeriodOfTheDay is 0, or 1 (day, or night). You can have multiple records for a given day (typically you can have at most 2 but some people like to add all of their sales in individually, so you could have 5 or more for a day).
Where I am stuck
Because you can have two records on the same day (i.e. a day sales, and a night sales, or multiple of each) I can't just group by day of the week (i.e. group all records by Saturday).
This is because the number of sales you made does not equal the number of days you worked (i.e. I could have worked 10 saturdays, but had 30 sales, so grouping by 'saturday' would produce 30 sales since 30 records exist for saturday (some just happen to share the same day)
Furthermore, if I group by daycreated,monthcreated,yearcreated it works in the sense it produces x rows (where x is the number of days you worked) however that now means I need to return this resultset to the back end and do a row count. I'd rather do this in the query so I can take the sales and divide it by the number of days you worked.
Would anyone be able to assist?
Thanks!
UPDATE
I think I got it - I would love someone to tell me if I'm right:
SELECT COUNT(DISTINCT CAST(( julianday((datetime(CreationDate / 1000, 'unixepoch', 'localtime'))) ) / 7 AS INT))
FROM Sales
WHERE strftime('%w', datetime(CreationDate / 1000, 'unixepoch'), 'localtime') = '6'
AND YearCreated = 2017
This would produce the number for saturday, and then I'd just put this in as an inner query, dividing the sale total by this number of days.
Buddy,
You can group your query by getting the day of week and week number of day created or creation date.
In MSSQL
DATEPART(WEEK,'2017-08-14') // Will give you week 33
DATEPART(WEEKDAY,'2017-08-14') // Will give you day 2
In MYSQL
WEEK('2017-08-14') // Will give you week 33
DAYOFWEEK('2017-08-14') // Will give you day 2
See this figures..
Day of Week
1-Sunday, 2- Monday, 3-Tuesday, 4-Wednesday, 5-Thursday, 6-Saturday
Week Number
1 - 53 Weeks in a year
This will be the key so that you will have a separate Saturday's in every month.
Hope this can help in building your query.

T-Sql business rule calculation

I have a complicated problem that needs to be solved in t-sql. Possibly without a cursor or a loop.
Given the following table with customer’s payment setup.
ID Frequency Amount Start Date End Date
1 Monthly 100 01-01-2016 N/A(ongoing)
The customer wants to know how much he\she will have to pay for the month of November.
If they were to close the account on 15 nov 2016.
For example:
Assume the customer wants to close their account on 15-nov-2016 and wants to know the $amount they will be paying from 1st of November to 15 of November.
Calculation
The frequency cycle for the customer payment is Monthly.
Taking the frequency into account we know that:
Customer start date of November will be 1st of November
The end date will be 30 November
Calculation formula
(DayUpToCloseDate/DaysInNov) * Amount = amount customer is asking.
DaysUpToCloseDate = 15 (diff 1st of nov and 15th of nov)
DaysInNov = 30
Amount = 100
(15/30)*100 = 50
So we can tell the customer he/she will be paying 50$ in November if they were to close the account on 15th of November.
First we need to declare the 2 variables, the date in question and the monthly amount due.
DECLARE #date datetime = '2017-11-15'
DECLARE #amountDue int = 100
Then to get the month to date amount due we can use:
SELECT CAST(DATEPART(DAY,#date) AS FLOAT)/DATEPART(DAY,DATEADD(MONTH,DATEDIFF(MONTH,0,#date)+1,0)-1) * #amountDue AS [MonthToDateAmountDue]
Here is how we got there.
SELECT
--This gets the first day of the next month and subtracts 1 day, getting the last day of the month the date falls in.
DATEADD(MONTH,DATEDIFF(MONTH,0,#date)+1,0)-1 AS [LastDayOfMonth]
--We now extract the day date part of the last day of the month to get the total days in the month.
,DATEPART(DAY,DATEADD(MONTH,DATEDIFF(MONTH,0,#date)+1,0)-1) AS [DaysInMonth]
--Finally, we get the day date part from our #date and divide by the total days of the month to get the percentage of the month we have completed.
--Since int does not do decimals, we use a float.
,CAST(DATEPART(DAY,#date) AS FLOAT)/DATEPART(DAY,DATEADD(MONTH,DATEDIFF(MONTH,0,#date)+1,0)-1) AS [PercentageOfMonthCompleted]
--And mulitply it by the amount due.
,CAST(DATEPART(DAY,#date) AS FLOAT)/DATEPART(DAY,DATEADD(MONTH,DATEDIFF(MONTH,0,#date)+1,0)-1) * #amountDue AS [MonthToDateAmountDue]
Edit: So, I just learned about the EOMONTH function. This can be shortened to
SELECT CAST(DATEPART(DAY,#date) AS FLOAT)/DATEPART(DAY,EOMONTH(#date)) * #amountDue AS AS [MonthToDateAmountDue]