I have estimated time on a job but when I add the employee's (in this case 2) hours, it will duplicate the estimated - sql-server

I have estimated time on a job but when I add the employee's (in this case 2) hours, it will duplicate the estimated. I need to divide by the number of results (maybe employee records) to get the correct answer.
SQL pull from database.
SELECT
LaborDtl.JobNum,
LaborDtl.ClockInDate,
LaborDtl.OprSeq,
EmpBasic.Name,
(LaborDtl.LaborHrs) as [TotalHrs],
((JobOper.EstSetHours + JobOper.EstProdHours) / (COUNT (EmpBasic.Name))) as [TotEstHrs],
LaborDtl.ResourceGrpID
FROM Erp.LaborDtl
left outer JOIN Erp.JobOper ON
JobOper.JobNum = LaborDtl.JobNum
AND JobOper.OprSeq = LaborDtl.OprSeq
JOIN Erp.EmpBasic ON
EmpBasic.EmpID = LaborDtl.EmployeeNum
WHERE LaborDtl.Complete = '1'
AND LaborDtl.ClockInDate = '2019-7-1'
AND LaborDtl.ResourceGrpID = '5-XM-C'
AND LaborDtl.JobNum = 'PA16742'
GROUP BY
LaborDtl.JobNum,
LaborDtl.ClockInDate,
LaborDtl.OprSeq,
EmpBasic.Name,
LaborDtl.LaborHrs,
JobOper.EstSetHours,
JobOper.EstProdHours,
LaborDtl.EmployeeNum,
LaborDtl.ResourceGrpID
JobNum ClockInDate OprSeq Name TotalHrs TotEstHrs ResourceGrpID
pa16742 2019-07-01 20 Jerry Adam 1.6300 5.00 5-XM-C
PA16742 2019-07-01 20 Xue Lee 2.68000 5.00 5-XM-C
In this case, the TotEstHrs should be 2.5 on each line.

I think this does what you want:
((JobOper.EstSetHours + JobOper.EstProdHours) / SUM(COUNT(EmpBasic.Name))
OVER ()) as [TotEstHrs],
It adds the count over all the rows and then does the division.

Related

HOW TO FILTER DJANGO QUERYSETS WITH MULTIPLE AGGREGATIONS

Lets say I have a django model table
class Table(models.Model):
name = models.CharField()
date_created = models.DatetimeField()
total_sales = models.DecimalField()
some data for context
Name
date-created
total-sales
a
2020-01-01
200
b
2020-02-01
300
c
2020-04-01
400
*
**********
***
c
2020-12-01
1000
c
2020-12-12
500
now I want to filter an aggregate of
total_yearly_sales = 10500
current month being December
total_monthly_sales = 1500
daily_sales
total_daily_sales = 500
also do a Group by by name
models.Table.objects.values('Name').annotate(Sum('total-sales')).order_by()
I want to do this in one query(one db hit)
Hence the query should generate
total_yearly_sales
total_monthly_sales
total_daily_sales
total_sales_grouped_by_name ie {a:200, b:300, c:1900}
I know this is too much to ask. Hence let me express my immense gratitude and thanks for having a look at this.
cheers
The above queries I can generate them individually like so
today = timezone.now().date()
todays_sales = models.Table.filter(date_created__date__gte=today, date_created___date__lte=today).aggregate(Sum('total_sales'))
=> 500
monthly_sales(this month) = models.Table.objects.filter(date_created__year=today.year, date_created__month=today.month).aggregate(Sum('total_sales'))
=>10500
total_yearly_sales = models.Table.objects.filter(date_created__year=today.year).aggregate(Sum('total_sales')) => 10500

How to know how much disk space a table occupies?

code as follow
db=database("dfs://db1",VALUE,1 2 3)
timestamp = [09:34:07,09:36:42,09:36:51,09:36:59,09:32:47,09:35:26,09:34:16,09:34:26,09:38:12]
sym = `C`MS`MS`MS`IBM`IBM`C`C`C
price= 49.6 29.46 29.52 30.02 174.97 175.23 50.76 50.32 51.29
qty = 2200 1900 2100 3200 6800 5400 1300 2500 8800
t = table(timestamp, sym, qty, price)
dt=db.createTable(t,`dt).append!(t)
How much disk space does this table DT consume?
You can use the getTabletsMeta function to query the disk space usage of a partition table. The code is as below, and the unit of the return value is Byte:
def diskUsage(database, table){
return select sum(diskUsage) from getTabletsMeta("/"+database+"/%", table, true, -1);
}
pnodeRun(diskUsage{"db1", "t1"})

