MYSQL to display only the earliest of duplicates [duplicate] - mysql

This question already has answers here:
Find duplicate records in MySQL
(28 answers)
Closed 8 years ago.
select name, reported_at from nodes
where reported_at < curdate() or reported_at is null
group by name
Output:
name reported at
ncs-linux-test.edu 2012-03-16 18:36:03
ocdev1.net 2012-04-06 16:32:02
pinc-ctm.net NULL
With that statement, I get any results form reported at/name that are less than the current date.
What I need though is the statement to only pull out data that has a duplicate(s) with more current information.
For example:
The statement would only pull out:
ncs-linux-test.edu 2012-03-16
if there was an
ncs-linux-test.edu
with a date more current than 2012-03-16.

select *
from nodes n
join nodes nlater
on n.name = nlater.name
and n.reportedat < nlater.reportedat

Related

Group avarage actions per day in a SQL query for MySQL [duplicate]

This question already has answers here:
MySQL count and group by day
(5 answers)
Closed 5 years ago.
i have a table which has fields like this:
id | created | action_type | value
Created is a timestamp in a varchar field. What i need to do is to calculate the average actions per day in one query. All need to be grouped by one day.
so i need something like this
2017-10-01 : 15
2017-10-02 : 20
How can i achieve this?
:)
Presumably, you just want a group by:
select date(created), count(*)
from t
group by date(created);

mysql select the last value of a day for 7 days [duplicate]

This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 8 years ago.
I have a table with date and Etotalday.
What I want to select is the last value of the last 7 days.
Many solutions I looked for gives the highest value of a day which is not always the same as the last value.
In my case table is build with values and the day starts with the highest value of the day before and at about 06.00 new values are added.
Is there somebody who can gives me a hint how to do this?
thnks
Lookes like duplicate question but I want the last value not the highest value and not the max value.
You can do this with order by and limit
select t.*
from PacData t
where date >= now() - interval 7 day
order by date;
limit 1;
If you want to return the last value for each of the past seven days (which might be the intent of the question), then here is one method:
select t.*
from PacData t
where not exists (select 1
from PacData t2
where date(t2.date) = date(t.date) and t2.date > t.date
);
This says: "Get me all records from the table where there is not record on the same date with a larger value from the date time column". By the way, date is a lousy name for a column that stores both a date and a time.

pulling the following information with a single select [duplicate]

This question already has answers here:
Count columns according to dates in SQL
(3 answers)
Closed 8 years ago.
I have the following information in my mysql table
Eventually I would like to make a select that would count the entries for a page_id but just for the specific date , meaning I would like to have the following output:
84 - 7 - 09/23/2013
85 - 4 - 09/23/2013
84 - 1 - 09/24/2013
Can it be done in a single select ?
select page_id, count(*), date
from table_name
group by page_id, date
SELECT page_id, count(page_id), date
FROM table
GROUP BY page_id, date
You need to SELECT the 3 fields. Then you need to COUNT one of those (page_id) since you need to count how many repetitions you got. And the last step, for the query to run (and also to make sense) is to GROUP BY the other 2 fields.
Hope you get a clearer idea on how to query the table.

MySQL select Display Zero when group by month or week [duplicate]

This question already has answers here:
Mysql to select month-wise record even if data not exist
(2 answers)
Closed 9 years ago.
I have a table with a column date and a column ID (to keep it simple). I am doing a query to count the total and group by week
SELECT MONTH(table1.Date_1st), YEAR(table1.Date_1st), COUNT(table1.Id)
FROM table1 as table1
WHERE Date_1st BETWEEN '2013-04-01' AND '2013-07-25'
GROUP BY WEEK(Date_1st)
the problem is there is no results on specific week the result is not taken into consideration. I already try ifnull(table1.Id,0)
Try ISNULL or other NULL SQL functions:
NULL functions
ISNULL returns a 0 if the value is null.

Mysql date problem [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Select by Month of a field
i want to get the data from database whose month is 7. means
My Date format is
PostDate datetime
now i want to query..
select * from tabelname where PostDate = 7
If you are asking how to query MySQL for date objects based on month, then this may be your answer.
select * from tablename where month(PostDate) = '07';
Keep in mind this function (month()) is MySQL specific.