I have an SQL table that contains order data by date. I'm trying to combine the data across years in 4 weeks buckets so that I can compare year on year periods. Luckily the table contains year and week number columns so that I can sum the data to show order totals by week number, for example:
By using SELECT order_year, order_week_number, sum(order_total) from f2l4d1a2ta_237_floodlight_order_data_v1 group by order_week_number ORDER BY order_week_number, order_year
I get:
order_year order_week_number sum(order_total)
2017 1 96.40879041
2017 2 33.34092216
2017 3 97.79772267
2017 4 28.05668819
2017 5 75.79034382
2017 6 41.59171513
2017 7 3.754344347
2017 8 66.27940579
2016 1 65.81290635
2016 2 71.17703765
2016 3 65.95184929
2016 4 90.42108213
2016 5 44.32837015
2016 6 19.9644766
2016 7 53.46359297
2016 8 7.059479081
However what I'm really after is to see the order total for the 4 week period in the year, i.e.
order_year 4 week period sum(order_total)
2017 1 255.6041234
2017 2 187.4158091
2016 1 293.3628754
2016 2 124.8159188
Does anyone know how to group data with SQL in this way?
Thanks,
Matt
Add 3 to the week number then integer divide by 4 (whole number result)
Eg (1+3) DIV 4 = 1, (4+3) DIV 4 = 1
So GROUP BY (weekno + 3) DIV 4
Related
I need to add a new column to my table that will have the difference between sum of revenue of a new quarter over the last one.
My data looks like this:
Website Year Quarter Revenue
cosmo.com 2019 4 10
cosmo.com 2020 1 15
cosmo.com 2020 2 5
fashion.com 2019 4 10
fashion.com 2020 1 5
fashion.com 2020 2 20
The desired output is:
Website Year Quarter Revenue Difference
cosmo.com 2019 4 10 +5
cosmo.com 2020 1 15 +5
cosmo.com 2020 2 5 -10
fashion.com 2019 4 10 +10
fashion.com 2020 1 5 -5
fashion.com 2020 2 20 +15
I have tried to see the yearly difference, to begin with, but got a syntax error
select *,
`Revenue` - lag(`Revenue`) over(order by `Year`) as difference
from table`
From what I can tell, you want the difference from the previous quarter, not year. That would be:
select t.*,
(t.Revenue - lag(t.Revenue) over (partition by website order by Year, quarter)) as difference
from table t;
Note the use of partition by for the website.
I need to group sum in from previous period in SQL. Here's my data from simplified customer (customer A, B, and C)
Customer Year Month Order
A 2019 5 10
A 2019 4 5
A 2019 3 3
A 2019 2 1
A 2018 12 3
B 2019 5 1
B 2019 4 2
B 2019 3 1
C 2019 5 2
C 2019 4 1
C 2019 2 1
C 2019 1 3
Expected output is sum Order for previous period
For example, Expected output in first row is 12, because it sum in previous period (7 + 4 + 3 + 0) in customer A.
Another example, Expected output in second row is 7 because it sum in previous period (4 + 3 + 0) in customer A. So, table below is the expected output from table above
Customer Year Month Order Output
A 2019 5 10 12
A 2019 4 5 7
A 2019 3 3 4
A 2019 2 1 3
A 2018 12 3 0
B 2019 5 1 3
B 2019 4 2 1
B 2019 3 1 0
C 2019 5 2 5
C 2019 4 1 4
C 2019 2 1 3
C 2019 1 3 0
I need to do this in SQL
First of all, i would change the name of your columns (year, month and order are mysql reserved words or functions). You could do it with a subquery like this:
select customer,
`year`,
`month`,
`order`,
(select sum(output)
from yourtable b
where a.customer=b.customer and (a.year>b.year or (a.year=b.year and a.month>b.month)))
from yourtable a
What you do is you get every row and the last column is the sum of output for all other columns with the same customer and have year lower than the year in your customer or if the year is the same, the month is lower
In MySQL 8+, you should use window functions:
select t.*,
sum(output) over (partition by customer order by year, month)
from t;
Note only is this more concise, but it should have better performance as well.
This can be achieved by Window Functions. Below link provides the example
LAG([,offset[, default_value]]) OVER (
PARTITION BY expr,...
ORDER BY expr [ASC|DESC],...
)
http://www.mysqltutorial.org/mysql-window-functions/mysql-lag-function/
I got student_id=14 through form post and I need to fetch attendance report for the student_id as below
CLASS STUDENT_ID YEAR MONTH TOTAL_CLASSES TOTAL_PRESENT
11 14 2016 April 21 20
11 14 2016 May 25 25
11 14 2016 June 30 29
11 14 2016 July 18 18
11 14 2017 January 28 28
Here TOTAL_CLASSES represents total number days school is open and TOTAL_PRESENT represents total number of says a student is present out of TOTAL_CLASSES.
From HTML Form I GOT only student_id=14 and I need to fetch and show record as above.
Please see sqlfiddle here to support my answer http://sqlfiddle.com/#!9/63b6a/3
In my table remarks represents 1,2,3 for present and 0 for absent.
You're counting attendance numbers for each year, month, class and student. The key step in this query is to use GROUP BY to indicate the columns which you're grouping, then use the COUNT and SUM aggregation functions to compute the attendance columns you're looking for:
SELECT
class_id,
student_id,
YEAR(att_date) as year,
MONTH(att_date) as month,
COUNT(remarks) AS total_classes,
SUM(remarks > 0) AS total_present
FROM attendance
WHERE student_id = 15
GROUP BY YEAR(att_date), MONTH(att_date), class_id, student_id;
http://sqlfiddle.com/#!9/63b6a/11
You can remove the WHERE clause to show attendance for all students.
I am working on Educational project where i came across simple logic. I have two table month and semester_type. bellow are their schema and data;
month table
month_id month_name month_value lupdate
1 January 1
2 February 2
3 March 3
4 April 4
5 May 5
6 June 6
7 July 7
8 August 8
9 September 9
10 October 10
11 November 11
12 December 12
here is my semester_type table;
semester_type_id semester_type_name start_month end_month
1 Fall 8 12
2 Summer 1 4
and here is the output i want;
Semester Name Start Month End Month
Fall August December
Summer January April
i am confused with inner joining the month_id with start_month and end_month columns in both tables. can someone help me with codeigniter query
Join month's table twice with your semester table
select s.semester_type_name,
m.month_name start_month ,
m1.month_name end_month
from semester s
join month m on(m.month_id = s.start_month)
join month m1 on(m1.month_id = s.end_month)
Demo
Using codeigniter's active record library you can write it as
$this->db->select('s.semester_type_name,m.month_name start_month ,m1.month_name end_month')
->from('semester s')
->join('month m','m.month_id = s.start_month')
->join('month m1','m1.month_id = s.end_month')
->get()
->result();
I'm trying to make a summary report based off my database. It must be grouped by location by week by year and I want to total the amount of orders in that grouping.
It needs to look something like:
Year Week Location Total Amount
2014 1 Atlanta 22,000
2014 1 Schaumberg 32,566
2014 1 Dallas 32,567
2014 1 New York 32,356
2014 2 Atlanta 22,000
2014 2 Schaumberg 32,566
2014 2 Dallas 32,567
2014 2 New York 32,356
My table (system_order) structure is setup like this:
Order Amount Location Week Year
1 1895 Schaumberg 1 2014
2 1295 Atlanta 1 2014
3 1895 Atlanta 1 2014
4 1895 New York 1 2014
5 1495 Dallas 2 2014
6 1695 Schaumberg 2 2014
7 1895 Schaumberg 2 2014
8 1895 Dallas 2 2014
9 1895 New York 2 2014
Can this be done in one sql statement?
SELECT Year, Week, Location, sum(Amount) as 'Total Amount'
FROM [system_order]
GROUP BY Location, Week, Year
I have not testest. Bus this should work.
select sum(amount), year,week,location
from (system_order)
group by Location, week, year
order by sum(amount)
Usually you have to put the rows you selected into the group by clause. The exception is if they are in an aggregate function in the select clause.
This right here should work.
Select Year
,Week
,Location
,sum(amount) as 'Total_Amount'
from (system_order)
group by Year
,Week
,Location