# non normalized table

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?
LVL 1
###### Who is Participating?

Senior DBACommented:
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

Commented:
select code, avg(value) from table where code in (11,22,33) group by code
0

Commented:
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

freelancerCommented:
>>" returned in a single record. "

Why must it be returned as a single record?

(the solution provided above will produce 3 rows)
0

Commented:
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

Commented:
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

Author Commented:
thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.