- Mar 1, 2000
- 30,890
- 5,001
- 126
This may be overly complicated to explain here, but I will give it a shot.
I have the following query that will get me the median days between MASTER.DATE_RECD and INFINITY.FIELD group by the month of MASTER.DATE_RECD where MASTER.USER = 2 for the last 12 months
However, what I am wanting to really get is for ALL users, group by those users. There are about 90 total different users, and each should return 12 months. I just have the "...and master.USER = '2'" in there now so I can pull some data. Obviously, that would have to be removed to pull all the USER information.
No matter what "group by" I toss in, it doesn't work. I get like 20 rows back for random USERs and DATE_RECD months.
I'm guessing there has to be some additional partitioning in there, but I have no idea where at this point and my mind is mush looking at it.
ANY help would be appreciated.
I have the following query that will get me the median days between MASTER.DATE_RECD and INFINITY.FIELD group by the month of MASTER.DATE_RECD where MASTER.USER = 2 for the last 12 months
However, what I am wanting to really get is for ALL users, group by those users. There are about 90 total different users, and each should return 12 months. I just have the "...and master.USER = '2'" in there now so I can pull some data. Obviously, that would have to be removed to pull all the USER information.
No matter what "group by" I toss in, it doesn't work. I get like 20 rows back for random USERs and DATE_RECD months.
I'm guessing there has to be some additional partitioning in there, but I have no idea where at this point and my mind is mush looking at it.
ANY help would be appreciated.
Code:
SELECT DATE, x.USER,
AVG(No_Days) As Median_Days_to_SUIT
From
(
select CAST(CAST(MONTH(MASTER.DATE_RECD) AS varchar(2)) +'/01/' + CAST(YEAR(MASTER.DATE_RECD) AS varchar(4)) AS DATE) As Date,
master.USER,
DATEDIFF(DAY,MASTER.DATE_RECD,INFINITY.FIELD) as No_Days,
ROW_NUMBER() over (partition by CAST(CAST(MONTH(MASTER.DATE_RECD) AS varchar(2)) +'/01/' + CAST(YEAR(MASTER.DATE_RECD) AS varchar(4)) AS DATE) order by DATEDIFF(DAY,MASTER.DATE_RECD,INFINITY.FIELD) ASC) as DaysRank,
COUNT(*) over (partition by CAST(CAST(MONTH(MASTER.DATE_RECD) AS varchar(2)) +'/01/' + CAST(YEAR(MASTER.DATE_RECD) AS varchar(4)) AS DATE)) as EntryCount
from MASTER INNER JOIN INFINITY on master.FILENO = INFINITY.FILENO
WHERE INFINITY.CODE = '*LglDt'
AND DATEDIFF(MONTH,MASTER.DATE_RECD,GETDATE()) <= 11
and master.USER = '2'
) x
WHERE
x.DaysRank in (x.EntryCount/2+1, (x.EntryCount+1)/2)
group by
x.USER, x.DATE
order by DATE
