How to do this query in MYSQL - mysql

Hi I have problem for create SQL. I have table and the data like this :
id month year id_type qty
1 10 2012 1 5
2 10 2012 2 4
3 10 2012 3 3
4 10 2012 4 5
5 11 2012 1 1
6 11 2012 2 2
7 12 2012 1 3
8 12 2012 2 2
I want create SQL for generating the data like this
id_type month year qty month_b4 year_b4 qty_b4
1 10 2012 5 9 2012 0
2 10 2012 4 9 2012 0
3 10 2012 3 9 2012 0
4 10 2012 5 9 2012 0
1 11 2012 1 10 2012 5
2 11 2012 2 10 2012 4
1 12 2012 1 11 2012 1
2 12 2012 2 11 2012 2

This query will do it:
select
sales.id_type as id_type,
sales.month as month,
sales.year as year,
sales.qty as qty,
prev.month as month_b4,
prev.year as year_b4,
prev.qty as qty_b4
from
sales,
sales as prev
where
sales.id_type = prev.id_type and
((sales.month=1 and prev.month=12 and prev.year=sales.year-1) or
prev.month = sales.month - 1);
I also have it on a fiddle where you can experiment with it:
http://sqlfiddle.com/#!2/ed3af/14

Related

SQL DATA BASE QUERY

City
Id Name
1 Delhi
2 Noida
3 Gurugram
Parameter
Id Name
1 Health
2 Education
3 Employment
Rating
Id Rating City_Id Param_Id Quarter Year Value_Date
1 7.5 1 1 Q1 2017 2017-02-15
2 6.3 1 1 Q1 2017 2017-02-13
3 6.9 1 1 Q1 2017 2017-02-20
4 8.2 1 1 Q2 2017 2017-04-05
5 5.5 1 1 Q2 2018 2017-12-13
6 7.6 1 1 Q3 2017 2017-08-20
7 4.5 2 1 Q1 2017 2017-02-17
8 5.3 2 1 Q1 2017 2017-02-14
9 6.9 2 1 Q1 2017 2017-02-25
10 7.2 2 1 Q2 2017 2017-08-05
11 8.5 2 1 Q2 2018 2017-12-13
12 9.6 2 1 Q3 2017 2017-08-20
13 3.5 3 1 Q2 2018 2017-12-14
14 4.6 3 1 Q4 2017 2017-08-17
15 5.5 3 1 Q2 2018 2017-12-20
16 7.6 3 1 Q3 2017 2017-08-15
17 7.5 3 1 Q2 2018 2017-12-18
18 8.6 3 1 Q3 2017 2017-08-24
19 7.5 1 2 Q1 2020 2018-05-25
20 6.3 2 2 Q3 2018 2018-17-13
21 6.9 3 3 Q2 2019 2019-06-20
I want to fetch the data from the Rating Table. I have a list of city ids, parameter ids, Quarter and Year. For Example city_id = [1,2,3], paramter_id = [1], quarter = Q3 and Year= 2017 then the output id from Rating tables should be 6, 12 and 18. 6 and 12 are fine but 18 is selected because if same quarter and year is present then we fetch the data using latest value_date.
Another case city_id = [1,2,3], paramter_id = [1], quarter = Q4 and Year= 2017 then the output id from Rating tables should be 5,11,14. 14 is fine but 5 and 11 is selected because if Quarter and year together are not present then we calculate the max date of queries period that shoub be 2017-12-31 (Q4 -2017) and apply less than equal to value_date (must be earliest)
Hmmm . . . I am thinking window functions. So filter for all rows whose year/quarter is less than or equal to the quarter you want. Then enumerate the rows backwards:
select r.*
from (select r.*,
row_number() over (partition by city_id, parameter_id order by year desc, quarter desc) as seqnum
from rating r
where city_id in (1, 2, 3) and parameter_id = 1 and
year <= 2017 and
(year < 2017 or quarter <= 'Q4')
) r
where (year = 2017 and quarter = 'Q4') or
seqnum = 1;
The filter chooses either the correct quarter or the first row.

MySQL query to get data dynamically based on financial year

