In MS Access 2010, assume a query contains the following results:
Date Activity Hours
1.9. Reading 1
1.9. Writing 2
2.9. Reading 1
3.9. Talking 1
4.9. Reading 3
1.10. Talking 2
1.10. Writing 1
2.10. Reading 2
3.10. Talking 2
4.10. Reading 1
the Report should show the sum of hours spent each month grouped by activity, something like
Month Activity Hours
September Reading 5
Writing 2
Talking 1
October Reading 3
Writing 1
Talking 4
using the wizard I mange to get a report which looks like
Month Activity Hours
September Reading 1
Reading 1
Reading 3
Writing 2
Talking 1
October Reading 2
Reading 1
Writing 1
Talking 2
Talking 2
which is nearly what I'd like to have but.. ?
I tried =sum(Hours), but then all Hours fields simply contain the total sum of all hours, and I still get several lines for the same activity.
Actually the report is more complicated. In a form prior to the report, the user can enter a date range (e.g. September 1 - October 30). The report should sum over the whole time range and not break up to months, that is:
Date range Activity Hours
9/1 - 10/30 Reading 8
Writing 3
Talking 5
Create a query with a SQL like this:
SELECT MonthName(Month([Date])), Activity, SUM(Hours)
FROM yourTable
WHERE [Date] >= xxx AND [Date] <= yyy
GROUP BY MonthName(Month([Date])), Activity
The key element is the GROUP BY clause, it sums the Hours per Month + Activity.
Then base your report on that query.
BTW, it is not a good idea to call a column "Date", it's a reserved word in Access.
Related
I'm trying to come up with a database structure to account for products that are available on certain days of week and on certain weeks ( odd / even )
An example:
- product1 is available for selling Mondays in odd weeks
- product2 is available everyday on even weeks
- product3 is available on weekends
I thought of isolating the availability in a second table with the product as fk and each condition on a separate row like so:
fk_prod1 weekday 1
fk_prod1 weekparity 1
fk_prod2 onweekday 1
fk_prod1 weekparity 0
In this form I don't know how I would get the products that are available for today, for the rest of the week and next week.
Any suggestions are welcome :)
Queries are also much appreciated!
This may help out :) Might needed adjusted depending on your circumstances with the program but heres an idea:
Table 1: Products
This could hold ProductID, ProductName
So a row could look like this: 1, product1
Table 2: ProductDayMatrix (ProductDayID,ProductDay)
1,Monday
2,Tuesday
3,Wednesday
4,Thursday
5,Friday
6,Saturday
7,Sunday
Table 3: ProductWeekMatrix (ProductWeekID, ProductWeek)
1 = odd weeks
2 = even weeks
or like
1, Week1
2, Week2
3, Week3
4, Week4
Finally then Table 4:
Product Matrix: (ProductID,ProductDayID,ProductWeekID)
This could hold your products and their corresponding data. So you would insert data into this table with your information and it would look like this.
(1,4,2) = Product 1, On a Thursday, on the even weeks.
Or if things need multiple conditions have 2 different tables that would hold the day information.
Like: ProductAvailablityMatrixDay & ProductAvailabilityMatrixWeek
Then those could hold mulitple values for each so if product 1 was on monday and thursday you could insert: (1,1) & (1,4). Then reading from that table would tell you that product 1 was on day 1 and 4. Same goes with the weeks.
Would it be worth considering an approach similar to the way cron jobs are structured?
Your day of the week will always be 1-7
Your week of the month will always be 1-6
So using LIKE would give 0 possible conflicts where 1 could be matched to 10 for example as you won't ever have 2 digits.
So if your product1 it could be:
day = 1
week = 1,3,5
For product2 it would be:
day = 1,2,3,4,5,6,7
week=2,4,6
For product3 it would be:
day=6,7
week=1,2,3,4,5,6 (September this year spans 6 different weeks)
The your queries would be something like:
Today:
"SELECT * FROM availability WHERE day LIKE '%".date("N")."%' AND week LIKE %".(date("W")-date("W",strtotime("first day of the month")))."%'";
// this is using some PHP to get the week of the days of the week and week of the month.
Tomorrow:
"SELECT * FROM availability WHERE day LIKE '%".date("N",strtotime("tomorrow")."%' AND week LIKE %".(date("W",strtotime("tomorrow"))-date("W",strtotime("first day of the month")))."%'";
// this again is using some PHP to get the week of the days of the week and week of the month - you can achieve the same with just MYSQL but I'm more familiar with PHP to give you an idea.
I'm not saying its perfect but unless your going to do more with these rows I don't think the use of foreign keys is going to help you? Hope that helps?
I have a table which captures when certain events (say alien attacks) happened. Each time the aliens attack a new record is created in this table (some days can have multiple attacks and some days none)..
attack_id attack_date
--------- ---------
1 03/12/2015
2 03/12/2015
3 04/01/2015
4 04/21/2015
5 06/14/2015
I want to show in a line graph how many attacks occured per week. So the x-axis would be weeks in the year and the y-axis would be the number attacks in that week.
Thus the result set to feed my graph might look like
Week Number of attacks
---- -----------------
Can someone suggest a mysql query?
two things you need: week() function to get the week from the date, and count() to get how many attacks happened:
SELECT WEEK(alien_date) as attack_week, COUNT(*) as num_of_attacks
FROM yourTable
GROUP BY WEEK(alien_date)
I have a table within MySQL which is being used to store user session information across a fleet of instances. The structure of the table is pretty simple, with 6 fields.
Item: 37824
Timestamp: 2014-10-30 23:01:41
Active: 14
Idle: 2
Total: 16
Server:
The metrics are collected every 5 minutes.
I have a query which pulls the SUM of one of the metrics for the past 5 minutes.
select SUM(metric_activeSessions) from metrics_tbl WHERE metric_timeStamp > NOW() - INTERVAL 5 MINUTE
I want to build a chart from these metrics, using one of the many great libraries available, but yet to be determined.
I would like the chart to show the SUM of a given metric every over a period of 60 minutes, at 5 minute intervals.
- 09:00 : 12
- 09:05 : 15
- 09:10 : 18
- 09:15 : 21
- 09:20 : 28
I have no idea how to write an SQL query to collect information in that way and was hoping someone out there may be able to help me out.
Thank you in advance for any ideas,
Cheers,
Mitch
Assuming metric_timeStamp is formatted as a mysql timestamp, try something like this..
SELECT SUM(metric_activeSessions), MAX(metric_timeStamp) as max_time, MIN(metric_timeStamp) as min_time,
FROM metrics_tbl
WHERE metric_timeStamp > NOW() - INTERVAL 60 MINUTE
GROUP BY unix_timestamp(metric_timeStamp) div 300
Use that as a starting point. I'm eating ice cream right now so I can't run this myself, but that's the general idea.
Edit: fixed syntax issue
Edit 2: I was grouping by 10 minutes. Changed it to 5 minutes
Edit 3: If your rows contain a running total, then change
SUM(metric_activeSessions)
to
MAX(metric_activeSessions)
in the query above.
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.
I have a MySQL table containing a column to store time and another to store a value associated with that time.
time | value
------------
1 | 0.5
3 | 1.0
4 | 1.5
.... | .....
The events are not periodic, i.e., the time values do not increment by fix interval.
As there are large number of rows (> 100000), for the purpose of showing the values in a graph I would like to be able to aggregate (mean) the values for an interval of fixed size over the entire length of time for which the data is available. So basically the output should consist of pairs of interval and mean values.
Currently, I am splitting the total time interval into fixed chunks of time, executing individual aggregate queries for that interval and collecting the results in application code (Java). Is there a way to do all of these steps in SQL. Also, I am currently using MySQL but am open to other databases that might support an efficient solution.
SELECT FLOOR(time / x) AS Inter, AVG(value) AS Mean
FROM `table`
GROUP BY Inter;
Where x is your interval of fixed size.
I've usually solved this through a "period" table, with all the valid times in it, and an association with the period on which I report.
For instance:
time day week month year
1 1 1 1 2001
2 1 1 1 2001
....
999 7 52 12 2010
You can then join your time to the "period" table time, and use AVG.