How to count the same date values per day - mysql

I'm completly begginer but and don't even know what type this query is.
BTW. I want to get count Date(MYdatetime) type values per day by join the same table.
Is there any question comparing this query?
I have query like that:
select
date_format(
adddate('2011-1-1', #num:=#num+1),
'%Y-%m-%d'
) date
from
any_table,
(select #num:=-1) num
limit
365

In MySQL, I would simply do:
select date(t.datecol), count(*)
from any_table t
group by date(t.datecol);
If you want data for a particular time span, use a where clause:
select date(t.datecol), count(*)
from any_table t
where t.datecol >= '2011-01-01' and t.datecol < '2012-01-01'
group by date(t.datecol);

Use GROUP BY:
SELECT
year(theDate),
month(theDate),
day(theDate),
count(*)
FROM
test_table
GROUP BY
year(theDate),
month(theDate),
day(theDate)

You are wanting a count of all dates in a given time frame, including those dates that are not in the table (and thus would have a count of 0). This is a bit of a headache but not unprecedented. You basically need to create a utility table that is just every date you could possibly want and then do an outer join against that table. So create a simple table like:
CREATE TABLE `all_dates` (
`the_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And populate it with all dates you think you could potentially need. I think a 10 year radius from the current year would be good. So fill it with every date between 2008-01-01 and 2028-12-31.
INSERT INTO all_dates
(the_date) VALUES
('2008-01-01'),
('2008-01-02'),
('2008-01-03')
There is probably a clever way to generate all rows in one query using a procedure or somesuch, but I don't know what it would be, so I would personally just create a simple script (like in PHP) to generate those rows.
Once the all_dates table has all the date value rows for your needs, you can then do a query to get the count of each date (including missing dates) like:
SELECT DATE(all_dates.the_date), COUNT(any_table.datecol)
FROM any_table
RIGHT JOIN all_dates ON DATE(any_table.datecol) = all_dates.the_date
GROUP BY all_dates.the_date
HAVING all_dates.the_date BETWEEN '2011-01-01' AND '2012-01-01'

Related

How to select records with a count >30?

So I have this data set (down below) and I'm simply trying to gather all data based on records in field 1 that have a count of more than 30 (meaning a distinct brand that has 30+ record entries) that's it lol!
I've been trying a lot of different distinct, count esc type of queries but I'm falling short. Any help is appreciated :)
Data Set
By using GROUP BY and HAVING you can achieve this. To select more columns remember to add them to the GROUP BY clause as well.
SELECT Mens_Brand FROM your_table
WHERE Mens_Brand IN (SELECT Mens_Brand
FROM your_table
GROUP BY Mens_Brand
HAVING COUNT(Mens_Brand)>=30)
You can simply use a window function (requires mysql 8 or mariadb 10.2) for this:
select Mens_Brand, Mens_Price, Shoe_Condition, Currency, PK
from (
select Mens_Brand, Mens_Price, Shoe_Condition, Currency, PK, count(1) over (partition by Mens_Brand) brand_count
from your_table
) counted where brand_count >= 30

INSERT INTO SELECT mysql is trying to insert different data from the selected ones

I've been messing around with MySQL lately and I can't resolve one problem.
Having 2 tables.
status_graf(id,date,time,number of players online)
status_statistika(id,date,min,avg,max)
First one is storing data about online players every 5 minutes, second one is for daily statistics. It's separated, so I don't have to run crazy selects whenever man wants to see min,avg,max online players for last month for example.
This query works, result is a table with dates that haven't been in status_statistika yet.
SELECT date,MIN(playersonline),AVG(playersonline),MAX(playersonline)
FROM status_graf
GROUP BY date
HAVING date NOT IN (
SELECT date
FROM status_statistika
GROUP BY date
)
But when I try this
INSERT INTO status_statistika (
SELECT date,MIN(playersonline),AVG(playersonline),MAX(playersonline)
FROM status_graf
GROUP BY date
HAVING date NOT IN (
SELECT date
FROM status_statistika
GROUP BY date
)
);
it does not.
Using phpMyAdmin 4.3.8, MySQL 5.5.41 and it throws error #1062 - Duplicate entry '2015-01-29' for key 'date'.
Ideas?
SOLUTION:
Since the dot convention somehow didn't accept it, I had to rename column 'date' in one table to 'day' and then it worked
Looks like you have unique index on the date column in the status_statistika table.
INSERT INTO status_statistika (
SELECT DISTINCT date,MIN(playersonline),AVG(playersonline),MAX(playersonline)
FROM status_graf
GROUP BY date
HAVING date NOT IN (
SELECT date
FROM status_statistika
GROUP BY date
)
);
Notice the 'DISTINCT'

MySQL - query records above date and one below

In my MySQL database I have records with date of inserting (as one of column of datetime type).
Besides of some where clause constraints I want to select all records above given date (from given date to now) and exactly one record closest below given date.
Any ideas how to achieve this using MySQL in efficient way?
I thought about using count and rank, but I think it can be very inefficient.
Maybe someting like this?
(SELECT * FROM mytable WHERE mydate >= xxx)
UNION
(SELECT * FROM mytable WHERE mydate < xxx ORDER BY mydate DESC LIMIT 1)

Order By on date field starting in a middle point of the dates range

I have a table "A" with a "date" field. I want to make a select query and order the rows with previous dates in a descending order, and then, the rows with next dates in ascending order, all in the same query. Is it possible?
For example, table "A":
id date
---------------------
a march-20
b march-21
c march-22
d march-23
e march-24
I'd like to get, having as a starting date "march-22", this result:
id date
---------------------
c march-22
b march-21
a march-20
d march-23
e march-24
In one query, because I'm doing it with two of them and it's slow, because the only difference is the sorting, and the joins I have to do are a bit "heavy".
Thanks a lot.
You could use something like this -
SELECT *
FROM test
ORDER BY IF(
date <= '2012-03-22',
DATEDIFF('2000-01-01', date),
DATEDIFF(date, '2000-01-01')
);
Here is a link to a test on SQL Fiddle - http://sqlfiddle.com/#!2/31a3f/13
That's wrong, sorry :(
From documentation:
However, use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because it will have no effect anyway.
This should do the trick. I'm not 100% sure about adding an order in a UNION...
SELECT * FROM A where date <= now() ORDER BY date DESC
UNION SELECT * FROM A where date > now() ORDER BY date ASC
I think the real question here is how to do the joining once. Create a temporary table with the result of joining, and make the 2 selects from that table. So it will be be time consuming only on creation (once) not on select query (twice).
CREATE TABLE tmp SELECT ... JOIN -- do the heavy duty here
With this you can make the two select statenets as you originally did.

MySQL INSERT SELECT. Ordering insert by count but don't actually insert the count number

Sorry for the ridiculous title!
Is it possible to run a MySQL INSERT SELECT query where the 'SELECT' portion includes a COUNT() function (to benefit from a HAVING clause) -- this creates an unmatching column count on the INSERT portion.
For example:
INSERT INTO my_log (user_id, date_finished)
SELECT COUNT(id) AS count, user_id, '2011-05-31 00:00:00'
FROM assignments WHERE assignment_type = 10
GROUP BY user_id
HAVING count >=10
I want to insert into the TABLE my_log a date for users who've completed 10 assignments of the type 10. In reality I'm expanding on this slightly so it's important I use the COUNT and HAVING to find only users who have completed 10 or more assignments. But my problem remains that I'm trying to insert 3 columns into a declared 2 columns because of the COUNT() function, so I'm looking for a way to benefit from the COUNT but not return it in my data set / considered in the insertion.
Thanks for any help :)
Try with:
INSERT INTO my_log ( user_id, date_finished)
SELECT user_id, '2011-05-31 00:00:00'
FROM assignments WHERE assignment_type = 10
GROUP BY user_id
HAVING ( COUNT(id) ) >=10