Calculate Hourly Pay Based on Pay Rate Column - sql

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"

Related

AWS QuickSight: maxOver() calculated field?

I am a stock trader who visualizes data in QuickSight. I identify the trades I want to submit to the market, sometimes for the same stock, at the same time, but in opposite directions depending on the price of the stock at that time. See below for an example of trades I might identify for 1/19/22 0800:
Date
Hour
Stock
Direction
Price
Volume
1/19/22
0800
Apple
BUY
$10
2
1/19/22
0800
Apple
SELL
$20
1
1/19/22
0800
Microsoft
BUY
$15
3
Using QuickSight, I want to visualize (in pivot tables and charts) the volume that I trade, using the maximum possible trade volume. For example, QuickSight simply sums the Volume column to 6, when really I want it to sum to 5, because the max possible trade volume for that hour is 5 (the Apple trades in the example are mutually exclusive, because the stock price cannot be both beneath $10, triggering a BUY, and above $20, triggering a SELL at the same date-time. Therefore, I want the day's traded volume to reflect the MAX possible volume I could have traded (2+3)).
I have used the maxOver() function as so: maxOver({volume}, [{stock}, {date}, {hour}], PRE_AGG), but I would like to view my trade volume rolled up to the day as so:
Date
Volume
1/19
5
Is there a way to do this using QuickSight calculated fields? Should this aggregation be done with a SQL custom field?
Add a new calculated field called
volume_direction_specifier
{Volume} * 10 + ifelse({Direction}='BUY', 1, 2)
This is a single number that will indicate the direction and volume. (this is needed in cases where the max possible volume is the same for both the BUY and SELL entries within the same hour).
Then compute the maxOver on this new field in a calculated field called max_volume_direction_specifier
maxOver({volume_direction_specifier}, [{stock}, {date}, {hour}], PRE_AGG)
Add a new field which will give the Volume for rows that have the max volume_direction_specifier per hour
volume_for_max_trade_volume_per_hour
ifelse(volume_direction_specifier = max_volume_direction_specifier, {volume}, null)
And finally, you should be able to add volume_for_max_trade_volume_per_hour to your table (grouped by day) and its SUM will give the maximum possible trade volume per day.

How to get percentage Difference in qliksense

I need one help.
I calculate a Difference Between Current Year Sale and Last Year
like.
num(Sum({$<[HSUBSEGM.descr]={"Clinker and Cement"},
[CALYEAR]={">=$(vCurrentYear)"}>}[_volume_SO]),'##.0')- num(Sum({$<[HSUBSEGM.descr]
={"Clinker and Cement"},[CALYEAR]={">
=$(vPreviousYear)<=$(vPreviousYear)"}>}[_volume_SO]),
'##.0')
so I m getting and answer Suppose.
This Year sale is 100 $ and Last Year sale was 50 $.
so total growth 50 $.
But I want to show growth in %.
In this case my Revenue growth is 50 % because it got double from last year..
the mathematical answer should be of this form
(sum(CurrentYearSales)-sum(PreviousYearSales))
/ sum(PreviousYearSales)
So using your example as a guide:
I removed some of the num() functions and applied it to only the final result and simplified the set analysis for vCurrentYear and vPreviousYear. That is purely my own style choices.
Give this a go
num(
(Sum({$<[HSUBSEGM.descr]={"Clinker and Cement"},[CALYEAR]={$(vCurrentYear)}>}[_volume_SO])
- Sum({$<[HSUBSEGM.descr]={"Clinker and Cement"},[CALYEAR]={$(vPreviousYear)}>}[_volume_SO]))
/
Sum({$<[HSUBSEGM.descr]={"Clinker and Cement"},[CALYEAR]={$(vPreviousYear)}>}[_volume_SO])
,'##0.00%')

SQL Weighted Average - Population & Income

I am trying to find the average household income while using a weighted average. I have a data source of ZIP codes with the total population and the average household income. I want to be able to select multiple ZIP codes and still pull an accurate average household income.
Can I use SQL to pull a weighted average like this?
ZIP
TOTAL_POP
AVG_HH_INC
12345
130350
66000
54321
55750
78000
44668
17300
89000
If you want the overall average, then use arithmetic:
select sum(total_pop * avg_hh_inc) / sunm(total_pop)
from t;
Note: If the values are stored as 4-byte integers, then this runs the risk of overflow. Just use a different numeric representation if that is an issue.

SQL - Finding Percent Of a Total and subtracting to get two new totals

In my SQL course I'm trying to answer a problem in which i'm being asked to find X% of a Total then subtracting that result from the original total to produce another result. Then putting these results (the X% of the total and the total-X% of total) in two new columns.
Example: We need to know how much money we owe Tom and Ted. We have total up sales to $1,000,000.00. We owe Tom 75% of that total. The remainder goes to Ted.
I can't seem to find anything in my readings/videos about this nor a google search that isn't an answers that produces ratios or comparing to other records in the table. Also, not sure about how to get the results into their own columns. Thanks for any advice!
Example of what I got so far:
SELECT SUM(Sale_Amount) From Order_Table
Now I have to find the % of that SUM then subtract it from the SUM and push both results to two new columns, one for the percent of the SUM(Sale_Amount) and one for the remainder.
Given it's an SQL course (and it's not 100% clear what's being asked), I'm not going to give you the total answer, but I'll give you components but you'll need to understand them to put them together.
In SQL, you can
Get totals using SUM and GROUP BY
Do normal maths e.g., SELECT 10000 * 60/100 to get percentages of totals
'Save' results by a) having columns/fields to save them in, and b) UPDATE those fields with relevant data
Note if you're not saving data, and simply reporting them, you can just add those to a SELECT statement e.g., SELECT 100000 AS Total, 100000 * 0.75 AS Toms_Share, 100000 * 0.25 AS Teds_Share.

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.