【发布时间】:2019-08-07 09:45:39
【问题描述】:
您有什么想法可以根据组行状态定义组状态或我应该在谷歌上搜索什么? 在下面的组状态中是我所追求的。谢谢
Group Row Status Group Status
A Good Good
B Good Neutral
B Neutral Neutral
B Bad Neutral
C Bad Bad
D Good Error
D Bad Error
D Error Error
我上面的表格中有一个错误,Good+Neutral+Bad=Bad 和 C 应该是 Neutral=Neutral,所以这会导致与 @forpas 和 @Gordon Linoff 的答案有些混淆
我在下面添加了另一个表格以更好地显示案例/规则
任何带有错误 = 错误的混合
好的 中性 坏 = 坏
的任何组合
任何不同的状态 = 那个状态
Group Row Status Group Status
A Good Bad
A Bad Bad
A Neutral Bad
B Good Bad
B Bad Bad
C Good Bad
C Neutral Bad
D Bad Bad
D Neutral Bad
E Good Good
F Bad Bad
G Neutral Neutral
H Error Error
I Good Error
I Bad Error
I Neutral Error
I Error Error
J Good Error
J Bad Error
J Error Error
K Good Error
K Neutral Error
K Error Error
L Bad Error
L Neutral Error
L Error Error
M Good Error
M Error Error
N Bad Error
N Error Error
O Neutral Error
O Error Error
我能够使用@forpas 方法获得我想要的结果。我认为这应该涵盖我所有的情况。
with cte as (
select
[Group],
sum(case [Row Status] when 'Good' then 1 else 0 end) good,
sum(case [Row Status] when 'Neutral' then 1 else 0 end) neutral,
sum(case [Row Status] when 'Bad' then 1 else 0 end) bad,
sum(case [Row Status] when 'Error' then 1 else 0 end) error
from [Test].[dbo].[GroupStatus]
group by [Group]
)
select
t.*,
case
when c.error > 0 then 'Error'
when c.good * c.neutral * c.bad > 0 then 'Bad'
when c.good * c.neutral > 0 then 'Bad'
when c.bad * c.neutral > 0 then 'Bad'
when c.good * c.bad > 0 then 'Bad'
when c.good > 0 then 'Good'
when c.bad > 0 then 'Bad'
when c.neutral > 0 then 'Neutral'
else 'Error'
end [Group Status]
from [Test].[dbo].[GroupStatus] t inner join cte c
on c.[Group] = t.[Group]
【问题讨论】:
-
请解释组状态的规则。不明显。
标签: sql sql-server tsql join case