tsql sum data and include default values for missing data

Posted by markpirvine on Stack Overflow See other posts from Stack Overflow or by markpirvine
Published on 2010-05-13T15:43:12Z Indexed on 2010/05/13 15:44 UTC
Read the original article Hit count: 181

Filed under:

Hi,

I would like a query that will shouw a sum of columns with a default value for missing data. For example assume I have a table as follows:

type_lookup: id name 1 self 2 manager 3 peer

And a table as follows

data: id type_lookup_id value 1 1 1 2 1 4 3 2 9 4 2 1 5 2 9 6 1 5 7 2 6 8 1 2 9 1 1

After running a query I would like a result set as follows:

type_lookup_id value 1 13 2 25 3 0

I would like all rows in type_lookup table to be included in the result set - even if they don't appear in the data table.

Any help would be greatly appreciated,

Mark

© Stack Overflow or respective owner

Related posts about tsql