Solved

non normalized table

Posted on 2014-01-26
7
346 Views
Last Modified: 2014-01-30
I have a non normalized table - I think that's the correct description.  I have a column that is a code that indicates the type of data that is contained in a second column, so let's say "code" and "value" are the column names.    I need the average of all value for each code returned in a single record.  I only need this for, say 3 specific codes, 11, 22, 33.  How can I do this?
0
Comment
Question by:HLRosenberger
7 Comments
 
LVL 13

Expert Comment

by:magarity
ID: 39810300
select code, avg(value) from table where code in (11,22,33) group by code
0
 
LVL 13

Expert Comment

by:magarity
ID: 39810308
PS - A 'denormalized' table is one where two or more normalized tables are combined.  For example, if 'code' in your example also has 'code long description' in this same table then that would be denormalized.  If code long description were in a lookup table, that would be normalized,
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39810818
>>" returned in a single record. "

Why must it be returned as a single record?

(the solution provided above will produce 3 rows)
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39812202
SELECT
    AVG(CASE WHEN code = '11' THEN value END) AS code_11_avg,
    AVG(CASE WHEN code = '22' THEN value END) AS code_22_avg,
    AVG(CASE WHEN code = '33' THEN value END) AS code_33_avg
FROM dbo.tablename
WHERE
    code IN ( '11', '22', '33' )
0
 
LVL 13

Expert Comment

by:magarity
ID: 39812491
It's not clear either way that it's supposed to be a single record result set. That sentence doesn't parse well. The second query will do if that's the case.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39812740
Perhaps some sample data and your expected output would help clarify. I think one of the two solutions provided will accomplish what you want. Assume the average value of code 11 is 5, and the average value of code 22 is 10, and the average value of code 33 is 15, the first produces
code value
11     5
22    10
33    15
and the second produces
11avgval  22avgval  33avgval
5              10             15
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 39823205
thanks.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

815 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

8 Experts available now in Live!

Get 1:1 Help Now