ms query man min date month query - ms-access

I am trying to develop an access based database for a small company. I have made few tables, some of them are the "2011/2012 Total Production in $" and ""2011/2012 Total Production in CY."
All four tables have a column "Actual Production"
Now, using query tool, i have to achieve two objectives.
get the maximum and minimum production of each month in a year
display the date of when the Actual Production was maximum/minimum in the month
I have accomplished first task which was simple; Make a query, get relevant fields, Summary Max & Min and finally distrubute in month wise. I.e Jan Max 5000, Min 2000...Feb Max 6000 Min 1000
Now what i desire is to display the date of max or min production for the month. So if April had MAX 181,218.00 in its month, I want it to display the date when it occurred (i,e April 10th 2012)
I am a beginner with Access, so please be as simple as possible.
Sample data gleaned from comment:
Month Sum of Prod Min Prod in $ Max Prod in $
------------- ------------- ------------- -------------
January 2011 $1,184,096.98 $20,486.40 $171,470.40
February 2011 $1,558,072.20 $44,962.20 $116,359.20
March 2011 $1,744,442.19 $19,200.00 $141,065.10
April 2011 $1,698,608.63 $27,500.70 $181,218.00
May 2011 $1,826,915.38 $37,996.00 $130,066.00
June 2011 $2,317,890.71 $42,645.00 $144,323.30
The above data were few of the fields gnerated by Query.
What I am looking for is
Month Date of Min Prod Min Prod in $ Date of Max Prod Max Prod in $
------------- ------------- ------------- ------------- -------------
January 2011 Jan 15 $20,486.40 Jan 10 $171,470.40
February 2011 Feb 20 $44,962.20 Feb 27 $116,359.20
March 2011 March 10 $19,200.00 March 1 $141,065.10
and so forth.
Is it possible to use a query to generate this result?
thanks!
Relation to the comment.
Relevant fields in one of the tables are.
Date of Activity Actual Production
------------- ------------- -------
1/3/2012 $20,486.40
1/4/2012 $44,962.20
1/5/2012 $19,200.00

I got lost in the details of your question. So I'll show you sample data and queries to get something like what I hope you want from that data.
Here is the contents of tblAbbas.
activity_date actual_production
1/3/2012 $20,486.40
1/4/2012 $44,962.20
1/5/2012 $19,200.00
2/1/2012 $3.00
2/2/2012 $2.00
2/3/2012 $1.00
Here is the SQL for a query named qryMonthStart. The purpose of this query is to determine the first day of the month which includes the activity_date.
SELECT
DateSerial(Year(activity_date),Month(activity_date),1)
AS month_start,
activity_date,
actual_production
FROM tblAbbas;
The query below uses qryMonthStart as its data source and gives me this result set.
month_year SumOf_production min_prod_date MinOf_production max_prod_date MaxOf_production
January 2012 $84,648.60 1/5/2012 $19,200.00 1/4/2012 $44,962.20
February 2012 $6.00 2/3/2012 $1.00 2/1/2012 $3.00
And the query SQL ...
SELECT
Format(grpby.month_start,"mmmm yyyy") AS month_year,
grpby.SumOf_production,
qmin.activity_date AS min_prod_date,
grpby.MinOf_production,
qmax.activity_date AS max_prod_date,
grpby.MaxOf_production
FROM
(
(
SELECT
month_start,
Sum(actual_production) AS SumOf_production,
Min(actual_production) AS MinOf_production,
Max(actual_production) AS MaxOf_production
FROM qryMonthStart
GROUP BY month_start
) AS grpby
INNER JOIN qryMonthStart AS qmin
ON
(grpby.MinOf_production = qmin.actual_production)
AND (grpby.month_start = qmin.month_start)
)
INNER JOIN qryMonthStart AS qmax
ON
(grpby.MaxOf_production = qmax.actual_production)
AND (grpby.month_start = qmax.month_start)
ORDER BY grpby.month_start;
Beware that query will fail ("data type mismatch in criteria expression") if you have Null for activity_date. The simplest way to prevent that is to clean out the Nulls then prohibit them in the activity_date column (set the Required property to Yes in table design view). If you decide you must allow Nulls in activity_date, you've got more work ahead.
Also note that query will give you multiple rows for the same month_year if the actual_production values in more than one of the daily records for that month matches the monthly minimum (MinOf_production). And the same situation will apply for the monthly maximum (MaxOf_production).

