我有一张税率表,其中NULL实体类型行代表默认税率。
Year End | EntityType | RateType | TaxRate
------------------------------------------
2009 | NULL | Interest | 13
2009 | NULL | Other | 8
2009 | NULL | Interest | 13
2010 | NULL | Other | 9
2009 | Company | Interest | 15
2010 | Individual | Other | 6
我想为此表创建一个存储过程,并将Entitytype作为参数。这样,对于每年,它返回该实体类型的税率(如果存在),否则返回该年的NULL行和ratetype。
即:使用entitytype个人查询上述内容应该返回
Year End | EntityType | RateType | TaxRate
------------------------------------------
2009 | NULL | Interest | 13
2009 | NULL | Other | 8
2009 | NULL | Interest | 13
2010 | Individual | Other | 6
任何人都可以建议我能做到这一点吗?
提前致谢,
永
答案 0 :(得分:2)
SELECT
t1.year_end,
t1.entity_type,
t1.ratetype,
COALESCE(t2.taxrate, t1.taxrate)
FROM
Rates AS t1
LEFT JOIN
Rates AS t2 ON
t1.year_end = t2.year_end
AND t2.entitytype IS NULL
WHERE t1.year_end = @year
AND t1.entitytype = @entitytype