Inventory transaction sum for each month - date

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.

Related

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;

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?

Crystal Reports Conditional Grouping on Unrelated DB Field

I have some data in table SSTemp like this ("..." indicates data omitted for readability):
Month Year Number Gross Net
1 2013 1 1,000 500
2 2013 1 1,000 500
...
12 2013 1 1,000 500
1 2014 1 1,000 500
2 2014 1 1,000 500
...
12 2014 1 1,000 500
1 2015 1 1,000 500
...
12 2015 1 1,000 500
I am new to Crystal Reports and am using version 8 (no, we can't upgrade). I want to roll up the totals for all line items in years past and leave the data as-is for the current year in the same report. Database field {CONTROLFILE.CURRENTYEAR} contains the current bookkeeping year for our system which is vital to determine the rollup groups. The CONTROLFILE table contains general settings for the system and has no data in it useful for JOINing, however I need to consider CURRENTYEAR for the grouping. The MONTH column should be blank on the summary lines, and indicate month on the current year lines. The end result data should look like this:
Month Year Number Gross Net
2013 12 12,000 6,000
2014 12 12,000 6,000
1 2015 1 1,000 500
2 2015 1 1,000 500
...
12 2015 1 1,000 500
Any suggestions would be most appreciated!
Use sub report concept.
In main report calculate for previous years in sub report calculate for current year.
In main report group by year And suppress details show summary in group footer in another group footer section place sub report and just place data in detail part don't group

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

individual day calculations ,in MDX, with Start and End date in fact table

I have a ssas cube with the fact table containing:
FactID
Status
StartDate
EndDate
the dates are linked to a date dimension (status to the status dimension).
Im trying to get a report that shows the amount of facts at a status on each day over a two week period, eg:
01 May 2011, 02 May 2011, 03 May 2011 etc...
status1 300 310 320 ...
status2 250 240 265 ...
status3 125 546 123 ...
I can obtain the data for a single day using the following:
select
{
[TOTAL NUMBER FACT]
} on 0
,{
descendants([DIM STATUS].[STATUS DESCRIPTION])
} on 1
from [DW_CUBE]
WHERE
([DIM HISTORY START DATE].[YEAR MONTH DAY].FirstMember:[DIM HISTORY START DATE].[YEAR MONTH DAY].&[20110501],
[DIM HISTORY END DATE].[YEAR MONTH DAY].&[20110501]:[DIM HISTORY END DATE].[YEAR MONTH DAY].LastMember)
but do i get this working for more than a single day?
Many many thanks
Have a look at the following links:
http://www.bp-msbi.com/2010/10/avoiding-multiple-role-playing-date-dimensions/
http://cwebbbi.wordpress.com/2011/01/21/solving-the-events-in-progress-problem-in-mdx-part-1/
In brief, you can use MDX to do this with LinkMember, or if you are counting events in progress - with counting from begin of time till now and subtracting one event from another.
You can also solve the problem with modelling - in my post by pivoting and in Chris's follow-up with role-playing measure groups.