Pseudocode - Calculating the total amount from records - sum

I am currently revising for an exam and received this question:
Write a programme that will continually prompt for an employees number, hours worked, and pay rate. The programme should calculate how much each employee is paid and the total all the pays over all. Your programme is to repeat this process until a sentinel number of 9999 is entered, then the programme should print the number of employees whose pays have been entered, and the total amount overall.
So far I have:
PAY_CALCULATOR
set total_employees to zero
DISPLAY 'Please enter an employee number'
GET employee_no
DOWHILE employee_no != 9999
add 1 to toal_employee
DISPLAY 'Please enter hours worked'
GET pay_rate
So as you can see I am missing how to sum the values of each 'pay_rate' received.
Any insight, is there a better way to do this other than a DOWHILE?
Cheers, Benji

Try the following Pseudocode,
PAY_CALCULATOR
set total_employees to zero
set total_pay to zero
DO
DISPLAY 'Please enter an employee number'
GET employee_no
add 1 to toal_employee
DISPLAY 'Please enter hours worked'
GET hours
Display 'Please enter pay rate'
GET pay_rate
SET SAL = hours * pay_rate
SET total_pay = total_pay + SAL
WHILE employee_no != 9999
Display 'Number of Paid Employees', employee_no
Display 'Total Salary Paid', total_pay
For the given problem the number of employees is not predetermined. So Do While loop is O.K.

Related

Calculate Hourly Pay Based on Pay Rate Column

I am trying to calculate the hourly pay based on the pay rate column. Technically, I am trying to see who has hourly pay assigned to that column vs folks that have annual salary in that column.
Employee
Pay_Rate
400001
10283.52
304068
120000
304069
20.25
I am trying to find a decimal and if decimal is found, I seeing if its greater than zero. If its greater than zero, then I leaving it alone assuming its hourly pay; but if decimal isnt found, then I am taking that number and dividing by 2080 to get hourly pay. My logic, however, works well unless there is a decimal in annual pay. How do I code around that?
CASE WHEN ((EMP.PAY_RATE - trunc(EMP.PAY_RATE)) > 0) THEN EMP.PAY_RATE ELSE (EMP.PAY_RATE/2080) END AS "Hourly_Wage",
Sample output from the abve data set is:
Payrate
10283.52
57.692307
20.25
Simply changed the pay rate threshold. I was over thinking this until I read the comment from Thorsten.
CASE WHEN (EMP.PAY_RATE < 600) THEN EMP.PAY_RATE ELSE (EMP.PAY_RATE/2080) END AS "Hourly_Wage"

Calculating over time hours for employees against their contract hours

Calculating over time hours for employees against their individual contract hours, the percentage an employee has worked overtimes as well as how many worked more than 10% overtime.
Sample data is here:
http://sqlfiddle.com/#!9/a2ba19/2/0
Sample data has:
ID- Employee ID
PROJECT - Project code Employee belongs to
SEGMENT - are the projects an employee is working on
SEG_DESC- description of the projects
CONTRACT HRS- is the individual contract hrs the employee is hired to do
HOURS - are the hours an employee has worked in a segment
TEAM- Employee belongs to
END_OF_WEEK - data is collected weekly
I need the
the percentage an employee has worked overtime
how many employees have worked more than 10% overtime
Once these calculations are done, I need to be able to visualise the data in Power BI.
I have actually achieved all I needed to do in Power BI - but the dashboard is extremely slow and therefore not viable. Therefore, the overtime calculations need all to already happen in SQL.
How can I do this?
Please see how it should look:
Looking at your desired output it appears you just need a simple sum of the time per team per Id, in which case the following produces your desired
result:
with ot as (
select *, Sum(hours) over(partition by project, id, team) TimeWorkedPerWeek
from workinghours
)
select *,
TimeWorkedPerWeek-[contract hrs] OverTimeHours,
(TimeWorkedPerWeek)/[CONTRACT HRS]-1 [%Overtime],
case when TimeWorkedPerWeek> [CONTRACT HRS]*1.1 then 1 else 0 end [Overtime > 10%]
from ot
order by id, team
DBFiddle example

MDX Show all months for a person if any of the months have a value of "Y" in a separate dimension

