Which sales_rep has Which account for Which months - postgresql-10

This is a sample of my history table, named 'sales_rep_account_mgmt_hist':
account_id sales_rep date_modified
---------- --------- -------------------
1205 chloe 2019-01-15 09:17:32
1205 elisabeth 2019-06-15 09:17:32
1205 chloe 2019-08-01 07:55:03
1205 elisabeth 2020-01-02 08:04:52
1205 rachael 2020-01-04 16:22:32
1206 elisabeth 2019-08-28 13:14:12
1206 chloe 2019-10-26 07:37:22
1206 rachael 2019-10-29 09:22:56
I would like a query on this table to return year and month each sales rep managed accounts:
account_id sales_rep Yr Mo
---------- --------- ---- --
1205 chloe 2019 01
1205 chloe 2019 02
1205 chloe 2019 03
1205 chloe 2019 04
1205 chloe 2019 05
1205 chloe 2019 08
1205 chloe 2019 09
1205 chloe 2019 10
1205 chloe 2019 11
1205 chloe 2019 12
1205 elisabeth 2019 06
1205 elisabeth 2019 07
1205 rachael 2020 01
1206 elisabeth 2019 08
1206 elisabeth 2019 09
1206 rachael 2019 10
1206 rachael 2019 11
1206 rachael 2019 12
1206 rachael 2020 01
I've tried this: (dimdate is a date table)
with cte as (
select b.account_id, b.sales_rep, b.dt_mod, b.year_actual, b.month_actual, (b.year_actual::text || lpad(b.month_actual::text,2,'0')) as yr_mo
from(
select a.account_id, ram.sales_rep, a.dt_mod, a.year_actual, a.month_actual
from(
select account_id, max(date_modified)as dt_mod, d.year_actual, d.month_actual
from sales_rep_account_mgmt_hist m
join test_schema.dimdate d
on extract(year from m.date_modified) = d.year_actual and extract(month from m.date_modified) = d.month_actual
where m.date_modified between '2019-01-01' and '2020-02-01'
group by account_id, d.year_actual, d.month_actual
)a join sales_rep_account_mgmt_hist ram on a.dt_mod = ram.date_modified
)b
), cte2 as (
select year_actual as year_, month_actual as month_, (year_actual::text || lpad(month_actual::text,2,'0')) as yr_mo
from dimdate dd
where dd.date_actual between '2019-01-01' and '2020-02-01'
)
select distinct cte.account_id, cte.sales_rep, cte.dt_mod, cte.year_actual, cte.month_actual,cte2.year_, cte2.month_, cte2.yr_mo
from cte join cte2 on cte.yr_mo >= cte2.yr_mo
This seems like a step in the right direction but far from getting it done. I'm stumped :-\
A new row is created in the 'sales_rep_account_mgmt_hist' table above whenever a trigger fires on a certain production table. An inserted or updated row in the production table creates a copy of that row in the history table. The production table has account_id + sales_rep as its primary key. So the production table can't have more than one row for chloe with account 1205, for example, but the history table above can and so the reason for this table.
So for a supplied date range of 2019-01-01 until 2020-02-01, Chloe is managing 1205 from 1/2019 through 5/2019. Elisabeth takes over 6/2019 until Chloe picks it up again 8/2019 and continues until Rachael takes over 1/2020. Elisabeth managed it briefly in 1/2020 but only the sales rep who managed it last in the month gets counted. Date_modified indicates when a sales rep begins managing an account.

Related

SUMPRODUCT with AND criteria on Google Sheet

