MySQL Order year field by closest match - mysql

I'm trying to order some dates so that they are listed in a particular order closest to a specified date:
id year
---------
1 2010
2 2011
3 1992
4 1996
5 1987
6 1988
The result should look like (if I specify 1992 as the year to order by):
id year
---------
3 1992
4 1996 <4 away
5 1987 <5 away
6 1988 <6 away
1 2010 <8 away
2 2011 <9 away
I want it so that it returns all dates, but the most relevant (closest) first in the list.
Sorry I have revised my question to make it more clear what I'm trying to achieve.

Am I missing something? Isn't it just:
SELECT *
FROM table
WHERE year >= '1992'
ORDER BY year ASC
Then to edit I guess...
SELECT *, ABS(year - 1992)
FROM table
ORDER BY ABS(year-1992) ASC
I think that will work

This should solve your problem.
SELECT *
FROM `table`
ORDER BY ABS(1992-`year`) ASC

Related

MYSQL - Select count with group by with only a table

I have these data
idhouse year
7 2016
2 2018
2 2017
3 2017
4 2015
14 2003
3 2018
5 2018
4 2017
4 2018
I want to counting the number of houses belong to two years.
I tried with mysql select but didn't work.
How I should do it?
EDITED
Sorry for my bad explanation.
I have only one mysql table.
Filtering by 2017 and 2018 and count the numbrer these houses, I should get these match:
idhouse year
7 2016
2 2018*
2 2017*
3 2017*
4 2015
14 2003
3 2018*
5 2018
4 2017*
4 2018*
And the SELECT should be show 3
I assume a house can only appear once in a year. Try this:
SELECT
COUNT(*) nb_houses
FROM (SELECT house
FROM yourTable
GROUP BY house
HAVING COUNT(*)>1) A;
See this run on SQL Fiddle.
Assuming a PK on (house,year), if you just want to know how many houses are listed more than once, you can do this...
SELECT COUNT(DISTINCT x.house) total
FROM my_table x
JOIN my_table y
ON y.house = x.house
AND y.year <> x.year;
I asuming you're doing a web app with this question
SELECT house, COUNT(year) as count_year FROM table GROUP BY house HAVING COUNT(year) = 2
By using your data above, the result will be
house | count_year
____2 | _______2
Then if you are using server side scripting like PHP use mysqli_num_rows for get the number of row(s).
Or if you use other language, just adjust the algorithm to get the number of row(s)
With this Select I get it:
select count(*) from (select house from yourTable Where year = 2018 and house in (select house from yourTable where year = '2017')) A;
But can we improve this Select in terms of efficiency?
You can try here
Thanks.

With a GROUP BY select, can you sort by the projection's SUM?

I have a SELECT that uses GROUP BY X. It uses SUM in the projection. Is there any way to sort on that SUM? The group result with the highest value should be first in results table. I suspect there is no way to sort on this SUM since as each group is completed it's output and gone; there isn't any "collection" to sort. There is something totally different I have to do here. Do you have any hints?
Thank you.
(beginner)
If I understand your question right, the answer is quite simple:
SELECT customer, SUM(amount) FROM mytable GROUP BY customer ORDER BY SUM(amount);
I'm not sure you meant this but here is an example
assume that there is data in a table which shows payments people have made to a bank.
But people pay different money in different times.
*id* *payment* *time*
1 10 01 04 2016
2 20 01 02 2016
1 35 14 03 2016
3 22 21 01 2016
2 50 01 04 2016
Now you want to calculate who has paid max money.
SELECT person_id, MAX(payment) AS totalPayment
from payments
group by person_id
ORDER BY totalPayment DESC

Check if instances have occurred minimum once, every year in a specific range

