如何通过Case语句为每行

时间:2016-12-24 09:06:05

标签: sql sql-server sql-server-2008

美好的一天。我有一个脚本,它查看表并对列[vCheck]执行一些逻辑。逻辑无法正常工作。表A粘贴在下面。

如果条件为真,则该语句应该将该关联行设置为0.它当前将同一typeCol的所有关联行设置为0.如何对每一行进行个性化'关于groupid的条件,以便具有vCheck的唯一值?我尝试使用游标方法,IF,而表,变量和更新,这导致了同样的问题。

表A

groupid eventid numid   tagVal  greaterThan greaterEqThan   lessThan    lessEqThan  equalTo typeCol vCheck  activeid    actVal
28      18      9000002 0       NULL        NULL            NULL        NULL        NULL    0       1       70          0
28      18      9000001 NULL    NULL        NULL            105         NULL        NULL    3       1       68          74.9101
28      18      9001767 NULL    NULL        NULL            10          NULL        NULL    3       1       69          9.95535
9       19      9000002 0       NULL        NULL            NULL        NULL        NULL    0       1       70          0
9       19      9000001 NULL    NULL        NULL            105         NULL        NULL    3       1       68          74.9101
9       19      9001767 NULL    10          NULL            NULL        NULL        NULL    1       1       69          9.95535
10      20      9000002 0       NULL        NULL            NULL        NULL        NULL    0       1       70          0
10      20      9000001 NULL    105         NULL            NULL        NULL        NULL    1       1       68          74.9101
10      20      9001767 NULL    10          NULL            NULL        NULL        NULL    1       1       69          9.95535

脚本

DECLARE @nID nvarchar(50)
SET @nID = '9000001'

/****************Get all associated eventids with respect to the incoming numid**/
;WITH ss
AS (SELECT
  a.groupid,
  a.eventid
FROM subobjgroup a
WHERE numid = @nID)
/****************Get each member of the eventids selected above along with the tag data type, and tagval for each member.*/
,
ss2
AS (SELECT
  c.*

FROM ss b
INNER JOIN subobjgroup c
  ON b.eventid = c.eventid
 )

/**************Check if each member in the groupid and their conditions to each eventid selected are met*/
SELECT
  g.groupid,
  g.eventid,
  g.numid,
  g.tagVal,
  g.greaterThan,
  g.greaterEqThan,
  g.lessThan,
  g.lessEqThan,
  g.equalTo,
  g.typeCol,
  vCheck =
          CASE
            WHEN g.typecol = 0 THEN CASE
                WHEN g.actval = g.tagval THEN 0
                ELSE 1
              END
            WHEN g.typecol = 1 THEN CASE
                WHEN g.actval > g.greaterthan THEN 0
                ELSE 1
              END
            WHEN g.typecol = 2 THEN CASE
                WHEN g.actval >= g.greatereqthan THEN 0
                ELSE 1
              END
            WHEN g.typecol = 3 THEN CASE
                WHEN g.actval < g.lessthan THEN 0
                ELSE 1
              END
            WHEN g.typecol = 4 THEN CASE
                WHEN g.actval <= g.lesseqthan THEN 0
                ELSE 1
              END
            WHEN g.typecol = 5 THEN CASE
                WHEN g.actval = g.equalto THEN 0
                ELSE 1
              END
            ELSE 0
          END,
  g.activeid,
  g.actVal

FROM ss2 g
ORDER BY g.eventid

结果......(请记住,最后一列actVal会不断变化。您将看到vCheck列的值不是1.请注意,所有vCheck值都使用相同的typeCol值链接,而不是唯一地检查每一行条件)

groupid eventid numid   tagVal  greaterThan greaterEqThan   lessThan    lessEqThan  equalTo typeCol vCheck  activeid    actVal
9       19      9000001 NULL    NULL        NULL            105         NULL        NULL    3       1       68          96.7658
9       19      9000002 0       NULL        NULL            NULL        NULL        NULL    0       1       70          1
9       19      9001767 NULL    10          NULL            NULL        NULL        NULL    1       0       69          10.9553
10      20      9000001 NULL    105         NULL            NULL        NULL        NULL    1       0       68          96.7658
10      20      9000002 0       NULL        NULL            NULL        NULL        NULL    0       1       70          1
10      20      9001767 NULL    10          NULL            NULL        NULL        NULL    1       0       69          10.9553
28      18      9000001 NULL    NULL        NULL            105         NULL        NULL    3       1       68          96.7658
28      18      9000002 0       NULL        NULL            NULL        NULL        NULL    0       1       70          1
28      18      9001767 NULL    NULL        NULL            10          NULL        NULL    3       1       69          10.9553

期望的结果....(注意vCheck列和第1行,第4行和第7行。它们根据条件进行更改并且该行生效并设置为0或1)

