SQL Server - Procedure to calculate loss and profit - procedure

I´m training to be a SQL Server user and I have a question if I have to create a procedure in this case and how create it. I wrote a query where I can show the revenue capacity for a specific month if I booked every rooms in the hotel. And I wrote another query where I can show the total revenue for the booked rooms for the same specific month.
So with these datas, now I have to show if the hotel had loss and profit. Please, someone could help me?
/* Revenue capacity for August 06 */
SELECT sum(Vl_price * 31) Revenue_capacity_August06
FROM Room
WHERE Nr_hotel = '1'
group by Nr_hotel
/* Total Revenue for August06 */
SELECT sum(DATEDIFF (DAY,Dt_start,Dt_end) * Vl_price) Revenue
FROM Booked B
inner join Room R
ON B.Nr_room = R.Nr_room
WHERE R.Nr_hotel = '1' and Dt_start between '2006/08/01' and '2006/08/31'
I have to show if this specific hotel had loss or profit in the specific period.

Related

Tableau Desktop - How to count excluding nulls from a column and two more conditions that checks additional columns

I am identifying 4 metrics
Metric 1. Request - Count all unique ids
Metric 2. Enrolled - All customers that have a date. This confirms that the customer received orders
Metric 3. Current - Date is less than 6 months from today. This will confirm that the customers are active
Metric 4. Dropped - Date is more than 6 months from today, This confirms that customers did not buy from us for more than 6 months.
Calculation Summary
I am calculating the date difference and then using buckets like < 6 months and > 6 months to separate the data. Then using the individual calculated field to count the numbers for each metric.
Below are my current calculations in Tableau
Metric 1 : Request
countd(id)
Metric 2 : Enrolled
COUNTD(IF NOT ISNULL(
[Date])
THEN [ID]
END)
Metric 3 : To calculate Current Customers, I have below additional calculations.
1. Date diff calculation
if NOT ISNULL([Date])
then datediff('month',[Date],Today())
END
2. current six months Bucket
IF [Date Diff Calc]<=6 THEN "<6 months"
END
3. Current Customer metric
COUNT([current six months Bucket])
However, I need to make changes to - Metric 2 (Enrolled) and Metric 3 (Current) with additional conditions
Metric 2 : Enrolled
1. Customers that have 'QRST' prefix in their ID should only be counted when the Repeat column has 'No'
2. But for the rest of the customers, all rows should be counted regardless of repeat yes or no statuses.
3. Additionally, two IDs- QRST-AA2517 and QRST-CO1325 should be removed from the total count.
Metric 3 : Current
1. Customers that have 'ABC' as prefix in their ID and Country = countryname should not be counted under this metric
2. But for the rest of the customers, all rows should be counted regardless of the country
Sample data structure
ID DATE REPEAT COUNTRY
ABC-1234 12-3-2015 Yes USA
QRST-AA2517 11-5-2021 No Italy
XYZ - 1234 08-3-2022 No Germany

Formula to calculate historical data based off a specific condition?

I am trying to observe historical trends on customer acquisitions (new and returning) and am looking to use a formula to automate it for me.
Essentially, I am looking to determine the average amount of new customers we acquire on a specific day, specific week, and specific month. For example: what are the average customers we have acquired every Monday for the past 6 months, or what is the average number of customers we acquire the first week of every month?
Solution:
You can use the date operators in your QUERY statement to filter by month, week, or even day of week.
Examples:
every Monday for past 6 months
=query(A1:B, "select avg(B) where datediff(todate(now()),todate(A)) < 180 and dayofweek(A) = 2", 1)
first week of every month
=query(A1:B, "select month(A),avg(B) where day(A) <= 7 group by month(A) offset 1", 1)
You would need to tweak the sample queries to cover your data range and which columns do you need to average and compare.
References:
QUERY()
Query Language Reference | Scalar Functions

Commission calculation based on sliding month (googlesheets)