Parking fee using C

Beginner here. I just wanted to know how and where I went wrong with this code. I’m having problem with using the modulo for the time. The following is the problem and the code I created.
A company wants to create a system that automatically computes for the total fee of the parking services in their new mall.
The user first needs to input their vehicle type, time-in and time-out. Then, it will compute for the total fee of the parking service based on the time the vehicle spent parked. Use the table below as basis:
Vehicle Type First 3 hours After 3 hours
M-otorcycle FREE PHP 10.00/hr
C-ar PHP 30.00 PHP 10.00/hr
T-ruck PHP 50.00 PHP 25.00/hr
Assumptions:
User will not input a value wherein Timeout < Timein.
Max Inputs will for time will be from 0000 to 2359, and it will always follow that form (user will not input values like 13, 17, 110).
Input Format
Vehicle Type, Time-in, and Time-out
Input Sample
M
1230
1430
Output Format
Payment, hours and minutes in this format.
Output Sample
Payment:·0.00
Hours:·2
Minutes:·0
Code:
#include<stdio.h>
int main()
{
double payment;
int time, entry,exit;
int hours, minutes;
char type;
scanf("%c %d %d", &type, &entry, &exit);
hours = (exit-entry)/100;
minutes = (exit-entry) %60;
if (type == 'M' && time > 3)
payment = hours*10;
else
payment = hours*0;
if (type=='C' && time >= 3)
payment = ((hours-3)*10)+30;
else
payment = 30;
if (type == 'T' && hours >= 3)
payment = ((hours-3)*25)+50;
else
payment = 50;
printf("Payment: %.2f \nHours: %d \nMinutes: %d ", payment, hours, minutes);
return 0;
}
Can u send the table clearly.
And also,
minutes = abs((exit/100) - (entry/100));
No use of modulus!

Insights on approaching fare calculations

