我目前有以下查询:
SELECT name, id,
CASE
WHEN( status = 'Missing' AND severity = 'Optional' ) AND ( id=123 )
THEN COALESCE(count(patchid),0)
ELSE 0
END AS missingoptional,
CASE
WHEN( status = 'Missing' AND severity = 'Important' ) AND ( id=123 )
THEN COALESCE(count(patchid),0)
ELSE 0
END as missingimportant
FROM tablename
GROUP BY name, id, status, severity
ORDER BY id
目前的结果:
name id missingoptional missingimportant
abc 123 10 0
abc 123 0 20
预期结果:
name id missingoptional missingimportant
abc 123 10 20
请告诉我在上述查询中我必须做哪些更改才能获得预期的输出。
答案 0 :(得分:1)
也许这就是你想要的:
SELECT name, id,
SUM(CASE
WHEN( status = 'Missing' AND severity = 'Optional' ) AND ( id=123 )
THEN 1
ELSE 0
END) AS missingoptional,
SUM(CASE
WHEN( status = 'Missing' AND severity = 'Important' ) AND ( id=123 )
THEN 1
ELSE 0
END) as missingimportant
FROM tablename
GROUP BY name, id
ORDER BY id
我从GROUP BY
删除了状态和严重性,而是将SUM()
放入选择列表进行聚合。
答案 1 :(得分:0)
使用MAX()聚合函数
interface IAnimal
{
pubilc void Hunt();
}
interface IWeapon
{
}
Animal : IAnimal
{
IWeapon _weapon;
public void Animal(IWeapon weapon)
{
_weapon = weapon;
}
public void Hunt()
{
UseWeapon(_weapon);
}
protected abstract void UseWeapon(IWeapon weapon);
}