groupid eventid numid   tagVal  greaterThan greaterEqThan   lessThan    lessEqThan  equalTo typeCol vCheck  activeid    actVal
9       19      9000001 NULL    NULL        NULL            105         NULL        NULL    3       0       68          96.7658
9       19      9000002 0       NULL        NULL            NULL        NULL        NULL    0       1       70          1
9       19      9001767 NULL    10          NULL            NULL        NULL        NULL    1       0       69          10.9553
10      20      9000001 NULL    105         NULL            NULL        NULL        NULL    1       1       68          96.7658
10      20      9000002 0       NULL        NULL            NULL        NULL        NULL    0       1       70          1
10      20      9001767 NULL    10          NULL            NULL        NULL        NULL    1       0       69          10.9553
28      18      9000001 NULL    NULL        NULL            105         NULL        NULL    3       0       68          96.7658
28      18      9000002 0       NULL        NULL            NULL        NULL        NULL    0       1       70          1
28      18      9001767 NULL    NULL        NULL            10          NULL        NULL    3       1       69          10.9553

1 个答案:

答案 0 :(得分:1)

我用过这个:

with subobjgroup(groupid ,eventid ,numid ,tagVal ,greaterThan ,greaterEqThan ,lessThan ,lessEqThan ,equalTo ,typeCol ,vCheck ,activeid ,actVal) as
    (  select 28,18,9000002,0,NULL,NULL,NULL,NULL,NULL,0,1,70,0 union all
       select 28,18,9000001,NULL,NULL,NULL,105,NULL,NULL,3,1,68,74.9101 union all
       select 28,18,9001767,NULL,NULL,NULL,10,NULL,NULL,3,1,69,9.95535 union all
       select 9,19,9000002,0,NULL,NULL,NULL,NULL,NULL,0,1,70,0 union all
       select 9,19,9000001,NULL,NULL,NULL,105,NULL,NULL,3,1,68,74.9101 union all
       select 9,19,9001767,NULL,10,NULL,NULL,NULL,NULL,1,1,69,9.95535 union all
       select 10,20,9000002,0,NULL,NULL,NULL,NULL,NULL,0,1,70,0 union all
       select 10,20,9000001,NULL,105,NULL,NULL,NULL,NULL,1,1,68,74.9101 union all
       select 10,20,9001767,NULL,10,NULL,NULL,NULL,NULL,1,1,69,9.95535),
    ss AS (SELECT
      a.groupid,
      a.eventid
    FROM subobjgroup a
    WHERE numid = 9000001)
    /****************Get each member of the eventids selected above along with the tag data type, and tagval for each member.*/
    ,
    ss2
    AS (SELECT
      c.*

    FROM ss b
    INNER JOIN subobjgroup c
      ON b.eventid = c.eventid
     )

    /**************Check if each member in the groupid and their conditions to each eventid selected are met*/
    SELECT
      g.groupid,
      g.eventid,
      g.numid,
      g.tagVal,
      g.greaterThan,
      g.greaterEqThan,
      g.lessThan,
      g.lessEqThan,
      g.equalTo,
      g.typeCol,
      vCheck =
              CASE
                WHEN g.typecol = 0 THEN CASE
                    WHEN coalesce(g.actval, 0) = coalesce(g.tagval, 0) THEN 0
                    ELSE 1
                  END
                WHEN g.typecol = 1 THEN CASE
                    WHEN g.actval > g.greaterthan THEN 0
                    ELSE 1
                  END
                WHEN g.typecol = 2 THEN CASE
                    WHEN g.actval >= g.greatereqthan THEN 0
                    ELSE 1
                  END
                WHEN g.typecol = 3 THEN CASE
                    WHEN g.actval < g.lessthan THEN 0
                    ELSE 1
                  END
                WHEN g.typecol = 4 THEN CASE
                    WHEN g.actval <= g.lesseqthan THEN 0
                    ELSE 1
                  END
                WHEN g.typecol = 5 THEN CASE
                    WHEN coalesce(g.actval, 0) = coalesce(g.equalto, 0) THEN 0
                    ELSE 1
                  END
                ELSE 0
              END,
      g.activeid,
      g.actVal

    FROM ss2 g
    ORDER BY g.eventid

并根据您的数据更正结果。 vCheck仅仅因为typecol为1而未成为1,但因为数据是这样的。

groupid eventid numid   tagVal  greaterThan greaterEqThan   lessThan    lessEqThan  equalTo typeCol vCheck  activeid    actVal
28      18      9000002 0       NULL        NULL            NULL        NULL        NULL    0       0       70          0,00000
28      18      9000001 NULL    NULL        NULL            105         NULL        NULL    3       0       68          74,91010
28      18      9001767 NULL    NULL        NULL            10          NULL        NULL    3       0       69          9,95535
9       19      9000002 0       NULL        NULL            NULL        NULL        NULL    0       0       70          0,00000
9       19      9000001 NULL    NULL        NULL            105         NULL        NULL    3       0       68          74,91010
9       19      9001767 NULL    10          NULL            NULL        NULL        NULL    1       1       69          9,95535
10      20      9000002 0       NULL        NULL            NULL        NULL        NULL    0       0       70          0,00000
10      20      9000001 NULL    105         NULL            NULL        NULL        NULL    1       1       68          74,91010
10      20      9001767 NULL    10          NULL            NULL        NULL        NULL    1       1       69          9,95535