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
Related
I am trying to write a MySQL select statement which will use table1 and table2 to generate a table3.
table1
id
Month
Year
Name
Length
Breath
1
January
2002
Square
5
2
2
February
2003
Circle
6
3
3
March
2004
Cylinder
7
4
4
April
2005
Cube
8
5
5
May
2006
Quadilateral
9
6
table2
id
Month
Year
Name
Frequency
Area
Volume
1
January
2002
Square
1
20
50
2
Febrauy
2003
Circle
2
25
55
3
March
2004
Cylinder
3
Null
60
4
April
2005
Cube
4
35
65
5
May
2006
Quadilateral
5
40
Null
table3 the generated table should be like this.
Name
Type
Month
Year
Length
Breath
Frequency
Volume/Area
Cum_Area
Cum_Volume
Area
Volume
Square
2d
January
2002
5
2
1
2.5
20
50
20
50
Circle
2d
February
2003
6
3
2
2.2
45
105
25
55
Cylinder
3d
March
2004
7
4
3
2
45
165
30
60
Cube
3d
April
2005
8
5
4
1.86
80
230
35
65
Quadilateral
4d
May
2006
9
6
5
1.75
120
230
40
70
In the statements I've tried whenever the cumulative column encounters the first null value, every row from then on is null.
Also, how do I write the type column? I'd like square and circle to be '2d'; cube and cylinder to be '3d' and Quadrilateral to be '4d'.
Table 1 and 2 have the Name, Month and Year column in common.
I don't finish to understand what is your logic or why you don't have ids on the second table. But assuming what you write, this is a SUGGESTION.
Update your question I'll glad to update my answer.
Assuming null means 0:
SELECT
table1.Name,
CASE table1.Name
WHEN Square THEN '2d'
WHEN Circle THEN '2d'
WHEN Cylinder THEN '3d'
WHEN Cube THEN '3d'
WHEN Quadilateral THEN '4d'
END as Type,
table1.Month,
table1.Year,
table1.Length,
table1.Breath,
table2.Frequency,
(IFNULL(table2.Volume,0) / IFNULL(table2.Area,0)) as 'Volume/Area',
//I don't understand what do you need in this field,
//I don't understand what do you need in this field,
table2.Area,
table2.Volume
FROM table1
JOIN table2
ON table1.Name = table2.Name AND table1.Month = table2.Month AND table1.Year = table2.Year
I want to create a SQL query that count the number of babies born in month A, then it should count the babies born in month B but the second record should have the sum of month A plus B. For example;
Month | Number
--------|---------
Jan | 5
Feb | 7 <- Here were 2 babies born but it have the 5 of the previous month added
Mar | 13 <- Here were 6 babies born but it have the 7 of the two previous months added
Can somebody maybe please help me with this, is it possible to do something like this?
I have a straight forward table with babyID, BirthDate, etc.
Thank you very much
Consider using a subquery that calculates a running count. Both inner and outer query would be aggregate group by queries:
Using the following sample data:
babyID Birthdate
1 2015-01-01
2 2015-01-15
3 2015-01-20
4 2015-02-01
5 2015-02-03
6 2015-02-21
7 2015-03-11
8 2015-03-21
9 2015-03-27
10 2015-03-30
11 2015-03-31
SQL Query
SELECT MonthName(BirthDate) As BirthMonth, Count(*) As BabyCount,
(SELECT Count(*) FROM BabyTable t2
WHERE Month(t2.BirthDate) <= Month(BabyTable.BirthDate)) As RunningCount
FROM BabyTable
GROUP BY Month(BirthDate)
Output
BirthMonth BabyCount RunningCount
January 3 3
February 3 6
March 5 11
I have a table named Prices. Which contain ID, fkProductID (foregnkey of Products Table), PriceDate, Price, fkStore (the supplier ID who gives the prices)
Now I need to find the best price for each product for each month and the SUPPLIER name also.
Also some suppliers are giving duplicates prices. For example product id 8 got two times 1200 (ID# 8 and 3). If possible i only need to show the first supplier or a column with count values..
ID fkProductID PriceDate Price fkStore
-----------------------------------------------------
1 8 26-10-2014 1250 13
2 8 10-09-2014 1200 13
3 8 25-10-2014 1200 1
4 8 13-10-2014 1500 1
5 8 03-09-2014 1000 1
6 8 15-09-2014 1300 15
7 8 09-09-2014 950 21
8 8 10-10-2014 1200 23
9 8 09-09-2014 950 27
10 15 10-10-2014 3500 5
11 15 11-10-2014 3400 6
12 15 09-09-2014 3100 6
13 15 10-09-2014 3200 14
14 15 16-09-2014 3100 17
-----------------------------------------------------
my expected result.
-----------------------------------------------------
ID fkProductID Month Price Supplier
-----------------------------------------------------
7 8 September 950 21
2 8 October 1200 1
13 15 September 3100 13
11 15 October 3400 6
=================================================================
SCHEMA
SQL FIDDLE
You can do what you want with the substring_index()/group_concat() trick:
select substring_index(group_concat(idPrices order by Price, idPrices), ',', 1) as id,
fkProductId, monthname(PriceDate) as mon,
min(Price) as price,
substring_index(group_concat(fkStore order by Price, idPrices), ',', 1) as spec_id
from prices p
group by fkProductId, monthname(PriceDate);
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
I have a table like this.
id Person year
1 4 2001
2 7 2001
3 5 2001
4 9 2001
5 4 2001
6 7 2002
7 2 2002
8 5 2002
9 4 2002
10 6 2002
11 6 2003
12 4 2003
13 9 2003
14 2 2004
15 3 2004
16 7 2004
17 5 2004
18 7 2004
19 9 2005
20 8 2005
I would like for it to print all the years that person '9' has not been a part of.
I thought this was as simple as simply putting WHERE person!=9 GROUP by year, but that doesn't work, as if it ignores my where command.
It does however work the other way around when I say person=9 - then I only get the years that he has been a part of.
I have tried joining the table with itself. But no luck.
try:
... WHERE year NOT IN (SELECT year WHERE person = 9) ...
select distinct yr.year
from your_table as yr
left join
(
select distinct year
from your_table
where person=9
) as not_exist
on yr.year=not_exist.year
where not_exist.year is null;
OR
select distinct year from your_table
where year not in (select distinct year from your_table where person=9);