Group Dates by month - sql

I've been working on this query and I'm about 90% where I need to be however there is one piece of this that I'm unable to figure out. Basically I'm looking for the Sum of Net flows by month, starting 12/31/2014 through the current date. I'm able to extract data by day and the sum of net flows for that day, however now I really need to be able to group all the dates in to their respective months. Ex. If I have 01/01/2015, 01/02/2015 and 01/03/2015 I just want both of them to be grouped together and show up as 01/2015. Bellow is the query that I have written. Please help with this last step.
SELECT
DATE,
SUM(NET_FLOWS/1000000.00) AS YTD_NET_FLOWS
FROM
HISTORY_TBL
WHERE
DATE >= TO_DATE ('12312014','MMDDYYYY')
GROUP BY
DATE
ORDER BY
DATE

You can truncate a date to a given format (day, year, month, etc.), as shown here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084
SELECT TRUNC(DATE,'MM'), SUM(NET_FLOWS/1000000.00) AS YTD_NET_FLOWS
FROM HISTORY_TBL
WHERE DATE >= TO_DATE ('12312014','MMDDYYYY')
GROUP BY TRUNC(DATE,'MM')
ORDER BY DATE

Related

Need help aggregating custom months into a snapshot field

I am working with a dataset that has a continues date field and I want to aggregate it at a monthly level where the month ends on the 15th day of the month. So each snapshot date would go from the 15th of the month to the 14th of the following month.
Example: Snapshot Date = 7/15/2021 would correspond with the date range of 6/15/2021 through 7/14/2021.
Is there an easy way to do this for all months in the table using SQL Server.
Just subtract 14 days and convert to a year/month format. One trick is to move everything to the last day of the month:
select eomonth(dateadd(day, -14, datecol)), count(*)
from t
group by eomonth(dateadd(day, -14, datecol));

Why do i get a different results from my weekly code vs per week code?

Why am I getting different results when I compare weekly results into using a code individually per week. Does it have something to do with the timestamp?
This is the code for all the weeks:
select date_trunc('week',date_joined) as week, COUNT(*) as count from auth_user
where date_joined>='01-01-2019' and date_joined<='31-03-2019'
group by week
order by week
This is the resulting table:
first result
This is the code for getting an individual week:
select COUNT(*) from auth_user where date_joined>='31-12-2018' and date_joined<='06-01-2019'
This is the result for the first week: second result
I'd say that date_joined is a timestamp, and your second query misses the entries from January 6th.
Try with
AND date_joined < '2019-01-07'
Also, you should use ISO notation: YYYY-MM-DD

SQL - filter values based on month & date

I'm currently strugeling to get something done.
I needed only the rows where the date is between a certain date and month.
Example show only the rows where the date is between 01/05 (DD/MM) and 08/07 (DD/MM) the date can be found in the table tasks within the field information.
The year can't make any sense, but the results may only between those two dates in that year.
I've tried:
BETWEEN (TO_DATE ('01/05','DD/MM') AND (TO_DATE('08/07', 'DD/MM')
EXTRACT (DD/MM) from information.
none of those are working for me, I hope that someone of you can help me to figure this out!.
Thanks!
You seem to want to disregard the year. If so, then I would recommend:
TO_CHAR(datecol, 'MM/DD') BETWEEN '05/01' AND '07/08'
In order for BETWEEN to work in this case, you need the format in the order of MM-DD.
If you want this for a particular year, then use direct date comparisons:
datecol >= DATE '2018-05-01' AND
datecol < DATE '2018-07-09' -- note this is one day later
Oracle dates have a time component, so you need to be careful when making comparisons.

Sql select statement that will bring back my data in weeks, using derived tables with JasperReports

I'm currently running the following sql statement in JasperReports Server to bring back my data using derived tables.
Select count(createddate) as ModulesCreatedDuringPastWeek,
count(updateddate) as ModulesUpdatedDuringPastWeek,
createddate,
updateddate
from merchendisingmodule
group by merchendisingmodule.createddate, merchendisingmodule.updateddate
However when grouping my data, I am only able to do it in Year, quarter, month and day. However for my report I'm needing the data to be group weeks, and so I was wondering what I will need to add to my code to do this.
DATEADD(D,-DATEPART(weekday,createddate)+1,createddate)
I use this method to prevent issues around the year transitions (week 53 in first days of januari and also in the last days of december, will group days together that are 360 days apart).
I use the first day of the week, instead of week numbers. I can use these dates to group by.
Also this will ensure that every week is 7 days long, instead of the last week of the year being only 3 or 4 days long.
Btw, in this example the first day of the week is sunday.
If your dates include time, use:
CAST(FLOOR(CAST(createddate AS FLOAT)) AS DATETIME)
instead of createddate in the above SYNTAX

use of week of year & subsquend in bigquery

I need to show distinct users per week. I have a date-visit column, and a user id, it is a big table with 1 billion rows.
I can change the date column from the CSVs to year,month, day columns. but how do I deduce the week from that in the query.
I can calculate the week from the CSV, but this is a big process step.
I also need to show how many distinct users visit day after day, looking for workaround as there is no date type.
any ideas?
To get the week of year number:
SELECT STRFTIME_UTC_USEC(TIMESTAMP('2015-5-19'), '%W')
20
If you have your date as a timestamp (i.e microseconds since the epoch) you can use the UTC_USEC_TO_DAY/UTC_USEC_TO_WEEK functions. Alternately, if you have an iso-formatted date string (e.g. "2012/03/13 19:00:06 -0700") you can call PARSE_UTC_USEC to turn the string into a timestamp and then use that to get the week or day.
To see an example, try:
SELECT LEFT((format_utc_usec(day)),10) as day, cnt
FROM (
SELECT day, count(*) as cnt
FROM (
SELECT UTC_USEC_TO_DAY(PARSE_UTC_USEC(created_at)) as day
FROM [publicdata:samples.github_timeline])
GROUP BY day
ORDER BY cnt DESC)
To show week, just change UTC_USEC_TO_DAY(...) to UTC_USEC_TO_WEEK(..., 0) (the 0 at the end is to indicate the week starts on Sunday). See the documentation for the above functions at https://developers.google.com/bigquery/docs/query-reference for more information.