Related

SSRS Grouped total sum issue

I'm creating a SSRS report that shows projected sales for the next 12 months, grouped by ProductID. While the detail cells are showing correctly, the group sums for each month are displaying all sales for the 12 months rather than just the related month.
For example, here are all the table values for a single Product:
ProductID EstimatedDate ProjSales
123A Oct 10/2017 100
123A Nov 15/2017 100
123A Dec 01/2017 100
123A Dec 31/2017 100
However, this is what the report is currently showing for this Product:
Product EstimatedDate Oct 2017 Nov 2017 Dec 2017
123A Oct 10/2017 100 0 0
Nov 15/2017 0 100 0
Dec 01/2017 0 0 100
Dec 31/2017 0 0 100
Total 400 400 400
As seen above, the detailed cells calculate perfectly as each record in the detail section displays a Projected sales value if the Year/Month matches, otherwise it displays 0. Unfortunately, the final row with the "Total" amounts is incorrect as the monthly cells are showing the total of projected sales for all months rather than just the month in question.
Here are my report expressions for December 2017:
Detail Cell:
=IIF(Year(Fields!EstimatedDate.Value) = 2017 AND Month(Fields!EstimatedDate.Value) = 12, Fields!ProjSales.Value, 0)
Grouped Cell
=IIF(Year(Fields!EstimatedDate.Value) = 2017 AND Month(Fields!EstimatedDate.Value) = 12, SUM(Fields!ProjSales.Value), 0)
Any idea how I can change the grouped expression to retrieve the projected sales for each month?
Edit : format code
Use a Matrix.
Add a column group and group by year then month.
Then simply have ProjSales in the detail row group and SUM(ProjSales) in the Group total. There is no need to use expressions to calculate the values.
The column group captions will have to be expressions in order to pull out the month and year from the EstimateDate column. If you have the stored dates in a more conventional format YYYYMMDD etc then it would be easier as I'm not sure any DATE functions will recognise the format you show above.
If you need a more detailed answer let me know. I'm not able to do anything more at the moment.

To to get the sum of conditional aggregates in an Access form

I have a table tblBudget, which contains
ID ExpenseType Year Month Amount
11 Hardware 2017 Sep 5000
11 Software 2017 Oct 2000
11 Hardware 2016 Jan 1000
12 Software 2017 Feb 1500
I need to display the expenses by ID, ExpenseType, Year, Month. Hence I created a Group by query
Select id,ExpenseType,year,Month,sum (Amount) as sumOfAmount
from tblBudget
Group by ID,ExpenseType,Year,Month
This query works fine, however I need to sum up the total by Total Amount at the bottom of the screen.
On the access form I tried setting the control source as "=sum([Amount])" however it gives me an #error. I guess this is due to the fact that I already have sums of Amount in my datasheet. Is there any way to get the grand total in the footer of the form

Counting unique records by month

I'm a newbie to Access 2010. I have a table:
ID Mth OrderID Net Sales
1 1 3 36
2 1 2 12
3 1 2 20
4 2 1 10
I'd like to get a summary by Mth of the OrderID count, Quantity of those orders, and Net Sales of the those orders:
Mth Ordercount Quantity Net Sales
1 2 7 68
2 1 1 10
Is there a way to do this?
I'd also like to convert Mth = 1 into Month = Jan 2013 but have it list in date order, rather than alphabetically.
Mth
Jan 2013
Feb 2013
How do I do that?
So far, I've only been working with the design view and have not using an SQL code.
This can be done mostly in the design viewer of access although it would require creating more than one query and using those as a source instead of a table or you could write a sub select in sql code.
For your first question you will need to perform a distinct count on order id's based on month. This question answers the same problem and will provide the output you need.
Once you have a query that provides the number of orders per month you can create a new query that joins the table and your query on month with Net Sales as a total field. Where is quantity coming from in your source data?
To display the month number as month access has a MonthName function you can use. You can add 2013 to this by adding & " 2013" to the end of the expression.
You can sort on month by adding your month field a second time for the sorting but uncheck show box.

