MAX Date series before another date grouped - tsql

I have two tables in a stage-gate project management system, one (simplified) table containing a project ID and actual gate dates for each gate, 1-5. In the other I have a historic record of all forecast data; Projected Revenue, Projected Margins, Forecast Year, etc. Each time a forecast is updated, it records the new forecast values, the time stamp of the change, and the project ID. The requirement is to retrieve all metric values for the latest update prior to the actual gate date recorded in the first table. An example, Project 100 has a Gate 2 date of 2014-12-18. I need to retrieve the most recent values prior to that date.
Gate Date Table:
ProjectID InternalGate2
--------- -------------
100 2014-12-18
2000 2013-01-15
Historic Metric Table:
ProjectID Metric MetricYear LastUpdated MetricValue
--------- ------ ---------- ----------- -----------
100 Sales 2015 2013-09-05 125000
100 Sales 2016 2013-09-05 230000
100 GM 2015 2013-09-05 .48
100 GM 2016 2013-09-05 .49
100 Sales 2015 2014-05-26 200000
100 Sales 2016 2014-05-26 300000
100 GM 2015 2014-05-26 .50
100 GM 2016 2014-05-26 .51
100 Sales 2015 2015-01-28 300000
100 Sales 2016 2015-01-28 400000
100 GM 2015 2015-01-28 .55
100 GM 2016 2015-01-28 .56
2000 Sales 2014 2012-11-23 200000
2000 Sales 2015 2012-11-23 300000
2000 Sales 2016 2012-11-23 310000
2000 GM 2014 2012-11-23 .75
2000 GM 2015 2012-11-23 .77
2000 GM 2016 2012-11-23 .77
2000 Sales 2015 2013-02-11 450000
2000 Sales 2016 2013-02-11 450000
2000 Sales 2017 2013-02-11 500000
2000 GM 2015 2013-02-11 .68
2000 GM 2016 2013-02-11 .69
2000 GM 2017 2013-02-11 .70
For this example the results set would be the four rows for Project 100 with the LastUpdated Date of 2014-05-26 as this was the last update prior to 2014-12-18 and the first six rows of data for Project 2000 updated 2012-11-23.
Any guidance would be greatly appreciated.

The CTE could be a subquery if you prefer, but this works, basically just using two joins.
;WITH CTE as
(select h.ProjectID,MAX(LastUpdated) as LatestUpdate
from Historic h
inner join Gate g
on h.ProjectID = g.ProjectID
and h.LastUpdated <= g.InternalGate2
group by h.ProjectID)
select ProjectID,LastUpdated
from Historic h
inner join CTE c
on h.ProjectID = c.ProjectID
and h.LastUpdated = c.LatestUpdate

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.

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)

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