Brand name whose amount is increasing every year - mysql

I have table with three columns year, brand, amount and I want a query that return records of brand whose amount is increasing for every year.
I have pasted below the sample input data and expected output data.
table format:
year_v
Brand
Amount
2018
P&G
50000
2019
P&G
45000
2020
P&G
60000
2018
IBM
55000
2019
IBM
60000
2020
IBM
70000
2018
EY
80000
2019
EY
40000
2020
EY
36000
Expected output:
year_v
Brand
Amount
2018
IBM
55000
2019
IBM
60000
2020
IBM
70000
As for IBM, amount is increasing every year.

SELECT *
FROM brands
WHERE brand in
(SELECT brand
FROM
(SELECT brand,
(amount - lag(amount, 1, 0) OVER (PARTITION BY brand
ORDER BY year_v)) diff
FROM brands) t
GROUP BY brand
HAVING min(diff) > 0) ;
lag(): LAG (scalar_expression [,offset] [,default]) OVER ( [
partition_by_clause ] order_by_clause )
There may be scope for refactoring this query, but I will leave it to the OP to decide.
Demo

Related

issues writing simple sql queries

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

SQL getting records for the last quarter with 30 days grace period

I want to find the company records which has not done the filing for the last quarter, but i want to give grace period of 30 days. As in till Oct 30 2018, the company_seq should not be fetched. I am not able to do the grace period part. Can someone please help me?
And more thing to consider is, if the filing is not done for the previous quarter i.e. quarter 2, then the company sequence should be fetched
Sample Data
Company_seq YEAR QUARTER
1 2018 2
1 2018 3
2 2018 2
2 2018 1
3 2018 1
Company_SEQ COMPANY_NAME
1 Company 1
2 Company2
3 Company3
SELECT * FROM COMPANY
WHERE company_seq NOT IN
(SELECT DISTINCT company_seq
FROM Filing F
WHERE YEAR = (SELECT TO_CHAR(add_months(sysdate,-3),'YYYY') from dual)
AND quarter=(SELECT TO_CHAR(add_months(sysdate,-3),'Q') from dual)
Expected Query output:
3, Company3
I tried the below query and it did work
SELECT * FROM Temp_Company_Neha
WHERE company_seq NOT IN(
SELECT DISTINCT company_seq FROM Temp_Neha dehp
WHERE YEAR = (SELECT TO_CHAR(add_months(sysdate,-3),'YYYY') from dual)
AND quarter=(SELECT TO_CHAR(add_months(sysdate - 30,-3),'Q') from dual));
I'm thinking something like this:
select c.company_seq
from company c
group by c.company_seq
having add_months(max(add_months(to_date(year || '0101', 'YYYYMMDD'), 3 * c.quarter)), 1) < sysdate
The inner add_months() converts the year/quarter to the start of the next quarter. The outer add_months() is the grace period.

How to calculate the rank in mysql

I need to calculate the rank in mysql. Suppose I have list of sum of my product sales values of entire month then i need to rank the product from highest sales value in order to rank like 1 ,2 ,3 etc
Month Product Sum of Sales
Jan Latop 450000
jan Latop 150000
Jan Latop 250000
Feb Desktop 200000
Feb Desktop 150000
Feb Desktop 180000
so from above data output will be like
Month Product Sum of Sales rank
Jan Latop 450000 1
Jan Latop 250000 2
jan Latop 150000 3
Feb Desktop 200000 1
Feb Desktop 180000 2
Feb Desktop 150000 3
You can do something like this:
SELECT month,product,sumOfSales, #curRank := #curRank + 1 AS rank
FROM products p, (
SELECT #curRank := 0
) q
ORDER BY sumOfSales DESC;
I am assuming the table name is product and column name is sumOfSales.
You Can use this Query
SELECT sales FROM TABLE Order by sales DESC
sales is your column name where sum of sale is stored
Query Will Return Record with most sales first and so on.

Which phone has got high sales in each year?

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

how can i show sql header columns in rows in sql server

i want to show these records column wise for particular month and year, like below table format
Source Total
Organic 1252
Paid 121
Email Campaign 121
Total 1494
select Organic,Paid ,EmailCampaign ,Total from tbl_leads where Month='Aug' and Year='2015'
below is sample date
Organic Paid EmailCampaign Total ProjectName Month Year
4444 5555 2222 1111 demo project Feb 2015
1252 121 121 1494 debug test Aug 2015
In Sql Server you can use Cross Apply with Tabled Valued Constructor to unpivot the data
SELECT cs.Source,
cs.Total
FROM tbl_leads
CROSS apply (VALUES ('Organic',Organic),
('Paid',Paid),
('EmailCampaign',EmailCampaign),
('Total',Total)) cs(Source, Total)
WHERE Month = 'Aug'
AND Year = '2015'
Or Generic Sql solution
SELECT 'Organic' AS Source,
Organic AS Total
FROM tbl_leads
UNION ALL
SELECT 'Paid',
Paid
FROM tbl_leads
UNION ALL
SELECT 'EmailCampaign',
EmailCampaign
FROM tbl_leads
UNION ALL
SELECT 'Total',
Total
FROM tbl_leads