I have a table in mysql. How to write the query to find the phone name that has got highest sales(highest number of Sold_out in each year)
--------------------------------------------
Phone Sold_out Month Year
--------------------------------------------
iphone 3 Jan-15 2015
iphone 10 Feb-15 2015
samsung 4 March-15 2015
Lava 14 June-16 2016
Lenova 8 July-16 2016
Lenova 10 Sep-16 2016
Motorola 8 Jan-17 2017
Nokia 7 Jan-17 2017
Nokia 3 Feb-17 2017
--------------------------------------------
The result I need is
-----------------------------
year Phone sales
-----------------------------
2015 iphone 13
2016 lenova 18
2017 Nokia 10
-----------------------------
In the derived table the total sales are calculated at ( year,phone ) combination. Once the total sales are calculated all the top rows( rank = 1 by sales ) should be identified for each year. By using correlated sub-queries and having clause the first row is identified from each group( year ) and displayed as final output.
SELECT year,phone, sales_per_year
FROM
(
SELECT year,phone,SUM(sold_out) AS sales_per_year
FROM sales
GROUP BY year,phone
) o
GROUP BY year,phone
HAVING (
SELECT COUNT(*)
FROM
(
SELECT year,phone,SUM(sold_out) AS sales_per_year
FROM sales
GROUP BY year,phone
) i
WHERE i.year = o.year
AND i.sales_per_year > o.sales_per_year
) < 1
ORDER BY year,phone,sales_per_year DESC
Check the sql fiddle link
http://sqlfiddle.com/#!9/ff096e/14
Related
I have recently started working with SQL and I am trying to write a query that will display all cities that has the highest volume and listing on a yearly basis.
this query is able to handle the year 2011 alone
select city, year, month,max(volume),max(listings)
from kaydata
where year = 2011
result
city year month max(volume) max(listings)
0 Abilene 2011 2 6505000.0 746.0
sample data
city year month volume listings
0 modak 2011 1 5380000.0 701.0
1 Abilene 2011 2 6505000.0 746.0
2 ipetu 2010 3 9285000.0 784.0
2 oyog 2010 4 7085000.0 204.0
desired result
city year month max(volume) max(listings)
0 Abilene 2011 2 6505000.0 746.0
1 ipetu 2010 3 9285000.0 784.0
If I understand correctly, this would achieve what you're after:
select city,year,month,volume,maxlisting from (
select * , row_number() over (partition by year order by volume desc) rn
, max(listing) over (partition by year) maxlisting
from kaydata
) t
where rn = 1
I have these data
idhouse year
7 2016
2 2018
2 2017
3 2017
4 2015
14 2003
3 2018
5 2018
4 2017
4 2018
I want to counting the number of houses belong to two years.
I tried with mysql select but didn't work.
How I should do it?
EDITED
Sorry for my bad explanation.
I have only one mysql table.
Filtering by 2017 and 2018 and count the numbrer these houses, I should get these match:
idhouse year
7 2016
2 2018*
2 2017*
3 2017*
4 2015
14 2003
3 2018*
5 2018
4 2017*
4 2018*
And the SELECT should be show 3
I assume a house can only appear once in a year. Try this:
SELECT
COUNT(*) nb_houses
FROM (SELECT house
FROM yourTable
GROUP BY house
HAVING COUNT(*)>1) A;
See this run on SQL Fiddle.
Assuming a PK on (house,year), if you just want to know how many houses are listed more than once, you can do this...
SELECT COUNT(DISTINCT x.house) total
FROM my_table x
JOIN my_table y
ON y.house = x.house
AND y.year <> x.year;
I asuming you're doing a web app with this question
SELECT house, COUNT(year) as count_year FROM table GROUP BY house HAVING COUNT(year) = 2
By using your data above, the result will be
house | count_year
____2 | _______2
Then if you are using server side scripting like PHP use mysqli_num_rows for get the number of row(s).
Or if you use other language, just adjust the algorithm to get the number of row(s)
With this Select I get it:
select count(*) from (select house from yourTable Where year = 2018 and house in (select house from yourTable where year = '2017')) A;
But can we improve this Select in terms of efficiency?
You can try here
Thanks.
Im trying to select only ids of customers that have ordered atleast once every year in a specific time period for example 2010 - 2017
example:
1. customer ordered in 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 should be shown
2. customer ordered in 2010, 2011, 2012,2013,2014,2015, 2017 should not be shown
my query counts in all years not within the period
o_id o_c_id o_type o_date
1345 13 TA 2015-01-01
7499 13 TA 2015-01-16
7521 14 GA 2015-01-08
7566 14 TA 2016-01-24
7654 16 FB 2016-01-28
c_id c_name c_email
13 Anderson example#gmail.com
14 Pegasus example#gmail.com
15 Miguel example#gmail.com
16 Megan example#gmail.com
my query:
select c.id, c.name, count(*) as counts, year(o.date)
from orders o
join customer c on o.c_id=c.id
where year(o.date) > 2009
group oy c.id
having count(*) > 7
You need a table with all the years so you can check if user order that year. I create a sample with only two years because that is what in your sample data.
You can use this to create a list of years:
How to get list of dates between two dates in mysql select query
Also I use ranges for years so you can use index at the moment of the join.
If you already have a table users you can replace the subquery
SQL DEMO
SELECT user_id, COUNT(o_id) as total_years
FROM years y
CROSS JOIN (SELECT DISTINCT `o_c_id` as `user_id` FROM `orders`) as users
LEFT JOIN orders o
ON o.`o_date` >= y.`year_begin`
AND o.`o_date` < y.`year_end`
AND o.`o_c_id` = `user_id`
GROUP BY user_id
HAVING total_years = (SELECT COUNT(*) FROM years)
;
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 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();