Solved

Query syntax

Posted on 2014-10-14
3
80 Views
Last Modified: 2014-10-28
Hello,

Can someone explain the different between the two lines of code marked below in terms of the way the grouping is:


with cte as (
 select 
  -1 as kli_kliniknr,
  max(m.MeasurementDate) as MeasurementDate, 
  SUM(NumberOfMeasurements) as NumberOfMeasurements,
  SUM(NumberOfMen) as NumberOfMen,
  SUM(NumberOfWomen) as NumberOfWomen,

  -- What is the difference between these two rows?
  avg( Q1 * NumberOfMeasurements / NumberOfMeasurements) as Q1,
  sum( Q1 * NumberOfMeasurements) / sum(NumberOfMeasurements) as Q1x,

from [BPSD].[dbo].[MeasurementStatisticsPerUnit] m
group by m.MeasurementDate )
 
select * from cte ;

Open in new window

0
Comment
Question by:soozh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 167 total points
ID: 40380750
-- What is the difference between these two rows?
  avg( Q1 * NumberOfMeasurements / NumberOfMeasurements) as Q1,
  sum( Q1 * NumberOfMeasurements) / sum(NumberOfMeasurements) as Q1x,

Row 1 is AVERAGING
Row2 is SUMING

Row1  AVERAGE (Q1 * number of measurements / number of mesaurements
Row2  (its adding all together)  SUM(q1 * numberof measurements / SUM(numberof measurements)
0
 
LVL 48

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 166 total points
ID: 40380788
The AVG line is simply taking the average of the values of Q1.  The NumberOfMeasurements/Number of Measurements aspect of that element cancel each other out.

The Sum line looks like it is actually taking what most analysts would call the "weighted average".  This is determined by taking the sum of a value times the number of occurances of that value, and then dividing by the total number of measurements (the Sum(NumberOfMeasurements) figure).  So, if your table or query contains data like:

Q1     NumOfMeas
5                2
6                3
7               4

The first line would compute to the average of three elements)
AVG(5*2/2 , 6*3/3, 7*4/4) = 6

But the second method would compute as:
SUM(5*2, 6*3, 7*4)/Sum(2, 3, 4) = (10+18+28)/9 = 56/9 = 6.2222.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 167 total points
ID: 40380924
The first one is a simple average of Q1 ["* NumberOfMeasurements / NumberOfMeasurements" will always cancel each other out]

The second one, as Dale noted, is a different type of average, and could yield results quite different from the simple average.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Merge join vs exist 3 37
T-SQL: Please describe what a page split is 5 58
Oracle Date 6 42
Tracking Problematic Page Splits 1 50
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question