Getting the sum of three rows from one column? - sql

I have a table, the data in it looks similar to this:
user type hours used --to make another column with this info available
2334 sick 10.000 2.000 8.000
2334 vacation 48.000 56.000 -8.000
2334 personal 0 8.000 0
1356 sick 0 16.000 -16.000
1356 vacation 80.000 0 80.000
1356 personal 14.000 14.000 0
4355 sick 4.000 1.000 3.000
4355 vacation 112.000 0 112.000
4355 personal 8.000 0 8.000
I had to sum up the values in the hours column, but only for each user. So user 2334 would have earned 58.000 hours, 10.000 + 48.000 + 0 but would have used 66.000 hours resulting in -8.000 hours.

SELECT user, SUM(hours) totalHours
FROM tableName
GROUP BY user
SEE SQLFiddle Demo

Related

How to merge tables without duplicates

I am using Firebird 3, and have 2 tables: Income and Expenses, each table has a VatAmount and a Date field.
I want to merge this 2 tables in a query, avoid duplicate values and set a filter range between month dates, to get a result like this:
Month
Income VatAmount
Expenses VatAmount
Difference
Jan
1.000
500
500
Feb
3.000
1.000
2.000
Mar
700
2.000
1.300
I have been searching for a solution, but no luck so far.

Date dependent calculation from 2 dataframes - average 6-month return

I am working with the following dataframe, I have data for multiple companies, each row associated with a specific datadate, so I have many rows related to many companies - with ipo date from 2009 to 2022.
index ID price daily_return datadate daily_market_return mean_daily_market_return ipodate
0 1 27.50 0.008 01-09-2010 0.0023 0.03345 01-12-2009
1 2 33.75 0.0745 05-02-2017 0.00458 0.0895 06-12-2012
2 3 29,20 0.00006 08-06-2020 0.0582 0.0045 01-05-2013
3 4 20.54 0.00486 09-06-2018 0.0009 0.0006 27-11-2013
4 1 21.50 0.009 02-09-2021 0.0846 0.04345 04-05-2009
5 4 22.75 0.00539 06-12-2019 0.0003 0.0006 21-09-2012
...
26074 rows
I also have a dataframe containing the Market yield on US Treasury securities at 10-year constant maturity - measured daily. Each row represents the return associated with a specific day, each day from 2009 to 2022.
date dgs10
1 2009-01-02 2.46
2 2009-01-05 2.49
3 2009-01-06 2.51
4 2009-01-07 2.52
5 2009-01-08 2.47
6 2009-01-09 2.43
7 2009-01-12 2.34
8 2009-01-13 2.33
...
date dgs10
3570 2022-09-08 3.29
3571 2022-09-09 3.33
3572 2022-09-12 3.37
3573 2022-09-13 3.42
3574 2022-09-14 3.41
My goal is to calculate, for each ipodate (from dataframe 1), the average of the previous 6-month return of the the Market yield on US Treasury securities at 10-year constant maturity (from dataframe 2). The result should either be in a new dataframe or in an additionnal column in dataframe 1. Both dataframes are not the same length. I tried using rolling(), but it doesn't seem to be working. Anyone knows how to fix this?
# Make sure that all date columns are of type Timestamp. They are a lot easier
# to work with
df1["ipodate"] = pd.to_datetime(df1["ipodate"], dayfirst=True)
df2["date"] = pd.to_datetime(df2["date"])
# Calculate the mean market yield of the previous 6 months. Six month is not a
# fixed length of time so I replaced it with 180 days.
tmp = df2.rolling("180D", on="date").mean()
# The values of the first 180 days are invalid, because we have insufficient
# data to calculate the rolling mean. You may consider extending df2 further
# back to 2008. (You may come up with other rules for this period.)
is_invalid = (tmp["date"] - tmp["date"].min()) / pd.Timedelta(1, "D") < 180
tmp.loc[is_invalid, "dgs10"] = np.nan
# Result
df1.merge(tmp, left_on="ipodate", right_on="date", how="left")