I want to select fields dynamically based on financial year (i.e., for the year of Apr'18 to Mar'19).
I have data in a table exact as given below :
ROWNO YEAR MONTH
1 2016 1
2 2016 2
3 2016 6
4 2017 7
5 2017 5
6 2018 4
7 2018 5
8 2018 6
9 2018 7
10 2018 8
11 2018 9
12 2018 10
13 2018 11
14 2018 12
15 2019 1
16 2019 2
17 2019 3
18 2019 3
19 2017 4
20 2017 1
21 2017 2
22 2018 3
I want to get the result as shown below for the financial year 2018-19 are
ROWNO YEAR MONTH
6 2018 4
7 2018 5
8 2018 6
9 2018 7
10 2018 8
11 2018 9
12 2018 10
13 2018 11
14 2018 12
15 2019 1
16 2019 2
17 2019 3
I used Query
SELECT * FROM rup_calendar WHERE YEAR BETWEEN YEAR(CURDATE()) AND YEAR(CURDATE())+1;
but not able to get exact result.
Please give me the query to get the result as given above.
Given the data you have your approach is not far away
select rowno,year,month
from t
where year * 100 + month between
case when month(now()) < 4 then (year(now()) - 1) * 100 + 3
else year(now()) * 100 + 4
end
and
case when month(now()) < 4 then (year(now())) * 100 + 3
else (year(now()) + 1) * 100 + 4
end
Notice I have calulated a yearmonth field to ease comparison. Note I haven't fully tested this and you should do so by substituting an # variable for now() to test.

Ranking of the affiliates with most commissions from database

The table "commissions" is of this form:
affiliate referral amount date
3 2 15 2016-08-27
1 22 10 2016-08-29
4 45 5 2016-09-06
1 33 9 2016-09-08
3 17 4 2016-09-11
2 33 10 2016-09-16
1 9 7 2016-09-26
3 17 9 2016-09-26
2 69 10 2016-09-30
1 21 7 2016-10-01
4 55 2 2016-10-06
I need to find out the ranking of the affiliates with most commissions month to month.
I have tried:
SELECT affiliate, SUM(amount) as amount
FROM commissions
GROUP BY affiliate, YEAR(date), MONTH(date)
But unfortunately it is not throwing the desired results.
Please help.

aggregate on amount column by month - sql

I can't seem to figure out how to aggregate this table. I need to figure out from this table what is the amount aggregated by a month. So I have some projects and a duration per project, average income per month (for each project). For example, I would like to see what is the total amount for all project May 2011. Here is the original table:
Year Month Amount Duration (month) average per month
2012 1 7 4 1.75
2012 2 6 5 1.2
2012 3 5 6 0.833333333
2012 4 4 6 0.666666667
2012 5 9 5 1.8
2012 6 10 4 2.5
2012 7 20 3 6.666666667
2011 4 13 2 6.5
2011 3 3 10 0.3
2011 12 4 11 0.363636364
2011 2 5 12 0.416666667
2011 3 7 3 2.333333333
2010 5 8 4 2
2010 7 3 6 0.5
2010 9 4 7 0.571428571
2010 11 5 8 0.625
2010 1 6 8 0.75
2010 2 7 8 0.875
2010 3 8 9 0.888888889
2010 4 9 1 9
I would appreciate any help. Thanks.
Assuming year is varchar and month/duration int (if not, you may need to convert them as applicable) you can do something like this:
select sum(amount) from yourtable
where YearMonth between
period_add(year&'01',month-1) and period_add(year&'01',month+duration-2)
being YearMonth a string with the year/month to be queried, in your example would be '201105'
for a number of year/months you can create a one-column table:
create table yearmonths(yearmonth varchar(6));
insert into yearmonths values
('201101'),('201102'),(201103),
('201104'),('201105'),(201106)
and join it to your table:
Select yearmonth,sum(amount)
from yearmonths y
left join yourtable t
on(y.yearmonth between period_add(year&'01',month-1)and period_add(year&'01',month+duration-2)
group by yearmonth

Where are TFS Warehouse tables and how to access them directly?

I want to access warehouse of tfs to access the tables like
1)table that stores the code requests submitted,approved,rejected
2)table that stores which employee submitted the code or which team sumitted the code on
what date the code is submitted
3)table that stores th e bugs raised,bugs which are open,bugs closed,bugs priority(p0,p1,p2,p3) and some other details
i want the output in the form as below
date bugsClosed bugsOpen Reason
p0 p1 p2 p3 total p0 p1 p2 p3 total Active Fixed Postponed ByDesign ByRepro total
Jan 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
Feb 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
Mar 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
Apr 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
May 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
Jun 2 3 4 1 10 4 8 2 10 24 5 6 4 9 0 24
Thanks & Regards,
Srinivas P.
Start here... it will guide you to your answer.
http://msdn.microsoft.com/en-us/library/ms244696(v=vs.90).aspx
and here for a walk-through of the schema
http://msdn.microsoft.com/en-us/library/ms244711(v=VS.90).aspx
and here for writing custom reports on schema
http://msdn.microsoft.com/en-us/library/bb649552(v=VS.90).aspx