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);
Related
I have a linking table in MySQL which has the id's of people in one column and the id's of media that they have appeared in.
I have run a query:
SELECT person_id,COUNT(*)
FROM person_media
GROUP BY person_id;
This has returned:
person_id COUNT(*)
1 7
2 4
3 9
4 5
5 9
6 12
7 12
8 3
9 1
10 8
11 8
12 9
13 3
14 1
15 4
16 3
17 3
18 1
19 8
20 1
21 4
What I am looking to do is to count how many people have a COUNT(*) of 4 or more. I can't seem to find a way to do it, but I know it has to be possible. I'm relatively new to MySQL, so be gentle :)
Just use HAVING
SELECT person_id,COUNT(*)
FROM person_media
GROUP BY person_id
HAVING COUNT(*)>=4
The inner query gets all those with a count of 4 and more.
The outer query counts how many that are:
select count(*)
from
(
SELECT 1
FROM person_media
GROUP BY person_id
HAVING count(*) >= 4
) tmp
emp_id project_id
3 2
3 3
7 1
7 2
7 3
7 4
3 27
7 32
3 31
8 2
8 3
9 2
9 3
Above is table of works on. How to fetch which employee is working on which project in mysql
Use GROUP_CONCAT:
SELECT emp_id, GROUP_CONCAT(project_id)
FROM mytable
GROUP BY emp_id
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 would like to group by one column and sum up another column by doing so.
If I ask
SELECT * FROM Name_Data WHERE Name = 'Tim'
I will get a column named 'Count' that shows me how much Tim's there are per ZIPcode
ZIPcode Place Year Name Count
3042 Kleinpolder 1993 Tim 7
3042 Kleinpolder 2003 Tim 6
3051 Kleiwegkwart 1983 Tim 14
3051 Kleiwegkwart 1993 Tim 9
3059 Nesselande 1993 Tim 8
3059 Nesselande 2003 Tim 10
3068 Ommoord/z 1983 Tim 24
3068 Ommoord/z 2003 Tim 15
3065 s-Gravenland 1993 Tim 21
3011 Weena 2003 Tim 5
I would like to use GROUP BY ZIPcode and by doing so also sum up all the Count numbers. I have read how to use the SUM command but I can't figure out how to sum up the Count column.
So
3042 should be 7 + 6 = 13
, 3051 should be 14 + 9 = 23 and so on..
select sum(`Count`) as `Count`,Name,ZIPcode
from
Name_Data
WHERE Name = 'Tim'
group by ZIPcode
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