How can I group and get MS Access query to show only rows with a maximum value in a specified field for a consecutive number of times?

I have a large access table that I need to pull specific data from with a query.
I need to get a list of all the IDs that meet a specific criteria, i.e. 3 months in a row with a cage number less than 50.
The SQL code I'm currently working with is below, but it only gives me which months of the past 3 had a cage number below 50.
SELECT [AbBehWeeklyMonitor Database].AnimalID, [AbBehWeeklyMonitor Database].Date, [AbBehWeeklyMonitor Database].Cage
FROM [AbBehWeeklyMonitor Database]
WHERE ((([AbBehWeeklyMonitor Database].Date)>=DateAdd("m",-3,Date())) AND (([AbBehWeeklyMonitor Database].Cage)<50))
ORDER BY [AbBehWeeklyMonitor Database].AnimalID DESC;
I would need it to look at the past 3 months for each ID, and only output if all 3 met the specific criteria, but I'm not sure where to go from here.
Any help would be appreciated.
Data Sample:
Date
AnimalID
Cage
6/28/2022
12345
50
5/19/2021
12345
32
3/20/2008
12345
75
5/20/2022
23569
4
8/20/2022
23569
4
5/20/2022
44444
71
8/1/2012
44444
4
4/1/2022
78986
30
1/20/2022
78986
1
9/14/2022
65659
59
8/10/2022
65659
48
7/14/2022
65659
30
6/14/2022
95659
12
8/14/2022
91111
51
7/14/2022
91111
5
6/14/2022
91111
90
8/14/2022
88888
4
7/14/2022
88888
5
6/14/2022
88888
15
Consider:
Query1:
SELECT AnimalID, Count(*) AS Cnt
FROM Table1
WHERE (((Cage)<50) AND (([Date]) Between #6/1/2022# And #8/31/2022#))
GROUP BY AnimalID
HAVING (((Count(*))=3));
Query2
SELECT Table1.*
FROM Query1 INNER JOIN Table1 ON Query1.AnimalID = Table1.AnimalID
WHERE ((([Date]) Between #6/1/2022# And #8/31/2022#));
Output:
Date AnimalID Cage
6/14/2022 65659 12
7/14/2022 65659 30
8/10/2022 65659 48
6/14/2022 88888 15
7/14/2022 88888 5
8/14/2022 88888 4
Date is a reserved word and really should not use reserved words as names.

Summing Hours Worked Based On Two Unique Identifiers

I want to sum up the total hours worked in a given two week period (pay period) for employees in the company. I have a view that pulls a column for unique employee identifiers [CODE_USER], a column for uniquely identified pay types (Regular, Overtime, Holiday, Vacation, etc.) [Code], a column for total hours worked [Hours], and a column for each day of the workweek [Day].
As it stands right now, the [Hours] column shows total hours worked on a per day basis for each unique employee (based on the unique pay type, such as regular hours or overtime hours worked).
I need to combine all hours worked over a two week period for each employee [CODE_USER], for each pay type [CODE] into a summarized column named 'Hours'.
An ideal end result would look something like the following, given employee ID worked 80 regular hours, and 20 overtime hours over the course of two weeks (E1 equals Regular hours, E2 equals Overtime hours):
CODE_USER Code Hours
125 E1 80.00
125 E2 20.00
The closest I think I have gotten to solving it would be the following code, however it does not SUM hours worked for a unique CODE_USER for the two week period, it lists the hours worked for each day during the two week period as a collection of rows for that employee. For example, the following code shows 18 rows for the employee ID 125, the employee worked 10 full 8.00 hour days during the time period marked by E1 (regular), and there were 8 times where the employee worked overtime hours marked by E2 (overtime).
CODE:
SELECT [CODE_USER],
[Code],
SUM(Hours) AS Hours,
[Day]
FROM [LookUp].[dbo].[Daily_Hours_Worked]
WHERE [Day] >= '20191007' AND [Day] < '20191019'
AND [CODE_USER] LIKE '%125%'
GROUP BY [CODE_USER], [Code], [Hours], [Day]
ORDER BY [CODE_USER], [Day] DESC;
RESULTS:
CODE_USER Code Hours Day
125 E1 8.00 2019-10-18 00:00:00.000
125 E2 0.70 2019-10-18 00:00:00.000
125 E1 8.00 2019-10-17 00:00:00.000
125 E2 1.65 2019-10-17 00:00:00.000
125 E1 8.00 2019-10-16 00:00:00.000
125 E2 1.15 2019-10-16 00:00:00.000
125 E1 8.00 2019-10-15 00:00:00.000
125 E2 0.97 2019-10-15 00:00:00.000
125 E1 8.00 2019-10-14 00:00:00.000
125 E2 1.99 2019-10-14 00:00:00.000
125 E1 8.00 2019-10-11 00:00:00.000
125 E2 0.12 2019-10-11 00:00:00.000
125 E1 8.00 2019-10-10 00:00:00.000
125 E2 0.05 2019-10-10 00:00:00.000
125 E1 8.00 2019-10-09 00:00:00.000
125 E2 0.10 2019-10-09 00:00:00.000
125 E1 7.99 2019-10-08 00:00:00.000
125 E1 7.99 2019-10-07 00:00:00.000
EXPECTED RESULTS:
I want to see a SUM of E1, E2, etc., for the input pay period (2 week period) for each unique Employee ID [CODE_USER] in the table. The end result should be two rows for each employee with Regular Time (E1) and Overtime (E2) that SUMs that employee's hours worked for each category over the given time period.
Is it not simply that you should remove the day from the grouping and the specific employee from the where clause?
SELECT [CODE_USER],
[Code],
SUM(Hours) AS Hours
FROM [LookUp].[dbo].[Daily_Hours_Worked]
WHERE [Day] >= '20191007' AND [Day] < '20191019'
GROUP BY [CODE_USER], [Code]
ORDER BY [CODE_USER]
You don't need to group by hours; you're summing it. Situations where you should group by a column that you're also aggregating are rare
I'm confused as to why you say two weeks but the dates in your where clause are not two weeks apart; what if someone works on a weekend? I've left this part, just wanted to raise it as it seems odd that you'd do 12 days ie include only every other weekend (if the job is run once a fortnight)

How to join twice from same table. Group by same column

I have a litte problem here. I want to join twice from the same table, and group by a common value.
Here is the dataset (from table Voucher):
Date (dd/mm/yyyy) Amount
--------------------------
01.01.2010 1.000
15.01.2010 2.000
01.03.2010 3.000
01.03.2010 4.000
01.05.2010 5.000
01.01.2011 1.000
01.02.2011 2.000
01.04.2011 3.000
15.04.2011 4.000
01.05.2011 5.000
The result should be like this:
Month Amount 2010 Amount 2011
---------------------------------
1 3.000 1.000
2 2.000
3 7.000
4 7.000
5 5.000 5.000
How do I solve this?
Something along these lines will work for this case:
SELECT
DATEPART(month,[Date]) as Month,
SUM(CASE WHEN DATEPART(year,[Date]) = 2010 THEN Amount END) as [Amount 2010],
SUM(CASE WHEN DATEPART(year,[Date]) = 2011 THEN Amount END) as [Amount 2011]
FROM
Voucher
GROUP BY
DATEPART(month,[Date]) as Month
For other situations, you might want to look into PIVOT.
Date is a really poor name for a column.
You have to give the tables different names like in this example:
SELECT Fruit1, F1FruitName = F1.FruitName, F1FruitCost = F1.FruitCost,
Fruit2, F2FruitName = F2.FruitName, F2FruitCost = F2.FruitCost FROM T1
JOIN T2 F1 ON Fruit1 = F1.Fruit_ID
JOIN T2 F2 ON Fruit2 = F2.Fruit_ID
The concrete syntax depends on your database system.