In MySQL I'm tasked with a big dataset, with data from 1970 to 2010.
I want to check for consistency: check if each instance occurs minimum one time per year. I took a snippet from 1970-1972 as example to demonstrate my problem.
input:
id year counts
-- ---- ---------
1 1970 1
1 1971 1
2 1970 3
2 1971 8
2 1972 1
3 1970 4
expected:
id 1970-1972
-- ----------
1 no
2 yes
3 no
I though about counting within the date range and then taking those out who had 3 counts: 1970, 1971, 1972. The following query doesn't force the check on each point in the range though.
select id, count(*)
from table1
WHERE (year BETWEEN '1970' AND '1972') AND `no_counts` >= 1
group by id
What to do?
You can use GROUP BY with CASE / inline if.
Using CASE. SQL Fiddle
select id,CASE WHEN COUNT(distinct year) = 3 THEN 'yes'ELSE 'No' END "1970-72"
from abc
WHERE year between 1970 and 1972
GROUP BY id
Using inline IF. SQL Fiddle
select id,IF( COUNT(distinct year) = 3,'yes','No') "1970-72"
from abc
WHERE year between 1970 and 1972
GROUP BY id
You can use a having clause with distinct count:
select `id`
from `table1`
where `year` between '1970' and '1972'
group by id
having count(distinct `year`) = 3
Do you expect this?
select id, count(*)
from table1
WHERE (year BETWEEN '1970' AND '1972')
group by id
having count(distinct year) = 3

Count occurrences of distinct values

I am trying to find a MySQL query that will display the number of occurrences of an ID value within a year.
Table:
a_id year
---- ----
1 2010
1 2011
1 2012
1 2012
1 2013
1 2014
1 2015
2 2010
2 2011
2 2013
2 2014
2 2014
2 2015
3 2010
3 2010
3 2011
Expected output:
a_id year occurrences
---- ----- -----------
1 2010 1
1 2011 1
1 2012 2
1 2013 1
1 2014 1
1 2015 1
2 2010 1
2 2011 1
2 2013 1
2 2014 2
2 2015 1
3 2010 2
3 2011 1
I'm trying with the something along the lines of following sql query, but it gives me nothing like the expected output. It's the 3rd column im struggling with.
SELECT a__id, year, count(distinct a_id) as occurrences
FROM table1
GROUP by year
ORDER by a_id
How can i create that 3rd column?
Scince you are grouping by a_id and year you of course get only 1 distinct value for group. Simply change count(distinct a_id) to count(*).
For example you get group:
1 2012
1 2012
Notice in this group distinct a_id values is 1. But you want count of all rows in group. With distinct you will get 1 as occurence in all groups.
EDIT:
Ok, I have missed that you are grouping only by year, so you should group by a_id also. The rest of the answer stays as is. So you end up with:
SELECT a__id, year, count(*) as occurrences
FROM table1
GROUP by a__id, year
SELECT a__id, year, count(*) as occurrences
FROM table1
GROUP by a__id, year
Using the following will get you what you are looking for.
SELECT a_id, year, count(*)
FROM table1
GROUP BY a_id, year
ORDER BY a_id, year
Although, in previous versions, ORDER BY may have been guaranteed by MySQL, it is deprecated now. If you want to ensure your results come back sorted, add ORDER BY. 'Future you' will thank you for it.

MySQL select all last added query

Im trying to select all the items that are recently added.
I have to find what is the last added exam (sometimes more that one as shown below)
Here is an example of the DB.
id name start_date end_date duration
4 Exam August 2011 24.8.2011 0000-00-00 20
3 Exam July 2011 28.7.2011 0000-00-00 20
5 Exam August 2011 24.8.2011 0000-00-00 20
6 Exam August 2011 24.8.2011 0000-00-00 20
25 Exam September 2011 26.9.2011 07.10.2011 20
26 Exam September 2011 26.9.2011 07.10.2011 20
27 Exam September 2011 26.9.2011 07.10.2011 20
And here is the query that im using, my question is is there a better way to do it? Some optimization?
SELECT * FROM me_tests WHERE name = (
SELECT name FROM me_tests ORDER BY start_date DESC LIMIT 1
)
The second query will find the name of the last added (same as start date) one, will pass it to the first select and will select all the other tests based on the name.
SELECT * FROM me_tests ORDER BY start_date DESC LIMIT 1
Or -
SELECT * FROM me_tests
WHERE start_date =
(SELECT MAX(start_date) FROM me_tests);
What if the last added items were added at the same time but have different names?
In that case I would think you would want a query like this.
SELECT
*
FROM me_tests
WHERE start_date = (SELECT
start_date
FROM me_tests
ORDER BY start_date DESC LIMIT 1)
I would use
SELECT * FROM me_tests ORDER BY start_date DESC LIMIT 1
... except using * is bad, so you should name all the columns you want to retrieve.