How to sum duplicated values in SQL [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
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)

Related

calculate upcoming category based on the dates of the past categories [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 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;

How can I Sum Column Data according to date and then subtract this data from a column of another table on basis if same date of both tables [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
How can I Sum Column Data according to date and then subtract this data from a column of another table on the basis of the same date of both tables?
I am using mySql language
I have two tables and I want to subtract particle columns on basis of their dates. like My First Column Name is Sales having Columns Date Total.
The Second Table Name is Expenses and it has Columns Date and Amount
I want to Sum all the rows of both tables according to date(as one date can be repeated) using group by on both tables and then from the result of Group By used in both these tables, I want to subtract them on basis of same date on both tables and I want to show the result with Date after subtraction.
You can use sub-queries and join with group by as follows:
Select s.date, s.total - e.amount as result
from
(Select date, sum(total) as total
From sales group by date) s
Join
(Select date, sum(amount) as amount
From expenses group by date) as e
On e.date = s.date

how to find the sum of the salaries in every dep in every year? [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 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

MySQL - Month SELECT command [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 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 ..

Mysql find names not in previous dates [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
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'
);