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 4 years ago.
Improve this question
I am currently trying to solve a MySQL exercise and I am having issues with finding the right command.
There is a table called 'orders' with the following columns: orderNumber, orderDate, requiredDate, shippedDate, status and customerNumber.
The question says to show the orders payed in the summer (columns to be shown: orderNumber and orderDate).
I think the answer should be something like this, but I can't figure out how to precise the month (probably something with IN or BETWEEN but I can't solve it):
SELECT COUNT(orderDate), orderNumber
FROM orders
GROUP BY orderNumber;
You Use the MONTH function to get the month for the date and compare it with the summer period using BETWEEN
SELECT orderNumber, orderDate
FROM Orders
WHERE MONTH(w) BETWEEN x AND y
AND status = z
w is the date column to use, not sure which one it is
x & y represents start and end month of summer where you live
z the status the order gets when it’s paid
Is not clear how you distinguish between paid and unpaid but, anyway, for select the order, if you want the order list you could use
SELECT orderDate, orderNumber
FROM orders
where orderDate between str_to_date('2018-06-21', '%Y-%m-%d')
and str_to_date('2018-09-21', '%Y-%m-%d')
the aggregationion seems not necessary to me ..
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 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 3 years ago.
Improve this question
i need to find the the sum of the salary spent in every year in every department - the problem is that i have two columns of date - how can i "extract" the year from this dates?
What you're likely looking for is using the YEAR function to get the start date of the salary and summing up per department.
This is making the assumption that you want to count the full salary based on the year in which the pay started. Additionally, there may be added complexities if for example, someone worked for multiple departments at once. If you are asking which out of from or to date should be used to count which year the salary falls in, this is 100% up to you based on your requirements.
SELECT
a.dept_no as `Department`,
YEAR(b.From_date) as `Year`,
SUM(b.salary) as `Salary`
FROM
dept_emp a
INNER JOIN
salaries b
ON
a.emp_no = b.emp_no
GROUP BY
a.dept_no,
YEAR(b.From_date)
ORDER BY
a.dept_no
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 6 years ago.
Improve this question
I'm working on a homework assignment, and all has been well until I got to this point. My professor wants me to pull only dates in MARCH, APRIL, and MAY, without using the BETWEEN operator.
NOTE: I'm not getting any errors. I am using EDUPE, which runs MySQL, but has small variances where some things simply won't work.
Question was, is there a way to make the code I have function properly? Or am I going in the wrong direction?
/*Exercise Six*/
SELECT order_id as "Order ID", DATE_FORMAT(order_date, '%M-%d-%Y') as "Order Date"
FROM orders
WHERE order_date IN ('March%', 'April%', 'May%')
ORDER BY order_date ASC;
You can try with date_format again:
WHERE DATE_FORMAT(order_date, '%M') IN ('March', 'April', 'May')
Or just monthname():
WHERE MONTHNAME(order_date) IN ('March', 'April', 'May')
I'm not sure if this is the most efficient way, but you can do this with "union":
select order_ID, order_date from orders
where order_date Like '%Mar%'
union
select order_ID, order_date from orders
where order_date Like '%Apr%'
union
select order_ID, order_date from orders
where order_date Like '%May%'
EDIT: I prefer Otashin's answer.
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)