I have a database with tables named Mark, Mask, Matk, Mauk
Each tables have some columns and a column named date where the current date is stored in the php's date("h:i:sa d/m/Y") format.
Now I want to select the table names whose last column's date is less than 5 mins away from now.
I hope you understand my above statement.
I know I would have to do this for the last part: TIMESTAMP(NOW())-TIMESTAMP(Last columns date) < 300.
However I couldn't do the other parts.
P.S I am using the database in a C# app.
EDIT:I'm guessing the code should be something like this:SHOW TABLES FROM db WHERE NOW()-SELECTdateFROM TABLES ORDER BY date DESC LIMIT 1 <= 300
How about leaving out the timestamp part?
where LastColumnDate >= now() - interval 5 minute
I am assuming you are using MySQL based on your sample code.
Related
The database stores the currency exchange rate on a given day. Each day, one currency exchange value is collected and stored in the database as:
ID (int, AI)
VALUE
DATE
1
2.5
20.01.2021
2
2.7
21.01.2021
3
2.6
22.01.2021
If I would like to calculate the average exchange rate from the last 10 days, should I first sort the data by date and only retrieve the last 10 records when downloading the data, or is it enough to download the last 10 records from the database without sorting?
You can simply do in SQL Server database
SELECT TOP 10 AVG(VALUE) AS AverageRate
FROM YourTable
ORDER BY Id DESC
Concept should be same in other relational databases.
Tables (and table expressions such as views and CTEs) in SQL represent unordered sets. To get data in a particular order, you must specify an ORDER BY clause.
In fairly generic SQL you can do
SELECT AVG(VALUE) AS AverageRate
FROM (
SELECT VALUE
FROM YourTable AS t
ORDER BY Id DESC
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
) AS t
In some RDBMSs, instead of OFFSET FETCH, you use either LIMIT or TOP to achieve the same effect. You still need ORDER BY.
You can do it in both ways.
If you're using SQL with Dapper or ADO.NET, then you can write a query.
It should be sorted if you need the last 10 values
SELECT TOP 10 AVG(your value) AS average
FROM YourCurrencyExchangeTable
ORDER BY DATE DESC
If you're using EntityFrameWorkCore, you can write
var avg = db.yourDbContext
.Where(c => c.Date >= tenDaysAgoDate)
.Average(c => c.yourValue)
I hope my answer helps :)
Basically you have to sort first ( on date) and then get the last 10 values, so you're on the right track.
So I'm working on a c# project that creates row in a table with a datetime column and then extract that auto generated ID of that column afterwards. My way of extracting the ID is through the code below the problem is the length it takes to execute the first query and then the second query is more than 1 second so I end up getting empty rows. So how do I insert or select rows minus the seconds?
INSERT INTO transactionlog(transactionDate) VALUES(NOW())
AND THEN IMMEDIATELY THIS
SELECT transactionID,transactionDate FROM transactionlog WHERE transactionDate=NOW();
NOW() gives you the date/time when the statement it contains started. While it is guaranteed that several invokations in the same query return the same value, this is not true accross queries.
To get the auto-incremented value generated during an insert, you can use LAST_INSERT_ID() instead:
INSERT INTO transactionlog(transactionDate) VALUES(NOW());
SELECT LAST_INSERT_ID();
You can utilize mysql DATE_FORMAT() function to control to format.
SELECT transactionID,transactionDate FROM transactionlog WHERE transactionDate=DATE_FORMAT(NOW(), '%c/%e/%Y %H:%i');
You can have a look here at: https://www.w3resource.com/mysql/date-and-time-functions/mysql-date_format-function.php
if you want to play with the format specifier
I have some very odd behaviour from a MySQL query in my C# application which is pulling rows from a Table called project_info. It should retrieve all the Rows which Equal the Customer's Name, and the project start date is BETWEEN the DATE range in the query. The current query I am using works but the date range appears to behave very oddly...
For example if I have a Row in my Table which contains a project for Customer 'WHSmith' and they have a project with a start date of '30/07/2018' when I search with a date range BETWEEN 18/06/2018 AND 01/08/2018 the query returns no results, but if I repeat the query using a date range BETWEEN 18/06/2018 AND 02/08/2018 the Row is returned???
The query I currently have is:
SELECT * FROM project_info WHERE cust_name = "WHSmith" AND proj_date BETWEEN 2018-06-18 AND 2018-08-01;
The date is stored as a DATE field in the table so no Time is included in the query or the value... The cust_name and dates are held in variables within my application but I have checked these are correct and seem OK.
Does anyone have any idea why the BETWEEN query is behaving so oddly? or maybe suggest a better way to look for Row's in a DATE range... many thanks.
I've been currently working on a stored procedure that grabs all the clients I have in the database that have loans. How I'm filtering the results is by the start and end dates given when generating the data. The way my data works is that the returned table will have multiple date fields, 9 date fields to be precise. Each of these date fields are used depending on the LoanStatus field that is also a part of this table. LoanStatus can have values such as Settled, Approved, Cancelled.. What I want to do is to filter the results using this start and end date but applying it to all of the date fields
In my head I've got a couple of pseudo ways to possibly work this issue out:
First Possible Solution
Need some code that'll allows me to essentially do this:
allDatefields > #startDate AND allDatefields < #endDate.
Second Possible Solution
Is to only take into consideration the date field that matches with the loan status. For example, if the LoanStatus is Settled, then look at the DateSettled column of the table, or if the LoanStatus is Approved, then look at the ApprovalDate column of the database.
Personally, I would love to be able to filter through and apply the #startDate and #endDate to all date fields rather than have to look at each date seperately.
Is there a way in SQL Server to apply a WHERE clause to all date fields in a table?
To my knowledge, the where clause does not support your first scenario. I think you might have to settle for something like this:
(LoanStatus = 'Approved' and ApprovalDate > #startDate AND ApprovalDate < #endDate) OR (LoanStatus = 'Settled' and SettleDate > #startDate AND SettleDate < #endDate) OR ... same for the rest of loan status values
I am working on a database which has around 2 year data and has around 100 million rows and 30 columns with values of every 10 seconds of different parameters. I want to create a new table which will have average of these data containing only 1 row for each date of data. The database has around 100 000 rows for each date.
Table name is process
and primary key is id
How can I do it because whenever I search for something in this already existing table it takes a long time to find out the required output.
is it possible to create a new table which will take the average of all the data(around 1 lakh rows) of a single date and put them in one row
You want something like:
CREATE TABLE averages AS
SELECT
date_trunc('day', data_capture_timestamp_column) AS day,
avg(col1) AS col1_avg,
avg(col2) AS col2_avg,
...
FROM my_table
GROUP BY 1;
The GROUP BY 1 says to GROUP the data by the first SELECT argument, which in this case is the date. An expression index on my_table( date_trunc('day', data_capture_timestamp_column)) is recommended.