Query for taking data between two specific months and two specific year in postgresql - postgresql-9.1

I want to get data between From Month1 year1 to Month2 year2
Example From Jan 2016 to Jan 2017.
I have the query like
select * from customer cr where
EXTRACT(MONTH FROM cr.regdate) between Month1 and Month2 AND
EXTRACT(YEAR FROM cr.regdate) between Year1 and Year2
This query is giving wrong result when I would like to take the data from Jan 2016 to Jan 2017. I will be expecting the data from jan 2016, Feb 2016, Mar 2016 till Jan 2017.
But it is giving the result only for Jan 2016 and Jan 2017. Please guide me.

Related

Convert fromJSON list to a data frame

I am getting data from BLS website using the package blsAPI.
The code is:
library(blsAPI)
employ <- blsAPI(payload= "CES0500000001")
emp <- fromJSON(employ)
The data set emp is a list... this is where I am stumped. I've been trying all types of variations to convert emp to data.frame from list with no success.
Just set the argument return_data_frame = TRUE of blsAPI function. data.frame will be returned instead of list (default behaviour).
library(rjson)
library(blsAPI)
response <- blsAPI("CES0500000001", return_data_frame = TRUE)
head(response)
Output:
year period periodName value seriesID
1 2018 M08 August 126939 CES0500000001
2 2018 M07 July 126735 CES0500000001
3 2018 M06 June 126582 CES0500000001
4 2018 M05 May 126390 CES0500000001
5 2018 M04 April 126130 CES0500000001
6 2018 M03 March 125956 CES0500000001

Getting difference between two dates in days

I have a specific problem where I have records in my DB table like the following:
LastUpdated
10 January 2017
(The dates are stored in the database as a DateTime type.)
Now I need to fetch the difference in days between today's date and the last one including today's date. So, for example, today is the 12th, so the amount of days would be 2.
Now the second part of the problem is that I have another table setup like the following:
TransactionDate
1 January 2017
2 January 2017
3 January 2017
4 January 2017
5 January 2017
6 January 2017
7 January 2017
8 January 2017
9 January 2017
10 January 2017
So now after I perform a LINQ the updated results in my DBTable would look like the following:
3 January 2017
4 January 2017
5 January 2017
6 January 2017
7 January 2017
8 January 2017
9 January 2017
10 January 2017
11 January 2017
12 January 2017
So basically I'm trying to get the difference between the current date and the last updated date and then add it to the transaction details table. Upon adding the difference between two dates, I want to remove as much as the days in difference has been added, so that the total date span remains 10 days...
Can someone help me out with this?
Edit: this is the code so far:
var usr = ctx.SearchedUsers.FirstOrDefault(x => x.EbayUsername == username);
var TotalDays = (DateTime.Now - usr.LastUpdatedAt).Value.TotalDays;
Is this a correct way of fetching the difference between two dates like I've mentioned above?
Now after this I perform an HTTP request where I get the remaining two dates and insert it like:
ctx.TransactionDetails.Add(RemainingTwoDates);
ctx.SaveChanges();
Now I have dates expanding from 1st January to 12th of January, but I want to remove 1st and 2nd of January so that the total range of days stays 10;
How can I do this ?
You can remove transaction dates that are older than 10 days ago.
ctx.TransactionDetails.Add(RemainingTwoDays);
//Now you want to remove those older than 10 days
var today = DateTime.Today;
var tenDaysAgo = today.AddDays(-10);
var oldTrandactions = ctx.TransactionDetails.Where(t => t.TransactionDate <= tenDaysAgo).ToList();
foreach (var t in oldTrandactions) {
ctx.TransactionDetails.Remove(t);
}
ctx.SaveChanges();

Need clarification on the calculation of average polarity score returned by sentiment function of sentimentr(trinker)

