I can't seem to figure out how to aggregate this table. I need to figure out from this table what is the amount aggregated by a month. So I have some projects and a duration per project, average income per month (for each project). For example, I would like to see what is the total amount for all project May 2011. Here is the original table:
Year Month Amount Duration (month) average per month
2012 1 7 4 1.75
2012 2 6 5 1.2
2012 3 5 6 0.833333333
2012 4 4 6 0.666666667
2012 5 9 5 1.8
2012 6 10 4 2.5
2012 7 20 3 6.666666667
2011 4 13 2 6.5
2011 3 3 10 0.3
2011 12 4 11 0.363636364
2011 2 5 12 0.416666667
2011 3 7 3 2.333333333
2010 5 8 4 2
2010 7 3 6 0.5
2010 9 4 7 0.571428571
2010 11 5 8 0.625
2010 1 6 8 0.75
2010 2 7 8 0.875
2010 3 8 9 0.888888889
2010 4 9 1 9
I would appreciate any help. Thanks.
Assuming year is varchar and month/duration int (if not, you may need to convert them as applicable) you can do something like this:
select sum(amount) from yourtable
where YearMonth between
period_add(year&'01',month-1) and period_add(year&'01',month+duration-2)
being YearMonth a string with the year/month to be queried, in your example would be '201105'
for a number of year/months you can create a one-column table:
create table yearmonths(yearmonth varchar(6));
insert into yearmonths values
('201101'),('201102'),(201103),
('201104'),('201105'),(201106)
and join it to your table:
Select yearmonth,sum(amount)
from yearmonths y
left join yourtable t
on(y.yearmonth between period_add(year&'01',month-1)and period_add(year&'01',month+duration-2)
group by yearmonth
Related
Basically I am trying to calculate shots received in golf for various four balls, here is my data:-
DatePlayed PlayerID HCap Groups Hole01 Hole02 Hole03 Shots
----------------------------------------------------------------------
2018-11-10 001 15 2 7 3 6
2018-11-10 004 20 1 7 4 6
2018-11-10 025 20 2 7 4 5
2018-11-10 047 17 1 8 3 6
2018-11-10 048 20 2 8 4 6
2018-11-10 056 17 1 6 3 5
2018-11-10 087 18 1 7 3 5
I want to retrieve the above lines with an additional column which is to be calculated depending on the value in the group column, which is the players (Handicap - (the lowest handicap in the group)) x .75
I can achieve it in a group by but need to aggregate everything, is there a way I can return the value as above?, here is query that returns the value:
SELECT
PlayerID,
MIN(Handicap),
MIN(Hole01) AS Hole01,
MIN(Hole02) AS Hole02,
MIN(Hole03) AS Hole03,
MIN(CourseID) AS CourseID,
Groups,
ROUND(
MIN((Handicap -
(SELECT MIN(Handicap) FROM Results AS t
WHERE DatePlayed='2018-11-10 00:00:00' AND t.Groups=Results.Groups)) *.75))
AS Shots
FROM
Results
WHERE
Results.DatePlayed='2018=11=10 00:00:00'
GROUP BY
DatePlayed, Groups, PlayerID
.
PlayerID MIN(Handicap)Hole01 Hole02 Hole03 CourseID Groups Shots
-----------------------------------------------------------------
4 20 7 4 6 1 1 2
47 17 8 3 6 1 1 0
56 17 6 3 5 1 1 0
87 18 7 3 5 1 1 1
1 15 7 3 6 1 2 0
25 20 7 4 5 1 2 4
48 20 8 4 6 1 2 4
Sorry about any formatting really couldn't see how to get my table in here, any help will be much appreciated, I am using the latest mysql from ubuntu 18.04
Not an answer; too long for a comment...
First off, I happily know nothing about golf, so what follows might not be optimal, but it must, at least, be a step in the right direction...
A normalized schema might look something like this...
rounds
round_id DatePlayed PlayerID HCap Groups
1 2018-11-10 1 15 2
2 2018-11-10 4 20 1
round_detail
round_id hole shots
1 1 7
1 2 3
1 3 6
2 1 7
2 2 4
2 3 6
Hi Guys I have found the solution, basically I need to drop the MIN immediately after the ROUND of the equation and therefore it does not need a Group By.
SELECT
PlayerID,
Handicap,
Hole01,
Hole02,
Hole03,
CourseID,
Groups,
ROUND((Handicap -
(SELECT MIN(Handicap) FROM Results AS t
WHERE DatePlayed='2018-11-10 00:00:00'
AND t.Groups=Results.Groups))
*.75) AS Shots
FROM
Results
WHERE
Results.DatePlayed='2018=11=10 00:00:00'
i have a table
id year month
--------------
1 2015 4
2 2015 4
1 2015 5
1 2015 6
2 2015 6
3 2015 6
and so on
1 2016 3
2 2016 3
3 2016 3
4 2016 3
Now i want all distinct id's from year-month 2015-4 to 2016-3.
i tried between but didn't get desired result.
any help???
You can use this query:
SELECT id from temp where str_to_date(concat(year,'-',month,'-',1),'%Y-%m-
%d') between str_to_date('2015-4-1','%Y-%m-%d') and str_to_date('2016-3-31','%Y
-%m-%d');
wich converts your values to dates.
I always only need the most recent 10 years of data from the result set. The years will always have multiple records, so I can not pull only the first 10 records. How can I: SELECT Set 1, SELECT Set 2... through set 10 and only display those 10 years of data without pulling prior years?
Year Quarter Quarterly_yield
2012 1 8.41
2012 2 -0.71
2011 3 0.03
2011 4 3.43
2010 1 8.41
2010 2 -0.71
2009 3 0.03
2009 4 3.43
2008 1 8.41
2008 2 -0.71
2007 3 0.03
2007 4 3.43
2006 1 8.41
2006 2 -0.71
2005 3 0.03
2005 4 3.43
2004 1 8.41
2004 2 -0.71
2003 3 0.03
2003 4 3.43
2002 1 8.41
2002 2 -0.71
2001 3 0.03
2001 4 3.43
2000 1 8.41
2000 2 -0.71
You can do this this way:
SELECT * FROM table WHERE year IN
(SELECT year FROM table ORDER BY year DESC LIMIT 10);
This query can explained like this:
Inner query - select last 10 years from table
Outer query - select all columns from table where year in inner query result
How about
SELECT *
FROM table
WHERE year IN
(
SELECT year
FROM table
ORDER BY Year DESC
LIMIT 10
)
This selects the 10 most recent years
SELECT year
FROM table
ORDER BY Year DESC
LIMIT 10
Then you select every records with those years
select * from t
where
(year>(select max(year) from t)-10)
order by year desc, quarter desc
Hi I have problem for create SQL. I have table and the data like this :
id month year id_type qty
1 10 2012 1 5
2 10 2012 2 4
3 10 2012 3 3
4 10 2012 4 5
5 11 2012 1 1
6 11 2012 2 2
7 12 2012 1 3
8 12 2012 2 2
I want create SQL for generating the data like this
id_type month year qty month_b4 year_b4 qty_b4
1 10 2012 5 9 2012 0
2 10 2012 4 9 2012 0
3 10 2012 3 9 2012 0
4 10 2012 5 9 2012 0
1 11 2012 1 10 2012 5
2 11 2012 2 10 2012 4
1 12 2012 1 11 2012 1
2 12 2012 2 11 2012 2
This query will do it:
select
sales.id_type as id_type,
sales.month as month,
sales.year as year,
sales.qty as qty,
prev.month as month_b4,
prev.year as year_b4,
prev.qty as qty_b4
from
sales,
sales as prev
where
sales.id_type = prev.id_type and
((sales.month=1 and prev.month=12 and prev.year=sales.year-1) or
prev.month = sales.month - 1);
I also have it on a fiddle where you can experiment with it:
http://sqlfiddle.com/#!2/ed3af/14
I want to access warehouse of tfs to access the tables like
1)table that stores the code requests submitted,approved,rejected
2)table that stores which employee submitted the code or which team sumitted the code on
what date the code is submitted
3)table that stores th e bugs raised,bugs which are open,bugs closed,bugs priority(p0,p1,p2,p3) and some other details
i want the output in the form as below
date bugsClosed bugsOpen Reason
p0 p1 p2 p3 total p0 p1 p2 p3 total Active Fixed Postponed ByDesign ByRepro total
Jan 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
Feb 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
Mar 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
Apr 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
May 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
Jun 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
Thanks & Regards,
Srinivas P.
Start here... it will guide you to your answer.
http://msdn.microsoft.com/en-us/library/ms244696(v=vs.90).aspx
and here for a walk-through of the schema
http://msdn.microsoft.com/en-us/library/ms244711(v=VS.90).aspx
and here for writing custom reports on schema
http://msdn.microsoft.com/en-us/library/bb649552(v=VS.90).aspx