I have to pay a commission to Agents (affiliates) based on the following conditions:
the commission starts on a monthly basis following a USER (linked to the Agent) first deposit/purchase on a website
agents have a decreasing commission, ex: 1 month following first deposit of their USER = 30% of sales, 2d month period following 1st deposit of USER: 25% of sales, etc
Commission are paid on a month basis calculation (ex: from 01/07/2020 till 31/07/2020)
If a USER makes a first purchase on June 22d and if sales commission for 1st period is 30%, then agent is eligible to a 30% commission on sales from june 22d till July 22d, then 25% for sales from 23rd july till 23rd august, etc
I have designed a googlesheets (see below) that serves the purpose (using 12 columns to get the correct commission% for a specific user on a specific day!), but I am trying to find a more straight forward formula to get the applicable com. % based on the commission sliding table and the first deposit date of a specific user.
Can anyone help?
The google sheets showing my calc is here:
https://docs.google.com/spreadsheets/d/1I1gzZ670hJH8HwCGizzbvlQkg0dgAvgOSQTfOUL0VgU/edit?usp=sharing
This might help you. (Updated to correct the row number of where the formula should go.)
If you insert a new column in your sheet, to the right of Column W, the Current Commision Month, and paste the following formula in the cell where the Current Commision Month header text should appear (Row 9 in your sample) of that column, it replicates the results in your Current Commision Month.
But it does not require columns I through V. You can test that by deleting columns I through V - you can use Undo and Redo to go back and forth, if necessary. Depending on how you use your "End" column - the logic wasn't clear to me - the info for that can also be gained in this one column.
={"Current
Commision
Month";"";ArrayFormula(
if(
($H11:H<>"") * ($A11:A>=date(year(H11:H),month(H11:H),day(H11:H))),
ifs( $A11:A< date(year(H11:H),month(H11:H)+1,day(H11:H)),1,
$A11:A< date(year(H11:H),month(H11:H)+2,day(H11:H)),2,
$A11:A< date(year(H11:H),month(H11:H)+3,day(H11:H)),3,
$A11:A< date(year(H11:H),month(H11:H)+4,day(H11:H)),4,
$A11:A< date(year(H11:H),month(H11:H)+5,day(H11:H)),5,
$A11:A< date(year(H11:H),month(H11:H)+6,day(H11:H)),6,
$A11:A>=date(year(H11:H),month(H11:H)+7,day(H11:H)),9999),
""))}
The first IF test is to check that the FirstDeposit date is not blank, and that the sale date is greater than or equal to the FirstDeposit date.
The IFS tests go through and check whether the sale date is less than one of the months, and stops at the first value (commission month) that is greater than the sale date. If never, it places a vlaue of 9999.
Note that the "9999" values are just to indicate the sale date is greater than the "End" date, and can be changed to blanks or whatever you want.
[![enter image description here][1]][1]
I've added a sample tab with the final result. Let me know if this helps. There may be several other enhancements possible for your sheet, in particular the use of ARRAYFORMULAS to fill values down many of your columns.
I haven't spent time on the actual commision calculations, in the final columns, but if you feel that still needs improvements, I can try to simplify there as well.
[1]: https://i.stack.imgur.com/TfFZ5.png

Need to return production finish date of an order based on the weekly production Qty in excel

I have 2 tables
Table-1 = Order details
Table-2 = Production details.
Explanation of color inside table:
Yellow color = Output Qty week wise and product wise.
Green color = My expectation. Example- The second order of shirt(Qty-10) delivery date is 14 Jan & there are 2 more orders (order num 1 & 4) of shirt which have delivery earlier than 14 Jan. So the finish week will be 4 as the order num 1 & 4 (total Qty 6) will be produced till week 2 as per the Table-2 (total Qty =7 (3+4).
Thanks to help me write the formula in E 2 to E6 cells.
Table1:
Table2:
Work out the sum of quantities for the same product and dates including this one using sumifs.
Compare it to the cumulative sum of the numbers produced for this product using match.
=ArrayFormula(match(true,sumifs(C$2:C$6,B$2:B$6,B2,D$2:D$6,"<="&D2)<=sumif(column(H:K),"<="&column(H:K),index(H$3:K$4,match(B2,G$3:G$4,0),0)),0))
I'm assuming for the time being that you couldn't have two rows with the same product and delivery date. If this could happen, you could refine the formula for the situation where (say) the first delivery could be sent in week 2 but the next delivery would be in week 3.

Google Sheets Formula For Wage Calculation

We are trying to work out a formula for paying our sales team. Basically they get paid a basic wage of £350 regardless of making any sales as a "safety net" or 40% of sales done up to £2000 (whichever is greater). So for example, £1000 sales = £400 commission, £500 sales = £350 commissions.
Essentially the £350 is there incase sales fall below £875 they are guaranteed to still get the £350.
It gets a little tricky for me when sales go over £2000 anything OVER £2000 they get 50% on. So for example £3000 sales they would get £800 on the first £2000 and £500 on the £1000 over the £2000. So total wages for that week would be £1,300. Some examples below:
Sales: £500 Pay = £350
Sales: £900 Pay = £360
Sales: £1500 Pay = £600
Sales: £2500 Pay = £1,050
Sales: £4000 Pay = £1,800
Is there a formula I can use if i have the sales total of the sales agent, to calculate there wage automatically.
Any help would be greatly appreciated.
This is a fairly short way of doing it
=MAX(350,0.4*A1+0.1*text(A1-2000,"0;\0"))
Otherwise perhaps more simply
=if(A1<875,350,if(A1<2000,A1*0.4,800+(A1-2000)*0.5))
Which can be simplified to
=IF(A1<875,350,IF(A1<2000,A1*0.4,0.5*A1-200))

Resources