Grouping months by quarter over multiple years depending on a dynamic start month

Using MySQL and PHP I am building a JSON array to populate a data table.
For the purposed of my question suppose the data table has the following columns:
Year: 2010,2011,2012,2013...<br/>
Month: 1,2,3,4,5...<br/>
Value: 100, 150, 200 etc...<br/>
The table structure cannot be altered and my solution needs come into the MySQL query
The data can be viewed either monthly, quarterly or yearly. Monthly and yearly is achieved easily through grouping by year and month.
Quarterly data can be grouped by calendar quarter (Jan-Mar, Apr-Jun, Jul-Sep, Oct-Dec) by this group statement:
GROUP BY year, round((month/3)+0.3,0)
So where Jan, Feb and March might all have 100 for their value the summed result is 300, same for other months.
Now my problem comes when I want to group values by a financial quarter, for example a quarter that starts in Feb, or any other quarters.
I have a statement that works for the quarter grouping using two variables that can be accessed via the SQL query, start_year (i.e. 2014) and start_month (i.e. 2)
GROUP BY year, (((round(((((month-(start_month-1))+((year-start_year)*12))-((year-start_year)*12))/3)+0.33,0)/4)+4)-floor(((round(((((month-(start_month, '%m')-1))+((year-start_year)*12))-((year-start_year*12))/3)+0.33,0)/4)+4)))*12
which basically will assign a 0,3,6,9 value to each calendar month for the purposes of grouping.
In the financial year starting February this works fine for quarters 1-3, however breaks for the final quarter as it includes Nov and Dec 2014 data and Jan from 2015.
As a result I get 5 rows of data instead of 4.
This is because of the preceding GROUP by year clause, an important addition as we might want to generate a table that views sequential quarters for multiple years.
So what I am looking for is a way of grouping the years together by offsetting the start month.
So when the year starts in Jan it will group Jan-Dec but if we change that to starting Feb it will group Feb-Jan.
Any ideas, suggestions most welcome!
Regards,
Carl
I solved a similar problem just now (a Moodle report aggregating assignment scores by year and quarter) with something like this:
select year(from_unixtime(s.timemarked)) as year, quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter, count(distinct data1) as "tickets graded" from mdlassignment_submissions s where grade >= 0 group by year, quarter order by year, quarter;
The relevant part for what you're doing is quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter
As another commenter pointed out, MySQL has a quarter() function, but it doesn't do financial quarters. However, since (as I understand it, at least, based on consulting the relevant wikipedia page) financial quarters are just offset by 1, the % 4 + 1 at the end should convert it.

Transformation of records 1 column 3 row -> 1 row 3 column

First look at below query
SELECT COUNT(id) AS total_record, id, modeller,
MONTHNAME(completed_date) AS current_month,
QUARTER(completed_date) AS current_quarter,
Difficulty,
YEAR(completed_date) AS current_year
FROM model
WHERE modeller != ''
AND completed_date BETWEEN '2010-04-01'
AND '2010-05-31' AND Difficulty != ''
GROUP BY Difficulty,
MONTH(completed_date) ORDER BY
MONTH(completed_date) ASC
Results I am getting is
Modeller Month Year Difficulty
XYZ Jan 2010 23
XYZ Jan 2010 14
XYZ Jan 2010 15
ABC Feb 2010 5
ABC Feb 2010 14
ABC Feb 2010 6
I want result like
Modeller Month Year Difficulty
XYZ Jan 2010 23, 14, 15
ABC Feb 2010 5, 14, 6
My database is MySQL for application I am developing so any help would be greatly appericated.
Take a look on GROUP_CONCAT
I think you want GROUP_CONCAT(). I've simplified your fields so you'll need to add the calculations back in, but something like this should do:
SELECT modeller, month, year, GROUP_CONCAT(DISTINCT Difficulty)
FROM Model
WHERE $conditions
GROUP BY modeller, month, year
Nice, the GROUP_CONCAT function is powerfull stuff, and probably is a perfect solution in this case.
But watch out, in big systems this kind of manipulation can belong in the application layer and not in the database queries, maximizing easy of maintenance, re-utilization and reducing dependency in the specific RDBMS.
An alternative (more engineered) wait it to use the DAO pattern. Like documented in http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html