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 want to select info which was registered in database earlier first one by one,
its i want to select oldest info first then newer, like to be in order (first come first serve) one by one
like
Today - some info
Yesterday - some info
day before yesterday - some info
SELECT info FROM table ORDER BY A DESC LIMIT 1
table
id info date_registered
1 john 9999-12-31 23:59:59
Your question is vague, but there's various ways you can do it.
What are you doing with this?
check out OFFSET and LIMIT, which you'd do something like run your query and then increase the offset each time you run it.
just use whatever programming language you're using to loop over the result set? IE remove the limit.
3 it multiple times and add a where clause which uses the previous return value.
SELECT info
FROM table
where date_registered < '9999-12-31'
order by date_registered desc
limit 1
Related
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 the above table structure in the database and I want to calculate the upcoming abbreviation for an id (if a particular abbreviation has an actual date present (achieved) then it should give the current_date(which will be upcoming date even if it's in past) and next upcoming abbreviation for that particular id): if the last abbreviation has an actual date (for which category is also present).
expected result:
I'm new to SQL, i guess it can be achieved by taking maximum of actual_date present for abbreviation for a id.
If you want the abbreviation for each id with the maximum actual date, you can use row_number():
select t.*
from (select t.*,
row_number() over (partition by id order by actual_date desc) as seqnum
from t
where actual_date is not null
) t
where seqnum = 1;
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
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 ;
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.
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 9 years ago.
Improve this question
Web client requests records by batches of 10 specifying the from parameter. How do I need to write SQL query to select records from 1 to 10, from 11 to 20 and so on?
LIMIT is what you are looking for
SELECT
*
FROM
`table`
ORDER BY
`somecolumn`
LIMIT 0, 10
LIMIT 0, 10 means starting from record 0 take 10 rows. LIMIT 10, 10 would be the next page skipping 10 and taking 10 more rows.
Use the offset of the limit clause and don't forget to order by a specific column to get always the same results. To get records 21 to 30:
select * from your_table
order by some_column
limit 20, 10