how can I calculate a year information by week - ms-access

I want the query calculate like if week 2 for "Shalma", then "total seconds" for week 2 only will Sum in query....I want this result in query not in report, is that possible?
exp the result I want:
PIC Total Seconds SortByWeek
Aida 600 2
Arifah 540000 2
Shalma 28000 1
Shalma 72036900 2
Zul 54000000 1
Zul 3000 2
Zul 100000 3
Zul 283500 4
it shows total by week for each name.

Don't include the full date in grouping.
SELECT PIC, Sum([Total seconds]) AS [SumOfTotalSeconds], SortByWeek, Year(date_worked_smt) AS Yr, days
FROM [union]
INNER JOIN [day] ON union.SortByWeek = day.ID
GROUP BY PIC, Year(date_worked_smt), SortByWeek, days
ORDER BY PIC, SortByWeek;

Related

Get the count of rows for each month in specific date range

I have list of records in table A and want to get the count of records for each month in range of days, starting from first of this month to the current day. So if we have 3 months and the current day is 19, So for the first month i need count of records between 1st to 19th of the month, and for the second month from 1st to 19th and so on.
id time
1 2005-07-05 14:10:29
2 2005-07-12 15:47:35
3 2005-08-02 16:38:53
4 2005-08-04 10:48:12
5 2006-08-22 17:34:28
6 2006-09-01 22:11:35
7 2006-09-09 15:10:19
8 2006-09-06 21:55:56
The desired Result:
time count
2005-07 2
2005-08 2
2006-09 3
we ignored the record id 5 because we just count the days between 1st and 19th for each month.
How can i do this?
You cam filter the rows with the function DAY() and aggregate:
SELECT DATE_FORMAT(time, '%Y-%m') yearmonth,
COUNT(*) counter
FROM tablename
WHERE DAY(time) <= DAY(CURRENT_DATE)
GROUP BY yearmonth
See the demo.
Results:
yearmonth
counter
2005-07
2
2005-08
2
2006-09
3

MySQL: Sum of first n and last n records of single column based on conditions

Is it possible to calculate sum of first 5 overs and last 5 overs from a single column? I am in a situation where I have to calculate it using query.
I have to calculate sum of first 5 overs and show it in different column, and similarly I have to calculate sum of last 5 overs and show it in another column. My table look like this:
Overs runs players
1 1 p1
1 1 p2
1 4 p2
1 1 p2
1 0 p1
1 0 p1
2 1 p2
2 2 p1
2 3 p1
2 1 p2
Now it should calculate first five overs sum and calculate last five overs sum for each player and tell me which players scored how much runs in first 5 overs and how much they scored in last 5 overs.
players first5overSum last5overSum
p1 40 0
p2 50 120
p3 10 60
MySQL query look like this:
SELECT player_key,
sum(runs) as first5overSum
FROM `batsman_scores`
WHERE over<=5
group by player_key
I am able to calculate records for first 5 overs, but I don't know how to show last 5 overs sum of each player together and show it.
Please give me suggestions or solutions. I will be very thankful!
I think you can use conditional aggregation. I'm not quite sure what "first five" and "last five" mean, but the query would look something like this:
SELECT bs.player_key,
SUM(CASE WHEN bs.over <= 5 THEN bs.runs ELSE 0 END) as first5overSum,
SUM(CASE WHEN bs.over > bs2.max_over - 5 THEN bs.runs ELSE 0 END) as last5overSum,
FROM batsman_scores bs CROSS JOIN
(SELECT MAX(bs2.over) as max_over
FROM batsman_scores bs2
) bs2
WHERE bs.over <= 5
GROUP BY bs.player_key

MySQL - How to sum up first occurrences from many different products

