Im doing MYSQL query.
Here's my table.
I need a query that results a row that contains value in added counts.
For example, if i'm looking for value of 20,000 counts, result row should be 3rd row where date is '2016-09-03'((8576+8694+8689)>20000).
There is something wrong in my table schema, but help me querying.
Thank you.
You're looking for such a query, assuming date is unique:
select counter.*
from counter
inner join counter c2
on counter.date >= c2.date
and c2.searchword = 'cryptocurrentcy'
where counter.searchword = 'cryptocurrency'
group by counter.date
having sum(c2.count) > 20000
order by counter.date
limit 1
Related
In MYSQL, I have a dataset which contains stock data of many many dates, and I want to delete all the rows that have the lowest date value (my date value is an integer, so in other words, I want to delete trades from the first date). I have found this code on stack overflow, but it only deletes one row, while I want to delete ALL rows with this minimum date.
This is the code I have found.
DELETE FROM TABLE
ORDER BY DATE
LIMIT 1
Thank you!
Use a query to find out minimum date then delete all records for this date.
-- MySQL
DELETE t
FROM TEST_TRADES t
INNER JOIN (SELECT MIN(COB_DATE) a
FROM TEST_TRADES) tt
ON t.COB_DATE = tt.a
Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4262ea268d2d9ba9b617142edf536cdc
I found solution to this question:
This is what I've done:
SELECT #MIN_DATE := MIN(DATE) FROM TABLE T;
DELETE FROM TABLE WHERE T.DATE = #MIN_DATE
Leetcode problem link: https://leetcode.com/problems/rising-temperature/
The solution that I don't understand:
SELECT
weather.id AS 'Id'
FROM
weather
JOIN
weather w ON DATEDIFF(weather.recordDate, w.recordDate) = 1
AND weather.Temperature > w.Temperature
;
Here weather and w (Alias of weather) are the same tables and DATEDIFF is comparing dates but I don't understand this, If weather and w are the same tables then doesn't that mean that DATEDIFF is comparing the same rows. The solution is correct which means that both rows are not same how?
The table is the same and the columns being compared are the same but not the same rows satisfy the conditions on both tables.
Simplistic put a table join is a internal product of the rows of the tables involved. Which means that a self join of a table with 3 rows will return 9 rows.
When you set conditions, the result set is filtered and only the combined rows that satisfy them will be returned. In the exercise, the conditions are a relation between the row of the first table instance and the second instance.
It is better if you alias both copies of the table:
SELECT w1.id
FROM weather w1 JOIN weather w2
ON DATEDIFF(w1.recordDate, w2.recordDate) = 1 AND w1.Temperature > w2.Temperature;
What this query does is join every row of the table with every other row of the same table which has as recordDate the previous day and less Temperature and returns the id of the 1st copy (if the conditions are satisfied).
In fact all rows of the table are compared against all rows, but when it comes to the same rows they are rejected because for them the conditions fail.
Also, note that your query may return the same id more than once, because it could happen that for a row of the table there may exist more than 1 other rows where the date is 1 day less and the temperature is less.
So maybe you want:
SELECT DISTINCT w1.id
.....................
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.
Could anyone let me know how to limit the number of values in GROUP_CONCAT for each group in MySQL? I am using the below query which also produces more than 2 concatenated values for each group
SELECT GROUP_CONCAT(remaining)
FROM `busroute`
GROUP BY bus
Could anyone let me know how to modify the above query for my problem?
I don't know of a way to limit the number of rows that are grouped, and I don't think that you can do it.
But if you are only going to want to have two rows that you want to group, you can do the grouping and group_concat manually and only group two rows at a time:
SELECT t1.bus, concat(t1.remaining, ',', t2.remaining)
FROM busroute as t1
JOIN busroute as t2 on t1.bus = t2.bus
WHERE t1.id < t2.id
Here we've just gotten two copies of the busroute table and then joined them together on the bus number, then we take some unique column value in the row (which could be any column as long as it's column as the unique attribute set on it) and eliminate matches of a row against its self. I used '<' rather than '<>' since I only want to match the same pair of non-unique rows once.
I have the classic 'get all rows in one table with number of corresponding rows in another table' issue which should be solved by this query:
SELECT
ideas.id,
ideas.idea,
submitted,
COUNT(votes.id) AS vote_count
FROM ideas
LEFT OUTER JOIN votes ON ideas.id = votes.idea
WHERE dead = 0
GROUP BY votes.idea
ORDER BY vote_count DESC, submitted DESC
LIMIT 10;
There are 4 rows (with dead = 0) in ideas and one row in votes (relating to the first idea). However this query returns two records (idea #1 and idea #2) with correct vote_counts. Why is this not returning all of the records in ideas?
When you say GROUP BY votes.idea, you are asking for one result row per idea value in votes. Since you say votes has only one row, you should expect only two records in the result — one corresponding to the idea value in that row of votes, and the other with NULL (condensing the three rows with no matching vote record).
Did you mean GROUP BY ideas.idea?
Change:
GROUP BY votes.idea
to:
GROUP BY ideas.id
Because votes.idea can be NULL.