I was going through this forum for my query to get data for previous 7 days,but most of them give it for current date.Below is my requirement:

I have a **Table 1** as below:

These are start dates of week which is monday

```
from_date
2016-01-04
2016-01-11
2016-01-18

```

**Table 2**

I have all days of week here starting from monday.Ex: jan 04 - monday to jan 10 - sunday and so on for other weeks also.

```
get_date    flag    value
2016-01-04  N   4
2016-01-05  N   9
2016-01-06  Y   2
2016-01-07  Y   13
2016-01-08  Y   7
2016-01-09  Y   8
2016-01-10  Y   8
2016-01-11  Y   1
2016-01-12  Y   9
2016-01-13  N   8
2016-01-14  N   24
2016-01-15  N   8
2016-01-16  Y   4
2016-01-17  Y   5
2016-01-18  Y   9
2016-01-19  Y   2
2016-01-20  Y   8
2016-01-21  Y   4
2016-01-22  N   9
2016-01-23  N   87
2016-01-24  Y   3

```

**Expected Result**
here wk is the unique number for each start-end dates respectively
avg value is the avg of the values for the dates in that week.

last 2 days of the week are weekend days.

say 2016-01-09 and 2016-01-10 are weekends

```
from_date   get_date    Wk  Total_days  Total_weekdays_flag_Y   Total_weekenddays_flag_Y    Avg_value
2016-01-04  2016-01-10  1   7   3   2   6.714285714
2016-01-11  2016-01-17  2   7   2   2   8.428571429
2016-01-18  2016-01-24  3   7   4   1   17.42857143

```

Could anyone help me with this as I am not good at sql.

Thanks