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.
Related
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.
i have a table
id year month
--------------
1 2015 4
2 2015 4
1 2015 5
1 2015 6
2 2015 6
3 2015 6
and so on
1 2016 3
2 2016 3
3 2016 3
4 2016 3
Now i want all distinct id's from year-month 2015-4 to 2016-3.
i tried between but didn't get desired result.
any help???
You can use this query:
SELECT id from temp where str_to_date(concat(year,'-',month,'-',1),'%Y-%m-
%d') between str_to_date('2015-4-1','%Y-%m-%d') and str_to_date('2016-3-31','%Y
-%m-%d');
wich converts your values to dates.
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
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
I'm looking to get a list of the first and last business days of the month.
Its basically a list of business days:
2009-01-03
2009-01-04
2009-01-05
...
I just want to get a list of the first and last days, basically and max and min day(date) for each year-month combination.
Any suggestions?
Your question states that you already have a list of business days and that you need a way of finding the minimum and maximum for each year-month combination.
You can use ddply in package plyr to do this. I also make use of package lubridate because it has some convenience functions to extract the year and month from a date.
Create some data:
library(lubridate)
x <- sample(seq(as.Date("2011-01-01"), by="1 day", length.out=365), 100)
df <- data.frame(date=x, year=year(x), month=month(x))
Now extract the min and max for each month:
library(plyr)
ddply(df, .(year, month), summarize, first=min(date), last=max(date))
year month first last
1 2011 1 2011-01-03 2011-01-30
2 2011 2 2011-02-03 2011-02-19
3 2011 3 2011-03-06 2011-03-29
4 2011 4 2011-04-09 2011-04-30
5 2011 5 2011-05-01 2011-05-29
6 2011 6 2011-06-04 2011-06-28
7 2011 7 2011-07-02 2011-07-29
8 2011 8 2011-08-10 2011-08-30
9 2011 9 2011-09-01 2011-09-28
10 2011 10 2011-10-07 2011-10-31
11 2011 11 2011-11-01 2011-11-28
12 2011 12 2011-12-01 2011-12-30