Solved

SQL: AVG of an alias

Posted on 2014-10-02
2
182 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

623 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