sql server - How do I get a proper weighted average date in SQL -


I use this query to get my average date:

  convert (DATETIME, AVG Convert (Float, Chargeoffet)) [Average C / O Date]  

Give me the date of '2003-08-12 14: 17: 22.103' correctly.

When I use this query to get the weighted average date

  convert (DATETIME, SUM (converter (float, chargeoffet) * face value) / SUM (Face value))  

I get a date like '1938-10-19 21: 28: 48.000' which is far from the average date, I think it is a simple There is a thing and I'm missing something important but small.

Example:

  Announce the @temp table (value daytime, [weight] money) Enter in @temp values ​​(NULL, 8850.00) @temp values Insert (zero, 137.91) into @temp values ​​('2006-01-15', 221.13) into @temp values ​​('2006-10-15', 127.40) @temp values ​​('2001-07-31', 551.44) Insert in @temp values ​​('1997-10-12', 4963.41) Insert @temp values ​​('2006-03-15', 130.36) TEMP value ('2005-01-07', 1306.31) (Average date), convert (datat, sUM (converter (f) Lot [Value]] [Same]) / SUM ([Weight])) as [date of weighted average] by @temp  

This is an average date of '2003-11 -25 20: 00: 00.000 'and' 1 944-10-13 10: 52: 10.573 'without taps as a weighted average, it gives' 1 999-12-02 17: 10: 51.087' It is possible that my problem is that my calculations are faucet throwers.

Most probably, some of your chargeoffad 's NULL , but they do not have a face value .

These records do not contribute to the first amount but contribute as another. (Cast 'Cast' ('20030101' AS Date Time), 1.0 Select Union Cast ('20030201' AS Datetime), 2.0 Union All Cast as the q (Chargeoffdate, Face Value) with

Cast ('20030301' AS Date Time), 3.0 Union Select All, Null 4.0) Convert Convert (Detet, SM (Convert (Float, Chargeoffd) * Face Value) / SM (Face Valley) - 1961-11-12 Use this:

  with q (chargeoffet, face valley) AS (cast cast ('20030101' AS DATETEM), 1.0 c D 'All Select Cast (' 20030201 'AS Datetime), 2.0 Union All Select cast (' 20030301 'AS Datetime), 3.0 Union Select All, 4.0) Convert Convert (Detet, Sam (converter (float, chargeoffet) * Face value) / SM (Face Valley)) Where is the charge charge NULL ---- 2003-02-09 20: 00: 00.000  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -