Mysql find names not in previous dates [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a Mysql database that contains a list of names and dates of volunteers going back five years. What statement would I use to generate a list of new names after a specific date (i.e. people that have not volunteered in the past).
Here is an example, I have a list of 100 different volunteers who have volunteered many times on different dates over 5 years to the present. Some may have volunteered once, others 5 times, etc. Dave is a new volunteer who volunteered on February 6, 2015. I wish to generate a list of new volunteers after 2014-11-29. This list would pick up Dave's name only in this instance.

Try this:
SELECT name
FROM your_table
WHERE column_date < DATE_SUB(NOW(), INTERVAL 5 YEAR);
Or:
SELECT name
FROM your_table
WHERE column_date < DATE_SUB(CURDATE(), INTERVAL 5 YEAR);
EDIT (after the comments):
Try this:
SELECT DISTINCT aa.name
FROM your_table AS aa
WHERE aa.id NOT IN (
SELECT id
FROM your_table
WHERE column_date < '2014-11-29'
);

Related

SQL query. Should I loop through specific date range? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am making a hotel management system. I need your help with appropriate statement to execute. Can you please have a look?
This is all room_types = 3, their check-in and check-out date
If I make query "SELECT * from rooms WHERE room_type = 3 AND check_in_date BETWEEN '2021-02-12' AND '2021-02-13'" this is what it returns
it returns only one room
but BETWEEN '2021-02-12' AND '2021-02-13'" there will 5 rooms with room_type = 3 in house. How can write a query that returns it?
I need to return these
Because all those 4 rooms with type=3 will be in house BETWEEN '2021-02-12' AND '2021-02-13'
I am using MariaDB with ORACLE syntax.
Thank you!
I suspect that you want a date range overlap:
select *
from rooms
where room_type = 3
and check_in_date <= '2021-02-13'
and check_out_date >= '2021-02-12'
This brings rooms that have a reservation that overlap the given range.
From your first screenshot, I see only one room with a checkin date between the 12th and the 13th. That room is returned in the result in your second screenshot. To my eye that one row is the expected result for the criteria you specified.
Maybe as GMB suggests, you are looking to consider both the checkin date and the checkout date?
Karl

same table update from previous quarter row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a daily stock transaction table
T1(symbol, transDate, closingPrice, PrevQtrChange).
The last column is empty. I need an update statement that, for a given symbol, will get the closing price from the previous quarters transaction. Because of weekends, holidays, etc, i can't do a self join on the date being date-90 days. I could do it with a cursor, but ugh. And, the table contains millions of rows, so a cursor would be extremely slow, even with an index.
I'm a C/C++ programmer so while I know some SQL, doing this efficiently is something I'm unsure of.
Thanks in advance.
You can use window functions. The idea for the previous price is:
select t.*,
last_value(closingPrice) over
(partition by symbol
order by transDate
range between unbounded preceding and interval 90 day preceding
) as prev_quarterprice
from t;
You can then incorporate this into an update:
update t join
(select t.*,
last_value(closingPrice) over
(partition by symbol
order by transDate
range between unbounded preceding and interval 90 day preceding
) as prev_quarterprice
from t
) tt
on tt.symbol = t.symbol and tt.transDate = t.transDate
set t.PrevQtrChange = closingprice - tt.prev_quarterprice
where tt.PrevQtrChange is null ;

SQL query for date change for upcoming events [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to run a query in sequelize or SQL where select current month data and only show just upcoming date data like some kind of events if that day pass then next event or date will be showing like this I have 3 dates in this month
1 2020-03-15
2 2020-03-22
3 2020-03-27
So before 15 I want to only no 1 date after 15march pass I want only no 2 date and goes on
If you want to show the next date in the future, then it would be something like:
select t.*
from t
where date > current_date
order by date asc
fetch first 1 row only;
The exact syntax might vary by database -- say now() or getdate() instead of current_date; or select top (1) or limit 1 instead of the fetch clause.

mysql retrieve data from a range of time from table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
-this is the table of picture i want to retrieve datas from
I need two queries.
the first one is to get the actionNo of the latest stopTime before the current time.
the second query is to get the actionNo if the latest actionTime before the current time.
Example:
If the current time was: 00:46:20
the first query is to get actionNo 9 (stoptime: 00:45:00)
the second query is to get actionNo 8 (actiontime: 00:41:30)
-this is eg of range to get data picture
SELECT MAX(id) FROM timestuff WHERE `actionTime` < CURTIME();
SELECT MAX(id) FROM timestuff WHERE `stopTime` < CURTIME();
This gets you the latest id of the action/stoptime before the current time, will work if your table always is in the order as you have shown (that means, ids display the chronology of your time values - which isn't the case in your data as I just noticed).
For your case: you can take another step and add a inner join on the time closest to the current time and then look up the id of that data.
First query: For the latest stopTime before the current time:
SELECT
timestuff.id
FROM
(
SELECT
MAX(stopTime) AS maxTime
FROM
timestuff
WHERE
`stopTime` < CURTIME()
) AS time
INNER JOIN timestuff ON timestuff.`stopTime` = time.maxTime;
Second query: For the first actionTime after the current time:
SELECT
timestuff.id
FROM
(
SELECT
MIN(actionTime) AS minTime
FROM
timestuff
WHERE
`actionTime` > CURTIME()
) AS time
INNER JOIN timestuff ON timestuff.`actionTime` = time.minTime;

How to sum duplicated values in SQL [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
The sum of the acquisition price of works of art for each year (for example, if there were two works of art purchased for $1500 and $1000 in 2007, and one work of art purchased for $500 in 2008, then the sums would be $2500 and $500, for 2007 and 2008 respectively).
Assuming your table contains a field containing the year, and a field containing the price, you would simply use:
SELECT AcquisitionYear, SUM(Price) AS TotalPrice
FROM MyTable
GROUP BY AcquisitionYear
If your table contains a date field, you'd need to extract the year from this field using the YEAR() function:
SELECT YEAR(AcquisitionDate), SUM(Price) AS TotalPrice
FROM MyTable
GROUP BY YEAR(AcquisitionDate)
The keyword you're looking for is GROUP BY.
So your query will look something like
SELECT YEAR(date), SUM(price)
FROM tableName
GROUP BY YEAR(date)