Solved

SQL: AVG of an alias

Posted on 2014-10-02
2
142 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
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:
ScottPletcher 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now