Solved

SQL: AVG of an alias

Posted on 2014-10-02
2
166 Views
Last Modified: 2014-10-13
Hi,

We want to compute the average of the djsresult field and save that as a new column.

we're using:

,Case WHEN (txtResultName IS NULL OR txtResultName = 'Average Point Score') THEN intGradeTransposeValue ELSE txtresult END as djsresult

How do I then calculate an Average of the djsresult field and include it in the results table as a column.

Full code attached
icode.txt
0
Comment
Question by:itmtsn
[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
2 Comments
 
LVL 20

Assisted Solution

by:dsacker
dsacker earned 250 total points
ID: 40357462
You have to wrap it with the AVG function:
AVG(Case WHEN (txtResultName IS NULL OR txtResultName = 'Average Point Score') THEN intGradeTransposeValue ELSE txtresult END as djsresult)

Open in new window

Of course, you'll have to then group all of your other selected fields in a GROUP BY.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 40357489
You'll need to decide what you want to AVG over/by, and those columns in the PARTITION BY:
...
LEFT JOIN
      (SELECT * from
            (SELECT [TblReportsStoreID]
                  ,[txtSchoolID]
                        --...
                  ,[txtPosition]
                  ,Case WHEN (txtResultName IS NULL OR txtResultName = 'Average Point Score') THEN txtGradingName ELSE txtResultName END as djsresulttype
                  ,Case WHEN (txtResultName IS NULL OR txtResultName = 'Average Point Score') THEN intGradeTransposeValue ELSE txtresult END as djsresult
                 ,AVG(Case WHEN (txtResultName IS NULL OR txtResultName = 'Average Point Score') THEN intGradeTransposeValue ELSE txtresult END)
                            OVER (PARTITION BY <your_column_names_go_here>) AS Avg_djsresult
                  ,DENSE_RANK() OVER (ORDER BY dtReportCyclePrintDate desc, intReportCycleID desc) AS intCycleRank
            FROM [DJS-iSAMS-DJS].[dbo].[VwReportsManagementAcademicReports]
...
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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