Extracting Month, Year and Hours from a date range in ssis 2019 - date

I need to extract dates from a year (Jan-23 to 31dec 23) in month, day, and hours interval.
I am trying to use a script component for this task.
The expected output for the days is 365 rows in the destination :
2023-01-01 00:00:00.000
2023-01-02 00:00:00.000
2023-01-03 00:00:00.000
2023-01-04 00:00:00.000
2023-01-05 00:00:00.000
same for Month :
2023-01-01 00:00:00.000
2023-02-01 00:00:00.000
2023-03-01 00:00:00.000
2023-04-01 00:00:00.000
2023-05-01 00:00:00.000
2023-06-01 00:00:00.000
I tried to put a for loop and used some variables along with a script task.

Related

Formatting datetime in powershell script

I'm reading time into a variable from a CSV file but some of the time like 9 am is written as 9:00 instead of 09:00. How can I correct it into HH: mm format?
This should do the trick:
('9:00' -as [datetime]).ToString('HH:mm')
You can see it working on a loop from 0:00 to 12:00 with this:
PS /~> 0..12|%{("$_`:00" -as [datetime]).ToString('HH:mm')}
00:00
01:00
02:00
03:00
04:00
05:00
06:00
07:00
08:00
09:00
10:00
11:00
12:00

Check opening hours in different timezones

My Specs:
Postgres 9.6.6, latest Ubuntu LTS
Server Timezone is GMT
A table with two columns that shows store opening and closing times, with each timezone.
Here's the table:
ShopId OpenAt CloseAt
1 09:00:00 -08 17:00:00 -08
2 09:30:00 -05 17:30:00 -05
3 08:00:00 -11 15:00:00 -11
4 10:00:00 +07 15:30:00 +07
What I need to know is if at moment (at my current GMT time), the shop is open. Taking into consideration that Saturday and Sunday it's closed.
I'm digging around and I got something like:
SELECT ((OpenAt,CloseAt) OVERLAPS(NOW())) AND ISODOW < 6
with no luck...
Thanks
Perez
Try this :
SELECT ((date_trunc('day',nowAtShopLocation)+"OpenAt"::time, date_trunc('day',nowAtShopLocation)+"CloseAt"::time) OVERLAPS(nowAtShopLocation,nowAtShopLocation)) and EXTRACT (ISODOW FROM nowAtShopLocation) <6
from (
select *,now() AT TIME ZONE 'UTC'+(EXTRACT(TIMEZONE_HOUR FROM "OpenAt")||' hour')::interval nowAtShopLocation from your_table
) a

Count of sales partitioned by DOW (with date and time as input) - postgresql

Have scoured the internet for right response, but am not finding what I want.
I have an example dataset as follows:
Date --------------------------------- Number of Sales
Saturday 9th September 13:22:00 ------ 1
Sunday 10th September 16:44:02 ------ 3
Monday 11th September 12:00:00 ------ 2
Tuesday 12th September 13:04:34 ------ 4
Wednesday 13th September 15:84:04 ---- 3
Thursday 14th September 16:30:00 ----- 9
Friday 15th September 17:00:00 ------ 3
Saturday 16th September 18:00:03 ----- 5
Sunday 17th September 12:00:34 ------- 2
I want the output to be a table as follows:
Day of week -------------- Count
Mon ---------------------- 2
Tues --------------------- 4
Wed ---------------------- 3
Thurs -------------------- 9
Fri ---------------------- 3
Sat ---------------------- 6
Sun ---------------------- 5
This is a small sample, the dates go much further back, but this should give you an idea.
I am using postresql, version 9.5.8
I've tried different variations such as date_trunc, and partition (but perhaps am not using it properly). I keep getting the wrong output. Essentially, I want to be able to make a bar chart of day of week and count.
Ive tried this:
SELECT count(s.created_at), date_trunc('day', s.created_at)
FROM "sales" s
GROUP BY date_trunc('day', s.created_at)
This however gives me the count per unique day, rather than simply by dow, irrespective of date.
Thank you in advance.
https://www.postgresql.org/docs/current/static/functions-datetime.html
date_part or extract should do:
SELECT count(s.created_at), extract(dow from s.created_at)
FROM "sales" s
GROUP BY extract(dow from s.created_at)

Postgresql Week number same for Jan and Dec dates

In PostgreSQL, I am trying to convert timestamps into week numbers using the query below.
select user_id,
time_stamp,
date(time_stamp),
EXTRACT(YEAR FROM time_stamp) as Year,
EXTRACT(MONTH FROM time_stamp) as Month,
EXTRACT(WEEK FROM time_stamp) as Week
from user_engagement
user_id | time_stamp | date | year | month | week
---------+---------------------+------------+------+-------+------
6282 | 2013-01-01 14:29:35 | 2013-01-01 | 2013 | 1 | 1
6282 | 2013-01-02 14:29:35 | 2013-01-02 | 2013 | 1 | 1
6282 | 2013-12-30 14:29:35 | 2013-12-30 | 2013 | 12 | 1
6282 | 2013-12-31 14:29:35 | 2013-12-31 | 2013 | 12 | 1
I am surprised to see that the week numbers of the dates in Jan and Dec of the year 2013 are the same. Could someone explain the logic here?
I think the documentation explains this pretty well:
week
The number of the ISO 8601 week-numbering week of the year. By
definition, ISO weeks start on Mondays and the first week of a year
contains January 4 of that year. In other words, the first Thursday of
a year is in week 1 of that year.
In the ISO week-numbering system, it is possible for early-January
dates to be part of the 52nd or 53rd week of the previous year, and
for late-December dates to be part of the first week of the next year.
For example, 2005-01-01 is part of the 53rd week of year 2004, and
2006-01-01 is part of the 52nd week of year 2005, while 2012-12-31 is
part of the first week of 2013. It's recommended to use the isoyear
field together with week to get consistent results.

Month to date aggreate KPI query postgresql

I'm trying to find a way to obtain the amount of sales made from month to month but only comparing the aggregate total up to the current month's date.
In other words, I want to compare this month's sales up until now, against other months sales up until that month's same day.
Example using today's date as a reference (2016-06-18):
Total sales on January 2016 (*From 2016-01-01 to 2016-01-31*): 1000
Total sales on January 2016 (*From 2016-01-01 to 2016-01-17*): 650
Total sales on February 2016 (*From 2016-02-01 to 2016-01-17*): 670
Total sales on July 2016 (*From 2016-06-01 to current_date - 1*): 680
The structure of my data is as follows:
date sales
2016-01-01 5
2016-01-02 4
2016-01-03 5
2016-01-04 7
.
.
.
When I run the query I would like to have a monthly comparison of the totals mentioned above, so a result that looks like this:
month sales
2016-01 650
2016-02 670
.
.
2016-06 680
So comparing the month-to-date total of each month.
This is what I have tried so far and it seems to be fine, but somehow I always get a small difference whenever I make tests for limit days like the first of each month or the last.
select
order_month,
sum(sales) as sales
from table
where extract (day from order_date::date)
<=
extract (day from current_date::date)
- case when extract (day from current_date::date)=1 then 0 else 1 end
group by 1;