Basically I'm attempting a question on how to compute fee. However, my code is extremely long. I ended up using a heck tons of 'if' and 'else' for my code. I was wondering if there is any better way on approaching this question.
I initially attempted to use loops to keep stacking the fee but I face a problem when my time cross the critical juncture. I added time to TimeIn along with my fees, but as it crosses the 7am mark, I need to reset it back to 7am so that my fees will be charge properly. And that was when I gave up when my code became very long too.
Eg. loop adds from 0630 to 0730, fees are properly increased, the first 30 minutes of 0700 will be skipped and fees are wrongly charged.
The question I attempted:
Mr. Wu has been going to work every day by taxi for many years. However, the taxi fare has been increasing rather quickly in recent years. Therefore, he is considering driving to work instead.
One of the costs for driving is the parking fee. The parking rates of the car park at Mr. Wu’s workplace are as shown in the table below.
Weekday Saturday Sunday
4am ~ 7am $2.00 per hour $2.50 per hour $5
7am ~ 6pm $1.20 per 30 minute $1.50 per 30 minutes per
6pm ~ midnight $5.00 per entry $7.00 per entry entry
Special note:
1. The car park opens at 4am and closes at midnight. All vehicles must leave
by midnight.
2. There is a grace period of 10 minutes on any day (i.e., it is completely
free to park for 10 minutes or less regardless of day and time.)
3. There is a 10% surcharge for parking more than 10 hours on a weekday and
20% for Saturday. There is no surcharge for Sunday.
4. There is an additional $3.00 fee for exiting after 10pm on any day.
(Surcharge is not applicable on this fee.)
Your program should read in one integer, which is an integer between 1 and 7
representing the day of the week (1 being Monday and 7 being Sunday). It should also
read in two numbers representing the time-in and time-out in 24-hour format. It
should then calculate and display the parking fee (with two decimal places).
You may assume that the inputs are valid (i.e., the day is within the specified range,
both time-in and time-out are between 4am and midnight in 24-hour format, and timeout
is no earlier than time-in).
My extremely long code:
#include <stdio.h>
#include <math.h>
double computeFee(int, int, int);
int main(void){
int day, timeIn, timeOut;
scanf("%d %d %d", &day, &timeIn, &timeOut);
printf("Enter day: %d\n", day);
printf("Enter time-in: %d\n", timeIn);
printf("Enter time-out: %d\n", timeOut);
printf("Parking fee is $%.2lf\n", computeFee(day, timeIn, timeOut));
return 0;
}
double computeFee(int day, int timeIn, int timeOut){
double fee = 0;
double TimeIn, TimeOut;
TimeIn = 60*(floor(timeIn/100)) + (timeIn%100);
TimeOut = (60*(floor(timeOut/100)) + (timeOut%100));
if((day>=1)&&(day<=5)){
if(TimeIn<420){
if(TimeOut<420){
fee += 2*ceil((TimeOut - TimeIn)/60);
}
else{
fee += 6;
if(TimeOut>=1080){
fee += 31.4;
}
else{
fee += 1.2*ceil((TimeOut - 420)/30);
}
}
}
if(TimeIn>=420){
if(TimeIn>=1080){
fee = 5;
}
else{
if(TimeOut>=1080){
fee += (1.2*ceil((1080 - TimeIn)/30) + 5);
}
else{
fee += (1.2*ceil((TimeOut - TimeIn)/30));
}
}
}
if((TimeOut-TimeIn)>=600){
fee *= 1.1;
}
}
if(day == 6){
if(TimeIn<420){
if(TimeOut<420){
fee += (2.5*ceil((TimeOut - TimeIn)/60));
}
else{
fee += 7.5;
if(TimeOut>=1080){
fee += 40;
}
else{
fee += (1.5*ceil((TimeOut - 420)/30));
}
}
}
if(TimeIn>=420){
if(TimeIn>=1080){
fee = 7;
}
else{
if(TimeOut>=1080){
fee += (1.5*ceil((1080 - TimeIn)/30) + 7);
}
else{
fee += (1.5*ceil((TimeOut - TimeIn)/30));
}
}
}
if((TimeOut-TimeIn)>=600){
fee *= 1.2;
}
}
if(day == 7){
fee = 5;
}
if((timeOut/100)>=22){
fee += 3;
}
if((timeOut - timeIn)<=10){
fee = 0;
}
return fee;
}
Examples on how fees are calculated:
Example 1: Tuesday, 4:29am to 7:50am.
• 4:29am to 7am is charged as 3 1-hour slots: $2.00 * 3 = $6.00
• 7am to 7:50am is charged as 2 30-minute slots: $1.20 * 2 = $2.40
• Total fee = $6.00 + $2.40 = $8.40
Example 2: Saturday, 7:01am to 7:49pm.
• 7:01am to 6pm is charged as 22 30-minute slots: $1.50 * 22 = $33.00
• 6pm to 7:49pm is charged as one entry: $7.00
• 20% Surcharge for parking more than 10 hours: ($33.00 + $7.00) * 20% =
$8.00
• Total fee = $33.00 + $7.00 + $8.00 = $48.00
Example 3: Sunday, 3pm to 10:01pm.
• 3pm to 10:01pm is charged as one entry: $5.00
• Additional fee for exiting after 10pm: $3.00
• Total fee = $5.00 + $3.00 = $8.00
Example 4: Thursday, 11:49pm to 11:59pm.
• Grace period
• Total fee = $0.00
Example 5: Monday, 12pm to 10:01pm.
• 12pm to 6pm is charged as 12 30-minute slots: $1.20 * 12 = $14.40
• 6pm to 10:01pm is charged as one entry: $5.00
• 10% Surcharge for parking more than 10 hours: ($14.40 + $5.00) * 10% =
$1.94
• Additional fee for exiting after 10pm: $3.00
• Total fee = $14.40 + $5.00 + $1.94 + $3.00 = $24.34
Thanks for reading my long question. And thanks in advance for the help.
note: I havent learn arrays and anything beyond that. Only learn loops and selection statement so far(reading K&R programming tutorial, until chapter 17, using Online GeekforGeek as compiler). However, I will still greatly appreciate solutions using other methods.

How to pair row for arrival and departure

FPL_ID AFSKEY FLIGHTNO FLIGHTYPE STAD AIRCRAFTTYPECODE TAILNO STANDCODE
1733285 4383931 UL 0314 A 2014-01-01 05:35:00.000 A343 4RADC C015
1733554 4382525 UL 0315 D 2014-01-01 08:25:00.000 A343 4RADC C015
1733385 4382929 AK 5107 A 2014-01-01 07:00:00.000 A320 9MAFB F086
1733484 4381571 AK 5212 D 2014-01-01 07:25:00.000 A320 9MAFB F086
I need help.
How to pair base on FLIGHTYPE A=Arrival and D=Departure into a single row ?
Just inner join the same table again. In the example below, f1 will contain arrivals and f2 departures.
select f1.*, f2.* -- replace with the list of columns you need
from flights f1
inner join flighs f2
on f1.FLIGHTNO = f2.FLIGHTNO
and f1.FPL_ID <> f2.FPL_ID
and f1.FLIGHTYPE = 'A' and f2.FLIGHTYPE = 'D'

Resources