I am using sentiment analysis function sentiment_by() from R package sentimentr (by trinker). I have a dataframe containing the following columns:
review comments
month
year
I ran the sentiment_by function on the dataframe to find the average polarity score based on the year and month and i get the following values.
review_year review_month word_count sd ave_sentiment
2015 March 8722 0.381686065 0.163440921
2015 April 7758 0.387046768 0.158812775
2015 May 7333 0.389256472 0.149220636
2015 November 14020 0.394711478 0.14691745
2016 February 7974 0.400406931 0.142345278
2015 September 8238 0.379989344 0.141740366
2015 February 7642 0.361415304 0.141624745
2015 December 24863 0.387409099 0.141606892
2016 March 8229 0.389033232 0.138552943
2016 January 10472 0.388300946 0.134302612
2015 August 7520 0.3640285 0.127980712
2016 May 3432 0.422246851 0.125041218
2015 June 8678 0.356612924 0.119333949
2015 January 9930 0.351126449 0.119225549
2016 April 9344 0.397066458 0.111879315
2015 July 8450 0.349963536 0.108881821
2015 October 7630 0.38017201 0.1044298
Now i run the sentiment_by function on the dataframe based on the comments alone and then i run the following function on the resultant data frame to find the average polarity score based on year and months.
sentiment_df[,list(avg=mean(ave_sentiment)),by="month,year"]
I get the following results.
month year avg
January 2015 0.110950199
February 2015 0.126943461
March 2015 0.146546669
April 2015 0.148264268
May 2015 0.143924126
June 2015 0.110691204
July 2015 0.106472437
August 2015 0.118976304
September 2015 0.135362187
October 2015 0.111441484
November 2015 0.137699548
December 2015 0.136786867
January 2016 0.128645808
February 2016 0.129139898
March 2016 0.134595706
April 2016 0.12106743
May 2016 0.142801514
As per my understanding both should return the same results, correct me if I am wrong. Reason for me to go for the second approach is because i need to average polarity based on both month and year, as well as based on months and i don't want to use the method twice as it will cause additional time delay. Could some one let me know what i am doing wrong here?
Here is an idea: Maybe the first function is taking the averages from the individual sentences, and the second one is taking the average from the "ave sentiment", which is already an average. So, the average of averages is not always equal to the average of the individual elements.

Setting Up a Leave Plan Table in PeopleSoft 9.1

I'm challenged with a Leave Table setup issue and would like some guidance.
Background: I have a division at work where they do not accumulate any vacation time on their first year of service. All the accrued vacation time are backloaded and you receive the hours the following calendar year based on the previous year's service. I am having issues setting up the accrual service for the First Year Award Values because when I try to set the "Month Eligible" field to 13, it gives me an error. Screenshots can be provided or I can try to explain this better. But I'm up for any suggestions since I have a test environment to play around with this setup
Example 1:
DOH = jan 1, 2015 on Jan 1, 2016; member would accrue 10 days based on the service from Jan 1, 2015 to Dec 31, 2015 on Jan 1, 2017; member would accrue 10 days based on the service from Jan 1, 2016 to Dec 31, 2016
The breakdown for the 1st year of service is prorated based on month of hire:
Example 2:
DOH = feb 1, 2015 on Jan 1, 2016; member would accrue 9 days based on the service from Feb 1, 2015 to Dec 31, 2015 on Jan 1, 2017; member would accrue 10 days based on the service from Jan 1, 2016 to Dec 31, 2016
Example 3:
DOH = mar 1, 2015 on Jan 1, 2016; member would accrue 8 days based on the service from Feb 1, 2015 to Dec 31, 2015 on Jan 1, 2017; member would accrue 10 days based on the service from Jan 1, 2016 to Dec 31, 2016
with continuing the breakdown until the 12th month.
Example 4:
DOH = dec 1, 2015 on Jan 1, 2016; member would accrue 0 days based on the service from Dec 1, 2015 to Dec 31, 2015 on Jan 1, 2017; member would accrue 10 days based on the service from Jan 1, 2016 to Dec 31, 2016
Will this be part of the "Special Calculation Routine" checkbox?
I suggest using the Service Calc at Year Begin box instead. That will calculate leave accruals based on service as of Jan. 1 of the current year. For the accrual setup, try the following:
Service Units = Months
Accrual Rate Units = Hours per Year (Award Frequency = First Run of Year)
First Year Award Values ==> NOT USED
Accrual Rate Values (You did not indicate subsequent years, so you may need more intervals.)
After Service Interval Accrue Hours At
13 Service Months 10 Hours per Year
Service Bonus Values (Assuming no accrual if hired after October)
After Service Interval Award Bonus Hours
3 Service Months 1.000000
4 Service Months 1.000000
5 Service Months 1.000000
6 Service Months 1.000000
7 Service Months 1.000000
8 Service Months 1.000000
9 Service Months 1.000000
10 Service Months 1.000000
11 Service Months 1.000000
12 Service Months 1.000000
The SBV's + Svc Calc # Yr Begin should cover your first year requirement, but you may need to tweak the setup if I did not understand it correctly.

