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
Related
The following table structure is given:
year
team_ID
team
2011
1
Manchester
2011
2
Bayern
2011
3
Madrid
2012
1
Manchester
2012
2
Bayern
2012
4
Chelsea
The Objective is to select the teams that were in the table in the year 2011 but got kicked out for the year 2012.
In this case only Madrid has the year 2011 but not 2012, so it would be the only one selected.
The expected output is:
team
Madrid
Thank you for your help!
You should use a correlated exists, such as
select team
from t
where year=2011
and not exists (select * from t t2 where t2.year = 2012 and t2.team = t.team);
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 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
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();