I have a big view called: how_many_per_month
name_of_product | how_many_bought | year | month
p1 20 2012 1
p2 7 2012 1
p1 10 2012 2
p2 5 2012 2
p1 3 2012 3
p2 20 2012 3
p3 66 2012 3
How to write MySQL query in order to get only first few occurences of product p1, p2, p3 at once?
To get it one by one for first 3 months I can write:
SELECT name_of_product , sum(how_many_bought) FROM
(SELECT name_of_product, how_many_bought FROM `how_many_per_month`
WHERE name_of_product= 'p1' LIMIT 3) t
How to do it to all possible products at once so my result for taking only first month is like:
p1 20
p2 7
p3 66
For two months:
p1 30
p2 12
p3 66
The problem is that some products are published in different months and I have to make statistic how many of total of them are sold in first month, first 3 months, 6 months, 1 year divided by total.
Example using union
select
name_of_product,
sum(how_many_bought) as bought,
"first month" as period
from how_many_per_month
where month = 1
group by name_of_product
union
select
name_of_product,
sum(how_many_bought) as bought,
"first 2 month" as period
from how_many_per_month
where month <= 2
group by name_of_product
union
select
name_of_product,
sum(how_many_bought) as bought,
"first 6 month" as period
from how_many_per_month
where month <= 6
group by name_of_product
union
select
name_of_product,
sum(how_many_bought) as bought,
"first 12 month" as period
from how_many_per_month
where month <= 12
group by name_of_product
Demo: http://www.sqlfiddle.com/#!2/788ea/11
Results are different a little bit from your expectation. Are you sure that you write them properly? If you need to gain more speed in query time you can use group by case as I've already said.
I'm not quite sure what you're trying to achieve as the description of your question is a bit unclear. From what I've read so far, I understand you want to show the total of how many ITEM_X, ITEM_Y, ITEM_Z were sold for the past 1,3,6 months.
Based on the data you've provided, I've created this sqlfiddle that sums all results and groups them by item. This is the query:
SELECT
name_of_product,
sum(how_many_bought) as how_many_bought
FROM how_many_per_month
WHERE year = 2012
AND month BETWEEN 1 AND 3
GROUP BY name_of_product
-- NOTE: Not specifying an year will result in including all "months"
which are between the values 1 and 3 for all years. Remove it
in case you need that effect.
In the example above the database will sum all sold items between months 1 and 3 (including) for 2012. When you execute this query in your application just change the range in the BETWEEN X AND X and you'll be good to go.
Additional tip:
Avoid using sub-queries or try using the as a last resort method (in case there's simply no other way to do it). They are significantly slower than normal and even join queries. Usually sub-queries can be transformed into a join query.
SELECT
hmpm.name_of_product , SUM(hmpm.how_many_bought)
FROM (
SELECT name_of_product
FROM how_many_per_month
/* WHERE ... */
/* ORDER BY ... */
) sub
INNER JOIN how_many_per_month hmpm
ON hmpm.name_of_product = sub.name_of_product
GROUP BY hmpm.name_of_product
/* LIMIT ... */
MySQL not support LIMIT in subquery, but you need ordering and condition. And why not have id_of_product field?

How to select database rows by days ,month,years

How to select database rows by days ,month,years
I have table like this
id count generatedAt
1 130 2013-01-13 02:21:02
2 120 2013-01-08 04:15:06
3 89 2013-01-08 01:42:57
4 24 2012-11-25 05:31:43
5 3 2012-02-31 09:25:24
I would like to select the rows by day or month or year.
For example by day.
2-3 is same day so I need only
1,2,4,5
for example by month,1,2,3 is same month so I need only
1,4,5
for year I need only 1,4
How can I make it?
I am using doctorine2
You can do something like this . .
you can choose a date , month or specific year to select rows.
select * from TabeName
//for days
where DAY(myDate) = 20
//for month
MONTH(myDate) = 12
// for year
YEAR(myDate) = 2008

How to calculate a moving 4 week average every week in MySQL

I have a table something like this.
count | date
------------------
1 2012-01-01
4 2012-01-01
5 2012-01-02
12 2012-01-03
7 2012-01-04
4 2012-01-05
19 2012-01-06
1 2012-01-07
etc...
I'm looking for a way to calculate the average count per week over the previous 4 week period for each week.
The results should be something like...
avg | yearweek
------------------
3 201201
5 201202
6 201203
1 201204
11 201205
3 201206
18 201207
12 201208
etc...
...where each yearweek is the weekly average over the past 4 yearweeks.
Getting the weekly averages is simple enough but how do I then get that over the past 4 yearweeks? And then how to do I do that as a rolling average? Am I better off just doing this in code?
While you could certainly do this in the code of your application, if you really need to do it in SQL, you could first create a table of results aggregated by week and then join it to itself to get the 4-week moving average.
In doing so, instead of storing the averages, I would store the sums and the number of days (1st or last week of year might not have 7 days - thinking of the edge cases). That way, you would avoid calculating unweighted averages when the denominators of averages are different.
So let's say you have a table "weekly_results", which has fields: yearweek, sumcount, numdays. You can now self-join to the last 4 weeks and get the sums and counts, and then calculate the averages from that:
SELECT yearweek, sum_cnt/sum_dys as avg_moving_4wk
FROM (
SELECT a.yearweek, sum(b.sumcount) as sum_cnt, sum(b.numdays) as sum_dys
FROM weekly_results a
join weekly_results b
on a.yearweek - b.yearweek <4 and a.yearweek - b.yearweek >=0
GROUP BY a.yearweek
) t1
GROUP BY yearweek