How to extract month and year from a variable with description in R?

I have a data set with different kinds of variables and 1 variable includes a description with year and month ,from that variable i want to extract month and year,but i am unable to fetch.
Sample_Data
var1 var2
203 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008
205 UPFRONT BROKERAGE FOR THE MONTH OF MAY 2010
206 2008 MARCH MONTH BROKERAGE
207 UPFRONT BROKERAGE FOR 2009 MONTH OF APRIL
204 BROKERAGE FOR THE MONTH OF MARCH 2008
Expected_output:
var1 var2 month year
203 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008
205 UPFRONT BROKERAGE FOR THE MONTH OF MAY 2010 MAY 2010
206 2008 MARCH MONTH BROKERAGE MARCH 2008
207 UPFRONT BROKERAGE FOR 2009 MONTH OF APRIL APRIL 2009
204 BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008
Tried:
library(lubridate)
Sample_Data$month = month(Sample_Data$var2)
Sample_Data$year = year(Sample_Data$var2)
I have tried in different ways like,used lubridate,posixlt but unable to find the solution. Please help me in this way.
We can use extract from tidyr by specifying the regex to match the characters as showed in the input dataset.
library(tidyr)
extract(df1, var2, into=c('month', 'year'), '.*\\s+([A-Z]+)\\s+(\\d+)$',
remove=FALSE, convert=TRUE)
# var1 var2 month year
#1 203 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008
#2 205 UPFRONT BROKERAGE FOR THE MONTH OF MAY 2010 MAY 2010
#3 206 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008
#4 207 UPFRONT BROKERAGE FOR THE MONTH OF APRIL 2009 APRIL 2009
#5 204 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008
Or using base R, we remove the substring from the beginning of the string in the 'var2', capturing the word (\\w+) followed by space (\\s+) followed by numbers (\\d+) till the end of the string, in the replacement, we specify the capture group (\\1). We read this using read.table to create the new columns in 'df1'.
df1[c('month', 'year')] <- read.table(text=sub('.*(\\b\\w+\\s+\\d+)$',
'\\1', df1$var2), stringsAsFactors=FALSE)
df1
# var1 var2 month year
#1 203 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008
#2 205 UPFRONT BROKERAGE FOR THE MONTH OF MAY 2010 MAY 2010
#3 206 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008
#4 207 UPFRONT BROKERAGE FOR THE MONTH OF APRIL 2009 APRIL 2009
#5 204 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008
NOTE: In both the methods, we are converting the new columns to their respective class.
data
df1 <- structure(list(var1 = c(203L, 205L, 206L, 207L, 204L),
var2 = c("UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008",
"UPFRONT BROKERAGE FOR THE MONTH OF MAY 2010",
"UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008",
"UPFRONT BROKERAGE FOR THE MONTH OF APRIL 2009",
"UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008"
)), .Names = c("var1", "var2"), class = "data.frame",
row.names = c(NA, -5L))
You can't quite treat it as a date yet, because you need to parse the string. Try t(sapply(strsplit(Sample_Data$var2," "),function(x) x[7:8])) to get the two columns that you want.
You don't need lubridate because you are not really working with the Date data type. Use strsplit in base to split your var2 into "words". It looks like month is always the next-to-last word, and year is the last word.
# reproducible example please!
d <- read.table(textConnection("
var1, var2
203, UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008
205, UPFRONT BROKERAGE FOR THE MONTH OF MAY 2010
206, UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008
207, UPFRONT BROKERAGE FOR THE MONTH OF APRIL 2009
204, UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008
"), header=TRUE, sep=",", stringsAsFactors=FALSE)
get_month <- function(s) {
words <- unlist(strsplit(s, " "))
words[length(words)-1]
}
get_year <- function(s) {
words <- unlist(strsplit(s, " "))
as.integer(words[length(words)])
}
d$month = sapply(d$var2, get_month)
d$year = lapply(d$var2, get_year)
d
produces the desired output
> d
var1 var2 month year
1 203 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008
2 205 UPFRONT BROKERAGE FOR THE MONTH OF MAY 2010 MAY 2010
3 206 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008
4 207 UPFRONT BROKERAGE FOR THE MONTH OF APRIL 2009 APRIL 2009
5 204 UPFRONT BROKERAGE FOR THE MONTH OF MARCH 2008 MARCH 2008

Resources