I have a table in which I have the following columns
FirstDate,
LastDate,
Grade_Category,
CR_Tones,
CR_grade
I created a query where this data is grouped by date and material type.
I need to see at the end of the grade column weighted average of the Grade with Tones.
Table is as below:
This is a query that I have designed and I have grouped movement date, grade category, etc the summed up tones, etc.
A query like below can be good starting point
select
lastdate as [date],
grade_category as [material type],
SUM(CR_grade*CR_Tones)/SUM(CR_Tones) as [weighted grade]
from table
group by lastdate, grade_category
Looks like you are looking for grouping sets
select
lastdate as [date],
grade_category as [material type],
SUM(CR_grade*CR_Tones)/SUM(CR_Tones) as weightedgrade --your formula for weighted grade
from yourtable
group by Grouping Sets (lastdate, grade_category)
Related
How do I query the following:
For each customer, product and month, count the number of sales transactions that were between the previous and the following month's average sales quantities. For January and December, display NULL or 0.
Can only use: 5 aggregate functions (sum, count, avg, max & min)
This is the table reference:
create table sales
(
cust varchar(20),
prod varchar(20),
day integer,
month integer,
year integer,
state char(2),
quant integer,
date date
);
Schema:
Example of my Desired Result
I am stuck with the following codes. I'm having a hard time how to execute it.
SELECT cust, prod, month, COUNT(*) AS SALES_COUNT_BETWEEN_AVGS
FROM sales
I use MySQL. Please guide me thank you.
Maybe try a query like below
the first part is to calculate averages using group by
second part is to use to those averages in a JOIN twice for past month and future month
third part is WHERE clause in which we compare data. Note we have used greatest and least functions to determine min and max between two values from past and next month
Query
WITH T AS
(SELECT cust, prod, month, AVG(quant) AS avg_quantity
FROM sales
group by cust, prod, month
)
SELECT S.cust, S.prod, S.month, COUNT(1) AS Sales_count
FROM sales S
LEFT JOIN T T1
ON T1.cust=S.Cust AND
T1.prod=S.Prod AND
T1.Month=S.Month-1
LEFT JOIN T T2
ON T2.cust=S.Cust AND
T2.prod=S.Prod AND
T2.Month=S.Month+1
WHERE S.quant BETWEEN IFNULL(LEAST(T1.avg_quantity,T2.avg_quantity),0) AND IFNULL(GREATEST(T1.avg_quantity,T2.avg_quantity),0)
I need to List top 10 customers by average monthly transactions amount turnover, in
additional column indicating the year and month with highest monthly turnover of the customer.
I made the first part - list top 10 customers by average monthly transactions amount turnover.
Select column1, AVG(Case when when column1="x" then column2
when column1="y" then column2
when column1="z" then column2
when column1="q" then column2 End)/12 [AVG]
from table1
Group by column1
Order by AVG DESC;
How to make the second part of the task - in
additional column indicating the year and month with highest monthly turnover of the customer?
This sounds like aggregation and window functions. Date functions are notoriously database-dependent, but the idea is:
select ymc.*
from (select year(r_date) as yyyy, month(r_date) as mm, c_id, sum(amount) as amount,
row_number() over (partition by c_id order by sum(amount) desc) as seqnum
from t
group by year(r_date), month(r_date), c_id
) ymc
where seqnum = 1;
Not all databases support year() and month() but all have similar functionality. So you may need to modify for what your database uses.
Use below query, below query in for Oracle, tag your database so that can provide appropriate query.
select c_id, to_char(r_date, 'MM'), to_char(r_date, 'YYYY'), max(amount)
group by c_id, to_char(r_date, 'MM'), to_char(r_date, 'YYYY');
In oracle sql, how to get the count of newly added customers only for the month of april and may and make sure they werent there in the previous months
SELECT CUSTOMER ID , COUNT(*)
FROM TABLE
WHERE DATE BETWEEN '1-APR-2018' AND '31-MAY-2018' AND ...
If we give max (date) and min(date), we can compare the greater date to check if this customer is new , correct?
expected output is month count
april ---
may ---
should show the exact count how many new customers joined in these two months
One approach is to use aggregation:
select customer_id, min(date) as min_date
from t
group by customer_id
having min(date) >= date '2018-04-01 and
min(date) < date '2018-06-01';
This gets the list of customers (which your query seems to be doing). To get the count, just use count(*) and make this a subquery.
I have a column Date with the same name in four tables Products, Customers, Shops, World in mysql. what i want to do is that i want to find the greatest date in the DATE COLUMN of all four tables. I don't want the greatest date of every table but the greatest one in all four tables. I hope you are understanding. Please help me
The most efficient way I can think of would be using the greatest function:
select greatest(
(select max(date) from products),
(select max(date) from customers),
(select max(date) from shops),
(select max(date) from world)
) greatest_date
SQLFiddle demo
This might work:
SELECT MAX(Date) as Max_Date
FROM (
SELECT Date FROM Products
UNION
SELECT Date FROM Customers
UNION
SELECT Date FROM Shops
UNION
SELECT Date FROM World
) AS P;
I am building a query in mysql 5.0 to calculate a student semester grade. The initial table (studentItemGrades) contains the list of assignments etc which will be used to calculate the final grade. Each assignment has a PossibleScore, Grade and Weight. The calculation should group all similarly weighted items, and provide the SUM(GRADE)/SUM(POSSIBLESCORE) based on a date range of when the assignment was due. The problem I am encountering is the final summation of all the individual weighted grades. For example, the results currently produce the following:
CourseScheduleID sDBID AssignedDate DueDate Weight WeightedGrade
1 519 2010-08-26 2010-08-30 10 0.0783333333333333
1 519 2010-09-01 2010-09-03 20 0.176
1 519 2010-09-01 2010-09-10 70 0.574
from the query:
SELECT CourseScheduleID, sDBID, AssignedDate, DueDate, Weight,
((SUM(Grade)/SUM(PossibleScore))*(Weight/100)) AS WeightedGrade
FROM studentItemGrades
WHERE DueDate>='2010-08-23'
AND DueDate<='2010-09-10'
AND CourseScheduleID=1
AND sDBID=519
AND Status>0
GROUP BY Weight
The question: How do I now SUM the three results in the WeighedGrade output? And by the way, this is part of a much larger query for calculating all grades for all courses on a particular campus.
Thanks in advance for your help.
You can use a subquery, like so:
SELECT SUM(WeightedGrade) FROM
(
SELECT CourseScheduleID, sDBID, AssignedDate, DueDate, Weight,
((SUM(Grade)/SUM(PossibleScore))*(Weight/100)) AS WeightedGrade
FROM studentItemGrades
WHERE DueDate>='2010-08-23'
AND DueDate<='2010-09-10'
AND CourseScheduleID=1
AND sDBID=519
AND Status>0
GROUP BY Weight
) t1
In order to sum the three results, you would need to requery the results of this select using another select with a group by. This could be done using a single sql statement by using subqueries.
SELECT sq.CourseScheduleID, sq.sDBID, SUM(sq.WeightedGrade) as FinalGrade
FROM
(
SELECT CourseScheduleID, sDBID, AssignedDate, DueDate, Weight,
((SUM(Grade)/SUM (PossibleScore))*(Weight/100)) AS WeightedGrade
FROM studentItemGrades WHERE DueDate>='2010-08-23' AND DueDate<='2010-09-10'
AND CourseScheduleID=1 AND sDBID=519 AND Status>0 GROUP BY Weight
) AS sq
GROUP BY sq.CourseScheduleID, sq.sDBID