MYSQL Delete all rows with the minimum DATE value - mysql

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

Related

Find duplicate value in the table via sql

I have a table called stocktransfer
Rows truncated to keep it simple here.
as per the image the problem with the record is that there is duplicated transaction number across two different invoice number which is incorrect to context of how business logic is.
So duplicate transaction is expected as long as it is under the same invoice number.
I wrote this query but it does not help since the duplication is expected.
Select strefno,sttran,STDATE,count(sttran)
From postfrh
Group By sttran,strefno,STDATE
Having Count(sttran) >1
Order By sttran
Can anyone please help with how to write a logic to find duplicated transaction where invoice numbers are different two.
strefno > TransctionNumber
sttran > InvoiceNumber
STDATE > date
SELECT strefno,
sttran,
STDATE,
row_number ( )
OVER ( PARTITION BY strefno
ORDER BY STDATE ) AS `rowNumber`
FROM postfrh
WHERE strefno IN
(SELECT strefno
FROM postfrh
GROUP BY strefno
HAVING count( sttran ) > 1 )
ORDER BY strefno;
You are probably looking for something like this. I don't have the exact table so I cannot be sure.
select a.tnum
from postfrh as a
, postfrh as b
where a.tnum = b.tnum
and b.inum != a.inum
(tnum = transaction number, inum = invoice number)
There are several ways to approach the problem but the above query works by joining two instances of the table, the first condition in the where clause means that there will only be items with the same transaction number, the second statement filters out transactions that have the same transaction number and invoice number.

How to count the same date values per day

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'

MYSQL matching added number query

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

Select row based on max date across several columns

For a mysql database
I have a table which includes duplicate rows because of date values in several columns. I am looking to select a single row for each unique customer id based on a max date value evaluated across several date columns
[customer id, startDate, StopDate, modifyDate, buyDate]
For each customer id, i'd like to return the row that has the maximum date either in the startDate, StopDate, modifyDate or buyDate columns ( there are some nulls in the date columns.
editing to include example - but to sure how to create a table here:
*** Edit
Been trying for quite awhile now to create a table here with an example. can't figure out. So posting an image? the desired rows to the returned indicated in red.
Assuming that the values are never NULL, you can use greatest():
select t.*
from table t
where greatest(t.startDate, stopDate, buyDate) =
(select max(greatest(t.startDate, stopDate, buyDate))
from t t2
where t2.customerid = t.customerid
);
Note: this will return multiple rows for a customer if more than one row contains the maximum date.

Update mysql id column according to timestamp column

I'm wondering if its possible to update all of the mysql table ids column based on timestamps?
So, I have table with columns ID,TEXT,STAMP, and IDs are really out of order. It's possible to sort table on STAMP and then update all of IDs incremental from 1 (so the oldest entry will have ID of 1) with one single query?
UPDATE table_name a,(SELECT id,(#newid:=#newid+1) AS d FROM table_name,(SELECT #newid:=0) AS f ORDER BY stamp) AS g SET a.id = g.d WHERE a.id = g.id;
i think this will do.