I'm trying to count all apples, oranges and mango of a specific month and year, but the formula doesn't work right now. Is there anything obvious that I'm doing wrong here?
=SUMPRODUCT(--(TEXT(A:A,"MMYYYY")="052020"), --(B:B={"apple","orange","mango"}))
Link to the sample sheet
Colum A Column B
May 2020 apple
May 2020 apple
May 2020 banana
May 2020 orange
May 2020 mango
Jun 2020 papaya
Jun 2020 mango
Aug 2020 apple
Oct 2020 apple
Oct 2020 orange
Oct 2020 banana
try:
=COUNTA(IFNA(QUERY(B7:C,
"select B
where month(B)+1 = 5
and year(B) = 2020
and C matches 'banana|mango|apple'"))
or:
=COUNTA(IFNA(FILTER(B7:B,
TEXT(B7:B, "mmyyyy")="052020",
REGEXMATCH(C7:C, "banana|mango|apple"))
I found it :)
=SUMPRODUCT( (TEXT(B9:B19,"MMYYYY")="052020")* ISNUMBER(MATCH(C9:C19,
{"apple","orange","mango"},0)))

how to get weak days of a particular week for a selected month and year in rails?

I want to get all the weak days of a particular week, for the selected year and month. I am getting data like year= 2015, month= 6 ,and weeknumber = 2 in controller. how to get week days for this particular data in rails?
i want a method like this which outputs the weekdays by taking week number. but the week number should be for that month like it should be less than or equal to five. below code outputs the week number for the whole year.
require 'date'
def week_dates( week_num )
year = Time.now.year
week_start = Date.commercial( year, week_num, 1 )
week_end = Date.commercial( year, week_num, 7 )
week_start.strftime( "%m/%d/%y" ) + ' - ' + week_end.strftime("%m/%d/%y" )
end
puts week_dates(22)
Because you're using rails you have the active support methods for beginning_of_week and end_of_week documented here you can do this
# first week in June
d = Date.new(2015,6, 1)
# => Mon, 01 Jun 2015
# add a week to get the second week
d += 1.week
# => Mon, 08 Jun 2015
(d.beginning_of_week..d.end_of_week).to_a
#=> [Mon, 08 Jun 2015, Tue, 09 Jun 2015, Wed, 10 Jun 2015, Thu, 11 Jun 2015, Fri, 12 Jun 2015, Sat, 13 Jun 2015, Sun, 14 Jun 2015]
If you want your week to start on sunday you can do this, passing the beginning day of the week:
(d.beginning_of_week(:sunday)..d.end_of_week(:sunday)).to_a
#=> [Sun, 31 May 2015, Mon, 01 Jun 2015, Tue, 02 Jun 2015, Wed, 03 Jun 2015, Thu, 04 Jun 2015, Fri, 05 Jun 2015, Sat, 06 Jun 2015]

In the context of rails, how can I order columns by the second?

I am working with rails and postgresql. I have a created_at timestamp on a model. I am in a situation where I need to order the results by second. I can't seem to get rails todo this.
e.g. If I insert 5 records in 1 minute, and then want to sort by created_at time, since they all have the same minute, it starts to order them by alpha because they all have the same insert minute.
How can I make it so that it orders by the insert in seconds. Postgres is storing it. But DateTime doesn't seem to take seconds into account in this way...
I hope this makes sense
Kirk
ActiveRecord order method should sort including the second
Eg:
Note the last two datetimes are in the same minute
User.pluck(:created_at)
=> [Fri, 14 Mar 2014 16:26:02 UTC +00:00, Fri, 14 Mar 2014 18:33:42 UTC +00:00, Fri, 14 Mar 2014 18:33:51 UTC +00:00]
User.order("created_at desc").collect(&:created_at)
=> [Fri, 14 Mar 2014 18:33:51 UTC +00:00, Fri, 14 Mar 2014 18:33:42 UTC +00:00, Fri, 14 Mar 2014 16:26:02 UTC +00:00]
Adding:
default_scope -> { order('created_at DESC') }
to your ActiveRecord should do it because created_at should look like this:"2014-03-12 15:29:26" which specifies the seconds.

c# lambda get the latest items on the generic list if the item is duplicate

I want to remove the old item from the list if there's a duplicate.
Here's my sample list:
1 Item1 Apr 27, 2013 $100
2 Item2 Apr 25, 2013 $200
3 Item3 Apr 24, 2013 $150
4 Item1 Apr 26, 2013 $100
5 Item3 Apr 25, 2013 $150
My Expected Result would be:
1 Item1 Apr 27, 2013 $100
2 Item2 Apr 25, 2013 $200
5 Item3 Apr 25, 2013 $150
You can use LINQ to get new list, with only proper elements:
var items = source.GroupBy(x => x.Item)
.Select(g => g.OrderByDescending(x => x.Date).First())
.ToList();

Iterating months using ruby

I have a very weird requirement where I need to deal with months. Here is what I am try to do actually. I have two object say
jan_start_date=Time.parse("2012-01-01 00:00:00")
jan_end_date=Time.parse("2012-01-31 23:59:59")
I take this two datetime objects and iterate over feb, mar, april and soon to get some data.
Then I will take feb start and end dates and iterate over march, april and soon.
Its like month on month data collection. Once I am done with say Jan data, I need to take Feb's data and so on and so forth. How do I achieve this. Since I need to iterate over months.
Kindly help me out
Thanks
ActiveSupport has beginning_of_month and end_of_month:
d = Date.today
#=> Fri, 13 Jul 2012
d.beginning_of_month
#=> Sun, 01 Jul 2012
d.end_of_month
#=> Tue, 31 Jul 2012
You can use Date#>> to shift dates forward monthwise:
(d>>2).end_of_month
#=> Sun, 30 Sep 2012
(d>>4).beginning_of_month
#=> Thu, 01 Nov 2012

Resources