Have a funky issue i'm trying to resolve if you'd be so kind.
Measures: BillableHours
Dimensions:
Personnel (EmployeeId, EmployeeName)
Grouping(EmployeePeriodKey, ActiveFlag)
PeriodCalendarYear, CalendarQuarter, CalendarPeriod)
Grouping dimension has flags and calcs that are particular to a person in a period so the PK is the combo of EmployeeId and CalendarPeriod.
Data As Follows:
EmployeeId CalendarPeriod ActiveFlag BillableHours
123 201501 Y 10
123 201502 Y 20
123 201503 N 30
123 201504 Y 40
People are filtering on "Active Flag" = "Y" and missing the "N" row in the results which is not what is desired. Whatever filter I design needs to be flexible enough that at an employee level I need to know if an employee ever had a value of "Y" JUST the periods selected by the query.
Scenario 1: user selects employee 123 for periods 201501:201504 and filters hypothetical flag to "Y" - Billable Hours should be 100, not 70.
Scenario 2: user selects employee 123 for periods 201501:201503 and filters hypothetical flag to "Y" - Billable Hours should be 60, not 30.
Scenario 3: user selects employee 123 for period 201503 and filters hypothetical flag to "Y" - Billable Hours should be 0, not 30. since in this selected group of periods this person was not active for any period
i'm not interested in all siblings, just the ones at a person level. And if person is not selected I need it to know to perform this check on a person level for the periods filtered for. If they have the following
ActiveFlag: "Y"
Fiscal Year: 2016
Group BillableHours
IT Consulting 1000
HR Consulting 1500
It would be understood that those total amounts represent the hours for every employee who was active for any part of FY2016 whether all 12 months or only 1. If someone was active the year before, but weren't in 2016 they should not show up because I only want to interrogate the flags for the periods selected.
Do you want to see Y value when it's non empty? Otherwise N + Y value?
IIF(
[Measures].[BillableHours] > 0,
([Grouping].[ActiveFlag].[All],[Measures].[BillableHours]),
[Measures].[BillableHours]
)
WhatI needed to accomplish with the above is to have all values for a specific employee evaluated to see if any values for that employee were valid. The key to doing that ended up being using EXISTING. Additionally using NON_EMPTY_BEHAVIOR cut down on evaluation cycles because there was no need to evaluate the rows if they weren't active in a time period. I've posted the MDX below.
CREATE HIDDEN UtilizedFTESummator;
[Measures].[UtilizedFTESummator] = Iif([Measures].[Is Active For Utilization Value] > 0,[Measures].[Period FTE],NULL);
NON_EMPTY_BEHAVIOR([Measures].[UtilizedFTESummator]) = [Measures].[Is Active For Utilization Value];
//only include this measure if the underlying employee has values in their underlying data for active in utilization
CREATE MEMBER CURRENTCUBE.[Measures].[FTE Active Utilization]
AS
SUM
(
EXISTING [Historical Personnel].[Employee Id].[Employee Id],
[Measures].[UtilizedFTESummator]
),VISIBLE=0;

How to Convert a negative value received through a parameter to positive in the stored procedure code?

I have some concerns, I'm creating a stored procedure to work with a cafeteria system. The user on the cafeteria will send me through parameter an employee code and the amount consumed. So I far I have calculated this:
IF #Amount< #Subsidy (Subsidy being 20 Dollars per day)
BEGIN
UPDATE CreditSub
SET Subsidy = Subsidy - #Amount, Fecha = #CurrDate
WHERE Code = #Code;
END
IF #Amount > #Subsidy (Being credit 100)
BEGIN
UPDATE CreditSub
SET #AmountDif= #Amount - Subsidy, Subsidy= 0, Credit = Credit - #AmountDif, Date = #CurrDate
WHERE Code = #Code;
END
What I'm basically doing is a calculation that if the user insert less than 20 dollars it will run the first batch if is greater it's going to run the second deducting the 20 dollars of the subsidy plus deducting the rest of the credit (being 100 Dollars).
Now, if the user cancels the order, the value returned to me will be a negative value like "If the employee orders 70 dollars of food, then the order must be cancelled, the system will return -70 which I need to take and summarize part of it to the subsidy and part of it to the credit if the subsidy was not spent on that day. How do I split the values or rollback the last transaction converting the negative to positive?. Please ask me any questions, I need help here with this.
Thanks a billion!
Break out CreditSub into two tables by #Code. The Credit table will track the credit amount throughout the day--positive or negative won't matter. The Subsidy table would be populated once per day (or as needed) with the subsidy amount. Then at the end of the day, you can do the math when you populate your other table. If you need to know what the current subsidy amount is, just join the two tables and perform the necessary calculation to get the amount.

how to calculate total number of L Prasent in my table with in 1 year for a person

I want to make a web app for salary management. In which I want to calculate total number of leaves with in a year if there is more than 16 L present in my table than it will count as A absent.
How to make procedure for this?
Any help is appreciated.
Perhaps this could work.
SELECT
EmployeeID,
DaysAbsent=(SELECT COUNT(DISTINCT aDate)FROM aDateTable
WHERE Absent=1 and EmployeeID=Employees.EmployeeID)
FROM
Employess