Month to date aggreate KPI query postgresql - 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;

Related

Inventory transaction sum for each month

i have a table with detail transaction for lot. Lot are harvest and store before being ship.
Date lot transaction qty
5 sept 3 store 300
8 sept 3 ship -50
10 oct 3 ship -20
15 nov 3 ship -20
...
If i want the inventory for a specific moment, i simply sum between to date
I would like a query that can give me a sum from store to a specific month throught the entirr year like:
Lot sept to oct to nov ...
3 250 230 210 ...
Select lot, sum(qty) from ... where (date > 1 sept and date < 1 oct) as sept, (date > 1 sept and date < 1 nov)... group by lot
I did'nt find anything or figure out how to do it i a simple query.
Regards
Obtain one table with a query where is see the evolution of quantity over the year.

calculate monthly average for each column based on the last recorded timestamp for each day Sql server

select [SName],MAX([DateTime]) as Dates
from table
where [DateTime] between #MNT1 and #MXT1 and [DateTime] <=#MXT1
group by[SName] ,CONVERT(Date,[DateTime])
order by [SName],CONVERT(Date,[DateTime])
I'd like to somehow calculate average only for the max timestamp per day for each column.
#mnt1 and 2 are set as start date and end date for report
ID SERVERNAME COL2 COL3 COLN DATETIME
1 A 1.1 1.5 2022-08-01 23:50:13.0000000
2 A 1.1 1.6 2022-08-01 23:55:13.55530000
3 B 1.7 2 2022-08-01 23:50:13.7530000
4 C 3.4 2 2022-08-02 23:50:13.7530000
5 C 1.4 5 2022-08-08 23:50:13.7530000
enter image description here
I'm trying to calculate average based on the last value logged per day for each server eg. server A for month of august will have timestamps throughout the day till server is shut down at midnight ,for A il have last recorded date timestamp for the 1st of august through to end of august now I need to capture those and calculate monthly average soley based on those timestamps and so on foe each server

How to group data weekly for MTD and YTD values

I'm trying to get Weekly MTD and YTD values based of hourly data, but I'm having difficulties achieving this.
This is the data I'm working with:
max(Date) - Last day of the week
ISOWeek - Week in question
Value - The data I'm trying to sum
SELECT MAX(ISOWeek) AS [ISOWeek]
,MAX(Date) AS [Date]
,SUM(Value1) AS [MTD]
FROM Table1
GROUP BY ISOWeek, FORMAT(Date,'yyMM')
ORDER BY ISOWeek DESC
This is what that query returns:
ISOWeek Date MTD
29 2020-07-19 367529
28 2020-07-12 367138
27 2020-06-30 103290
27 2020-07-05 266755
26 2020-06-28 346588
25 2020-06-21 337168
This is what I would like to get:
ISOWeek Date MTD
29 2020-07-19 261515
28 2020-07-12 184104
27 2020-07-05 103414
26 2020-06-28 432114
25 2020-06-21 346588
The data has to be grouped by ISOWeek, if it's a week that dips into two months, I'm only interested in the MTD of the month in which the week ends. We have hundreds of values, so the plan is to create a MTD view and a YTD view. If I can get some help with the MTD one, I can get the other one done.
I'm nearly sure that what I'm after has to do with a WHERE clause and DATEADD but I'm not too sure what it should say.
Thank you for taking the time.
I don't really follow the rules you would like to apply, but per dates apply the formula to get weekstart/monthend or what you need. Place the date instead of the current date in the example.
Then group by the modified date.
You could build a date dimension where you have the required dates in some columns (first day of month, first day of week,etc.). This way you get a table with all the dates and the matching result for each.
It might be easier/faster to join it on the requried column.
declare #monthstart date,
#monthend date,
#weekstart date
;
select #monthstart=datefromparts(year(current_timestamp),month(current_timestamp),1);
select #monthend=EOMONTH(getdate(),0);
select #monthstart,#monthend,EOMONTH(getdate(),1) as next_month, EOMONTH(getdate(),-1) as previous_month;
select cast(DATEADD(d,1-DATEPART(WEEKDAY,current_timestamp),CURRENT_TIMESTAMP) as date) as Sunday,
cast(DATEADD(d,2-case when DATEPART(WEEKDAY,current_timestamp)=1 then 8 else DATEPART(WEEKDAY,current_timestamp) end,CURRENT_TIMESTAMP) as date) as Monday
;

LOD Workaround with Tableau 8.3

I'm new to Tableau. I have a customer-event table to show which customers attended which events (like webinars, etc). One of fields is sales - which is the sales for that customer 30 days from the date of the event.
custid eventid eventdt 30daysales
1 aa jan 1 $100
1 ab jan 1 $100
2 aa jan 2 $150
Note that customer 1 attended 2 events on the same day. So the sales number is duplicated. If I were building a report for a single event, it's no problem. But when I build a monthly report, I want sum(Sales) = $250 and not $350.
My report sample:
Month eventcount customercount 30daysales
Jan 2 2 $250
With tableau 9, I read that using an LOD formula would allow me to sum sales on a per customer basis. But I'm on Tableau 8.3 and I'm wondering what the manual workaround is.
How do I write the calculated field to compute the 30daysales without duplicating?

How to calculate average weekly hours between 2 dates covering multiple weeks?

Postgresql 8.4.
I'm new to this concept so if people could teach me I'd appreciate it.
For Obamacare, anyone that works 30 hours per week or more must be offered the same healthcare as is offered to any other worker. We can't afford that so we have to limit work hours for temp and part-timers. This is affecting the whole country.
I need to calculate the hours worked (doesn't matter if overtime,
regular time, double time, etc) between two dates, say Jan 1, 2014,
and Nov 1, 2014 (Saturday) for each custom week (which beings on Sunday), not the week as defined by Postgresql (which begins on Monday).
Each of my custom work weeks begins on Sunday and ends on Saturday.
I don't know if I have to include weeks where
they did not work at all in the average, but let's assume I do. Zero hours that week would draw down the average.
Table name is 'employeetime', date field is 'employeetime.stopdate', hours worked per day is in the field 'employeetime.hours', employeeid field is 'employeetime.empid'.
I'd prefer to do this in one query per employee and I will execute the query once per employee as I loop through employees. If not I'm open to suggestions. But I'd like to understand the SQL presented in the answer.
Currently EXTRACT(week from '2014-01-01') calculates the start of the week as a Monday, so that doesn't work for me. Link here.
How would I do that without doing, say a separate query for each week, per person? We have 200 people to process.
Thank you.
I have set up a table to match your format:
select * from employeetime order by date;
id date hours
1 2014-11-06 10
1 2014-11-07 3
1 2014-11-08 5
1 2014-11-09 3
1 2014-11-10 5
You can get the week starting on Sunday by shifting. Note, here the 9th is a Sunday, so that is where we want the boundary.
select *, extract(week from date + '1 day'::interval) as week
from employeetime
order by week;
id date hours week
1 2014-11-07 3 45
1 2014-11-06 10 45
1 2014-11-08 5 45
1 2014-11-09 3 46
1 2014-11-10 5 46
And now the week shifts on Sunday rather than Monday. From here, the query to get hours by week/employee would be simple:
select id, sum(hours) as hours, extract(week from date + '1 day'::interval) as week
from employeetime
group by id, week
order by id, week;
id hours week
1 18 45
1 8 46