UserId, SRC, DST, Mode, Dist, Year
1,CHN,IND,airplane,200,1990
2,IND,CHN,airplane,200,1991
3,IND,CHN,airplane,200,1992
4,RUS,IND,airplane,200,1990
5,CHN,RUS,airplane,200,1992
6,AUS,PAK,airplane,200,1991
I want to find the total distance traveled by a user in a particular year.
Ex: <Id> <Distance> <year>
1 200 1991
1 600 1993
2 400 1991
2 200 1992
How do I write query for this?
You need to add holidays.year in group by clause
select holidays.id, sum(holidays.distance) as totaldistance, holidays.year
from holidays
group by holidays.id ,holidays.year
order by holidays.id ASC
Your groupings are UserID and Year so:
SELECT UserID, SUM(Dist), Year
FROM table
GROUP BY UserID,Year
SELECT USERID, YEAR, SUM (DIST)
FROM TABLE_USER_DISTANCE
GROUP BY USERID, YEAR;
Related
I have a table that consists of the following data, I would like to know whether it is possible to get a Month (i.e Jan, Feb) wise count of Reservations that happened and also Month wise count for each location.
PNR
Location
Reservation Date
Passenger Name
Travel Date
PNR81239087
Mumbai
2019-10-01 12:19:00
Ram
2019-11-06 15:59:00
PNR81239090
Kerala
2019-10-01 15:18:00
Kannan
2019-12-03 19:18:00
PNR812390199
Mumbai
2019-10-01 17:19:00
Ram
2019-11-01 18:39:00
For example,
Month Wise Count (including all locations) should look something like this,
Month
Count
October-2019
3
Monthwise count for each location:
Month
Count
Location
October-2019
2
Mumbai
October-2019
1
Kerala
I think this will work for you
Month Wise Count (including all locations) :
select MONTHNAME(Reservation_Date) as Month, count(*)
from yourTable
group by MONTHNAME(Reservation_Date)
Monthwise count for each location :
select MONTHNAME(Reservation_Date) as Month, count(*), Location
from yourTable
group by MONTHNAME(Reservation_Date), Location
EDIT IN QUESTION :
Changed to Group by Year and Month in one column:
Month Wise Count (including all locations) :
select concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date)) as Month-Year,
count(*) as Count
from yourTable
group by concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date))
Monthwise count for each location :
select concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date)) as Month-Year,
count(*) as Count, Location
from yourTable
group by concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date)), Location
You can use this :
/*Location-wise*/
SELECT DATENAME(MONTH, ReservationDate) as [Month],
COUNT(*) AS Count,
Location
FROM Temp
GROUP BY DATENAME(MONTH, ReservationDate), Location
ORDER BY Location DESC
/*All locations*/
SELECT DATENAME(MONTH, ReservationDate) as [Month],
COUNT(*) AS Count
FROM Temp
GROUP BY DATENAME(MONTH, ReservationDate)
You can see this working here : All locations and Locations-wise
I am calculating weighted formula for a field as sum(revenue)/ Sum(qty) and this is as per the below query. Now I will be creating a view that would store these results as I shown in code below.
My question is, if I select this w_revenue out of the view and want to see per year, how will I aggregate to show it per year? See desired outputs.
select month,item, sum(revenue)/ Sum(qty) as w_revenue,
year
from my_revenue_table
group by month, year,item;
create view xyz as
select month,item, sum(revenue)/ Sum(qty) as w_revenue,
year
from my_revenue_table
group by month, year,item;
select w_revenue
from xyz.
How do I do this so as to aggregate this per year?
Year Month Item Revenue Qty Sum(revenue)/Sum(Qty)
2019 Mar A 10 2 5
2019 Mar B 30 3 10
2019 Feb C 50 1 50
2019 Feb D 20 2 10
Expected value if I see per year:
Year Sum(revenue)/Sum(Qty)
2019 13.75 (10+30+50+20)/(2+3+1+2)
group on by year then create another view as
create view abc as
select sum(revenue)/ Sum(qty) as w_revenue, year
from my_revenue_table
group by year;
and call
select * from xyz union all
select null, null, a.* from abc a
to combine them, if I understood you correctly.
or revenue per year repeating at every row as
select x.*,
(select w_revenue
from abc
where year = x.year) as w_revenue_year
from xyz x
You cannot do the aggregation you want from the view. You don't have enough information.
If you change the view to something like this:
create view xyz as
select year, month, item,
sum(revenue)/ Sum(qty) as w_revenue,
sum(qty) as total_qty
from my_revenue_table
group by month, year, item;
Then you can aggregate the results as:
select year, sum(w_revenue * total_qty) / sum(total_qty)
from xyz
group by year;
EDIT:
Or just modify the view to have the information you want:
create view xyz as
select year, month, item,
sum(revenue)/ Sum(qty) as w_revenue,
sum(qty) as total_qty,
(sum(sum(revenue)) over (partition by year, item) /
sum(sum(qty)) over (partition by year, item)
) as w_revenue_year
from my_revenue_table
group by month, year, item;
I want to get a list of mysql results for each week beginning at July 2015, showing the SUM or new users from my table user GROUPED BY Week. Is this possible?
So as result:
CW25/15: 100
CW26/15: 70
CW27/15: 180
....
How to do?
Try this:
SELECT CONCAT('CW',WEEK(date_col),'/',YEAR(date_col)) as week,
COUNT(*) as count
FROM table_name
GROUP BY YEAR(date_col),WEEK(date_col)
ORDER BY date_col
You can do it like this(You didn't post your table structures so you will have to adjust it) :
SELECT concat('CW',week(DateColumn),'/',year(DateColumn)) as weekDate,
count(*) as cnt
FROM YourTable
GROUP BY concat('CW',week(DateColumn),'/',year(DateColumn))
ORDER BY year(DateColumn),week(DateColumn)
I want to create two queries for my table which has fields name,surname and amount paid,the first query should select the day,month and the amount paid,the second query should select a month,year in that year and the total amount paid in that month,lets say john paid on 2013-05-01, on 2013-05-03,while peter paid on 2013-04-08, i want the first query to output
month and day amount
05-01 200
05-03 400
04-08 50
and the second query should output:
month and year total
2013-05 600
2013-04 50
I know I can use the sum aggregate function to select the total but the tricky part is how to select the day and the month in the format above,
first query will be
SELECT DATE_FORMAT(date, "%m-%d") AS 'month and day',price as amount FROM `tablename`
and second query will be
SELECT DATE_FORMAT(date, "%Y-%m") AS 'month and year' , SUM(price) AS total FROM `tablename` GROUP BY YEAR(date), MONTH(date)
I have a table storing weatherdata in 5min intervals.
Now I need result with total raindays grouped by Year like this:.
2010 100 raindays
2011 120 raindays
2013 90 raindays
This is my current query:
SELECT date, SUM(rain) FROM $tableName GROUP BY date HAVING SUM(rain) > 0";
as expected, this gives me following result:
2010-01-02 1.2 (mm)
2010-01-05 1.6 (mm)
2010-02-10 2.6 (mm)
How I have to change my query, to have this grouped by year(date) and counted days ?
Thanks all for your help
PM
You can group by YEAR(date), to sum the rain grouped by year. Then to count the number of days you can COUNT DISTINCT the days without the time part, using DATE(date) function.
SELECT YEAR(date), SUM(rain), COUNT(DISTINCT DATE(date)) days
FROM $tableName
WHERE rain>0
GROUP BY YEAR(date)
Please see fiddle here.
The following query might help you if you want to find maximum element is each group,
select
e.date, e.rain
from
TableName e
join
(select
date, MAX(rain) MS
from
TableName
group by date
Having rain > 0) m ON e.date = M.date and e.rain = m.rain