# Median quantity by product by day_week for each month [Answered]RSS

## 3 replies

Last post Feb 17, 2016 05:46 AM by sandeepmittal11

Member

1 Points

12 Posts

### Median quantity by product by day_week for each month

Feb 16, 2016 01:23 PM|sellal|LINK

Hi everybody,

I'm back with new question

I have a daily sales I need to know how to calculate by SQL SEVER the median for each day_week of each month

the table is in the link   https://onedrive.live.com/redir?resid=8952CE5DAEF23B26!120&authkey=!AGTfzDXJkj6xvfQ&ithint=file%2cxlsx

Thank's a lot for helping me

Star

7990 Points

1586 Posts

### Re: Median quantity by product by day_week for each month

Feb 17, 2016 02:55 AM|Weibo Zhang|LINK

Hi sellal,

You could have a look at the following code and modify it according to your need. You could use Union All to get the two query results and then regard the final result as a new one to order.

```WITH CTE
AS
(
select offre, [Day_week1],[Day_week2],[Num_week],[month],[year],Quantity

from dbo.TempTest

UNION ALL

select offre, [Day_week1],NULL,NULL,NULL,NULL,AVG(Quantity) as 'Quantity'

from dbo.TempTest

group by [Day_week1],offre
)
SELECT * FROM CTE ORDER BY  offre, Day_week1, Day_week2 DESC
```

Best Regards,

Albert Zhang

Participant

1692 Points

760 Posts

### Re: Median quantity by product by day_week for each month

Feb 17, 2016 05:04 AM|TechView|LINK

Here is a link that shows how to deal with median in Sql server:

http://sqlperformance.com/2012/08/t-sql-queries/median

Hope helps.

Helping others is to provide help to yourself. Mark as answer, if the solution helps.

TechView

Contributor

5774 Points

1170 Posts

### Re: Median quantity by product by day_week for each month

Feb 17, 2016 05:46 AM|sandeepmittal11|LINK

Refer this post to understand how to calculate median in SQL Server

http://www.itdeveloperzone.com/2013/04/calculate-median-in-sql-server.html

Sandeep Mittal | Tech Blog : IT Developer Zone | Twitter : @itdeveloperzone | Facebook : @itdeveloperzone