473,417 Members | 1,626 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

Calculate Median with 2 group by columns

1
I am trying to calculate the median for values on a daily basis for multiple categories. I am able to do it for either a certain date for all categories or for a date range for one category, but cannot get it to break them both down. I am using the Count and RowNum calculations in my query to calculate the median values.
I have thousands of values per day for each category, so even creating a temp table by each category is not something I want to have to do if possible.
Sample Data
Category Date Value
1 5/23/2010 3
1 5/23/2010 6
1 5/23/2010 5
1 5/22/2010 8
1 5/21/2010 2
2 5/23/2010 5
2 5/22/2010 9
2 5/22/2010 4
2 5/21/2010 4

I would like the output grouped by Category and Date. Something like this.
Category Date Median
1 5/23 5
1 5/22 8
1 5/21 2
2 5/23 5
2 5/22 6.5
2 5/21 4

Here is a sample query that works for each separate one, but won’t produce the output when both are included.

Expand|Select|Wrap|Line Numbers
  1. SELECT X.Category, X.Date, avg(X.Value) FROM
  2. (SELECT A.Category, D.Date, p.Value,
  3.            ROW_NUMBER() OVER(PARTITION BY A.Category ORDER BY p.Value) AS RowNum,
  4.            COUNT(*) OVER(PARTITION BY A.Category) AS Cnt
  5. FROM TABLE1 AS A INNER JOIN
  6.      TABLE2 AS D ON A.ID = D.ID
  7. WHERE D.Date between '20100510' and '20100520' 
  8. AND A.OBJECTID = 135
  9. and A.Category in(‘XXX’ , ‘YYY’)) AS X
  10. WHERE RowNum IN ((Cnt+1)/2, (Cnt+2)/2)
  11. group by Category, Date
  12.  
If I remove Category from the select and group by statements and explicitly pick one value in the where clause it works fine and vice versa for Date, but when both are included it does not work. The query returns data, but does not include all the permutations grouped by both values.

Any ideas?
May 25 '10 #1

✓ answered by deepuv04

@bupah
hi,
use the date column for partitioning
try this hope will work
Expand|Select|Wrap|Line Numbers
  1. SELECT X.Category, X.Date, avg(X.Value) FROM
  2. (    SELECT    A.Category, D.Date, p.Value,
  3.             ROW_NUMBER() OVER(PARTITION BY A.Category,D.DATE ORDER BY p.Value) AS RowNum,
  4.             COUNT(*) OVER(PARTITION BY A.Category,D.DATE) AS Cnt
  5.     FROM    TABLE1 AS A INNER JOIN
  6.             TABLE2 AS D ON A.ID = D.ID
  7.     WHERE    D.Date between '20100510' and '20100520' 
  8.             AND A.OBJECTID = 135
  9.             and A.Category in(‘XXX’ , ‘YYY’)
  10. ) AS X
  11. WHERE RowNum IN ((Cnt+1)/2, (Cnt+2)/2)
  12. group by Category, Date
  13.  
remove where clause for date range and category, or change it according

1 4175
deepuv04
227 Expert 100+
@bupah
hi,
use the date column for partitioning
try this hope will work
Expand|Select|Wrap|Line Numbers
  1. SELECT X.Category, X.Date, avg(X.Value) FROM
  2. (    SELECT    A.Category, D.Date, p.Value,
  3.             ROW_NUMBER() OVER(PARTITION BY A.Category,D.DATE ORDER BY p.Value) AS RowNum,
  4.             COUNT(*) OVER(PARTITION BY A.Category,D.DATE) AS Cnt
  5.     FROM    TABLE1 AS A INNER JOIN
  6.             TABLE2 AS D ON A.ID = D.ID
  7.     WHERE    D.Date between '20100510' and '20100520' 
  8.             AND A.OBJECTID = 135
  9.             and A.Category in(‘XXX’ , ‘YYY’)
  10. ) AS X
  11. WHERE RowNum IN ((Cnt+1)/2, (Cnt+2)/2)
  12. group by Category, Date
  13.  
remove where clause for date range and category, or change it according
May 26 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: JP SIngh | last post by:
Hi All I need to calculate the number of working days between the two dates entered on an ASP page. I am not that great a coder in ASP and was wondering if someone can help. Basically the...
1
by: AJ | last post by:
Hi, I have a view in which I have 3 cols...(pno,ptno,diff)..diff is the difference in time in minutes.I want to calculate Median(diff) group by pno,ptno...using a sql query for SQL server... ...
8
by: nick.vitone | last post by:
Hi, I'm somewhat of a novice at Access, and I have no experience programming whatsoever. I'm attempting to calculate the statistical median in a query. I need to "Group by" one column and find...
2
by: niv | last post by:
Hello, I have a grid that consists of 6 columns and I want 3 of the columns to have a single header and the other 3 columns to have another header... Is this possible? How? Thanks, niv
73
by: brady | last post by:
I have a function called int calcMedian(int ar, int numElements) How do i code the function to calculate the median in the array? Please help
1
by: CrostonScottish | last post by:
Has anybody got any ideas or nifty code for calculating the median value in a form. I currently have a database which we use for post-course evaluations. Part of the evaluation asks the attendees...
6
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
2
by: dmorand | last post by:
Does anyone have any experience with retrieving a median value using SQL server? I'm trying to retrieve a median value from a list of various numerical values. Any help would be appreciated.
0
by: MrArjP | last post by:
So far, I have been researching and Microsoft.Office.Excel namespace is used alot, unfortunately with my work I have been ordered to use infragistics and so far I have had no luck with finding any...
4
by: MrDeej | last post by:
Hello! I have a table wich contains product number and different time date attachet pr product number. Today we have used to calculate MEAN to get an average time consumed pr product. We have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.