3

我有一个表 NETWORKS,每个网络可以有多个 CIRCUITS。每个网络都有一个整体状态(红色/黄色/绿色),每个电路都有一个单独的状态(红色/绿色)。每个电路的状态都是手动设置的。网络状态如下:

  • 如果它的所有电路都是绿色的--> 绿色
  • 如果它的所有电路都是红色的-->红色
  • 如果至少 1 个(但不是全部)电路为绿色 --> 黄色
  • 如果没有电路 --> NULL(无状态)

我正在尝试选择所有网络,其状态由 SELECT 动态确定,而不必将状态作为表中的列保存和管理。我想不出一种有效的方法来做到这一点。我现在的工作(都是小表,< 100,具有相对静态数据量的行),但效率极低,我希望有更好的方法。

SELECT
(CASE 
    WHEN (
        SELECT COUNT(*) 
        FROM NETWORK_CIRCUITS 
        WHERE network_id = N.network_id
    ) = 0 THEN 'noStatus'
    WHEN (
        SELECT COUNT(*) 
        FROM NETWORK_CIRCUITS 
        WHERE network_id = N.network_id
        AND [status] = 'greenStatus'
    ) = (
        SELECT COUNT(*) 
        FROM NETWORK_CIRCUITS 
        WHERE network_id = SSN.network_id
    ) THEN 'greenStatus'
    WHEN (
        SELECT COUNT(*) 
        FROM NETWORK_CIRCUITS 
        WHERE network_id = N.network_id
        AND [status] = 'redStatus'
    ) = (
        SELECT COUNT(*) 
        FROM NETWORK_CIRCUITS 
        WHERE network_id = N.network_id
    ) THEN 'redStatus'
    ELSE 'yellowStatus'
END) network_status
FROM NETWORKS N
4

3 回答 3

3

这是一种方法:

SELECT
    CASE 
        WHEN CircuitCount IS NULL      THEN 'noStatus'
        WHEN GreenCount = CircuitCount THEN 'greenStatus'
        WHEN GreenCount = 0            THEN 'redStatus'
        ELSE 'yellowStatus'
    END As network_status
FROM NETWORKS As N
LEFT JOIN
(   SELECT  COUNT(*) As CircuitCount,
            COUNT(NULLIF([status],'redStatus')) As GreenCount,
            network_id
    FROM    NETWORK_CIRCUITS
    GROUP BY network_id
)   As C    ON N.network_id = C.network_id
于 2013-07-11T17:28:31.423 回答
1

我的第一个想法是:

select network_id, case when green=0 and red=0 then null when green=0 then 'red' else when red=0 then 'green' else 'yellow' end as status
from
(select n.network_id,
  sum(case when c.status='green' then 1 else 0 end) as green,
  sum(case when c.status='red' then 1 else 0 end) as red
from network n
join circuit c on c.network_id=n.network_id
group by n.network_id)
于 2013-07-11T17:31:32.890 回答
1

我认为这应该做你想要的。它也应该比使用一堆子查询更快。

SELECT CASE
         WHEN circuits.network_id is NULL THEN 'No Status'
         WHEN circuits.greenCount = circuits.totalCircuits THEN 'Green'
         WHEN circuits.greenCount >= 1 and circuits.redCount >= 1 THEN 'Yellow'
         WHEN circuits.redCount = circuits.totalCircuits THEN 'Red'
       END as network_status
      , N.network_id
FROM NETWORKS N
LEFT JOIN 
    (SELECT network_id
           , sum(CASE WHEN [status] = 'redStatus' THEN 1 ELSE 0 END) as redCount
           , sum(CASE WHEN [status] = 'greenStatus' THEN 1 ELSE 0 END) as greenCount
           , count(*) as totalCircuits
    FROM NETWORK_CIRCUITS
    GROUP BY network_id) as circuits ON circuits.network_id = N.network_id
于 2013-07-11T17:27:36.797 回答