Hi
I want to get a run-time Median Age from my table within the selected timeframe. I am doing this through the next two steps:
1. myQuery is a query to get all the NOT NULL records from myTable:
SELECT myTable.myNumber
FROM myTable
WHERE myDate Between [FromDate] And [ToDate] AND myNumber is not NULL;
2. Get the Median myNumber from myQuery:
SELECT x.myNumber AS median
FROM [myQuery] AS x, [myQuery] AS y
GROUP BY x.myNumber
HAVING (((Sum(IIf(y.myNumber<=x.myNumber,1,0)))>=(Count(*)+1)/2) And ((Sum(IIf(y.myNumber>=x.myNumber,1,0)))>=(Count(*)+1)/2));
I run the queries, and found out that if the number of records in myQuery is odd, then it works fine. But when there is even number of records in myQuery, it returns NULL.
I am new in SQL programming. I got this piece of code from this forum. I can not figure out why the query does not work on even records.
Can anyone help me out? Thank you in advance.
I want to get a run-time Median Age from my table within the selected timeframe. I am doing this through the next two steps:
1. myQuery is a query to get all the NOT NULL records from myTable:
SELECT myTable.myNumber
FROM myTable
WHERE myDate Between [FromDate] And [ToDate] AND myNumber is not NULL;
2. Get the Median myNumber from myQuery:
SELECT x.myNumber AS median
FROM [myQuery] AS x, [myQuery] AS y
GROUP BY x.myNumber
HAVING (((Sum(IIf(y.myNumber<=x.myNumber,1,0)))>=(Count(*)+1)/2) And ((Sum(IIf(y.myNumber>=x.myNumber,1,0)))>=(Count(*)+1)/2));
I run the queries, and found out that if the number of records in myQuery is odd, then it works fine. But when there is even number of records in myQuery, it returns NULL.
I am new in SQL programming. I got this piece of code from this forum. I can not figure out why the query does not work on even records.
Can anyone help me out? Thank you in advance.