使用SqlServer进行硬查询(使用When和else)

时间:2018-02-20 10:12:03

标签: sql-server

我有一个查询,但我不知道为什么它是坏的。 错误是:SQL(4104):无效的列名'CUSTOMDATA2'多部分标识符“PCO.QUALIFICATIONCODE.code”无法绑定,但我不了解它的位置和原因。

我对这个问题很遗憾:/,我可以帮助一些查理大师吗? 编辑查询不要伤害别人。 提前谢谢。

SELECT PCO_QUALIFICATIONCODE.CODE,
  CASE
    WHEN (ActividadesActivas.[CUSTOMDATA2] IS NULL)
    THEN
      (SELECT Description
      FROM PCO_QUALIFICATIONCODE BIS
      WHERE SERVICEID = 1
      AND BIS.CODE    = SUBSTRING(CAST(PCO_QUALIFICATIONCODE.CODE AS VARCHAR), 1, 3)
      )
    ELSE ActividadesActivas.[CUSTOMDATA2]
  END CUSTOMDATA2,
  CASE
    WHEN (ActividadesActivas.[CUSTOMDATA3] IS NULL)
    THEN
      CASE
        WHEN (SUBSTRING(CAST(CODE AS VARCHAR), 5, 1) <> '')
        THEN
          (SELECT Description
          FROM PCO_QUALIFICATIONCODE BIS
          WHERE SERVICEID = 1
          AND BIS.CODE    = SUBSTRING(CAST(PCO_QUALIFICATIONCODE.CODE AS VARCHAR), 1, 5)
          )
        WHEN (SUBSTRING(CAST(CODE AS VARCHAR), 4, 1) <> '')
        THEN
          (SELECT Description
          FROM PCO_QUALIFICATIONCODE BIS
          WHERE SERVICEID = 1
          AND BIS.CODE    = SUBSTRING(CAST(PCO_QUALIFICATIONCODE.CODE AS VARCHAR), 1, 4)
          )
        WHEN ActividadesActivas.[CUSTOMDATA3] IS NULL
        OR ActividadesActivas.[CUSTOMDATA3]    = 'NULL'
        THEN ''
        ELSE ActividadesActivas.[CUSTOMDATA3]
      END
    ELSE ActividadesActivas.[CUSTOMDATA3]
  END CUSTOMDATA3,
  Totales.TOTAL
FROM [SQLPR1].[PREP].PCO_QUALIFICATIONCODE
LEFT JOIN
  (SELECT [QCODE],
    CASE
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1)  = '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1)  = '') )
      THEN
        (SELECT Description
        FROM PCO_QUALIFICATIONCODE
        WHERE SERVICEID = 1
        AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3)
        )
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1) <> '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1)  = '') )
      THEN
        (SELECT Description
        FROM PCO_QUALIFICATIONCODE
        WHERE SERVICEID = 1
        AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3)
        )
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1) <> '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1) <> '') )
      THEN
        (SELECT Description
        FROM PCO_QUALIFICATIONCODE
        WHERE SERVICEID = 1
        AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3)
        )
    END CUSTOMDATA2,
    CASE
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1)  = '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1)  = '') )
      THEN
        CASE
          WHEN CUSTOMDATA3 IS NULL
          OR CUSTOMDATA3    = 'NULL'
          THEN ''
          ELSE CUSTOMDATA3
        END
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1) <> '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1)  = '') )
      THEN
        (SELECT Description
        FROM PCO_QUALIFICATIONCODE
        WHERE SERVICEID = 1
        AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 4)
        )
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1) <> '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1) <> '') )
      THEN
        CASE
          WHEN (SELECT Description
            FROM PCO_QUALIFICATIONCODE
            WHERE SERVICEID = 1
            AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 4)) IS NOT NULL
          THEN
            (SELECT Description
            FROM PCO_QUALIFICATIONCODE
            WHERE SERVICEID = 1
            AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 4)
            ) + ' / ' +
            (SELECT Description
            FROM PCO_QUALIFICATIONCODE
            WHERE SERVICEID = 1
            AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 5)
            )
          ELSE
            (SELECT Description
            FROM PCO_QUALIFICATIONCODE
            WHERE SERVICEID = 1
            AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 5)
            )
        END
      ELSE ''
    END CUSTOMDATA3
  FROM [SQLPR1].[PREP].[PCO_INBOUNDLOG]
  WHERE SERVICEID = 1
  AND QCODE       > 9
  GROUP BY QCODE,
    CUSTOMDATA2,
    CUSTOMDATA3
  ) ActividadesActivas
ON PCO_QUALIFICATIONCODE.CODE = ActividadesActivas.QCODE
LEFT JOIN
  (SELECT [QCODE],
    CASE
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1)  = '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1)  = '') )
      THEN
        (SELECT Description
        FROM PCO_QUALIFICATIONCODE
        WHERE SERVICEID = 1
        AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3)
        )
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1) <> '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1)  = '') )
      THEN
        (SELECT Description
        FROM PCO_QUALIFICATIONCODE
        WHERE SERVICEID = 1
        AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3)
        )
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1) <> '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1) <> '') )
      THEN
        (SELECT Description
        FROM PCO_QUALIFICATIONCODE
        WHERE SERVICEID = 1
        AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3)
        )
    END CUSTOMDATA2,
    CASE
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1)  = '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1)  = '') )
      THEN
        CASE
          WHEN CUSTOMDATA3 IS NULL
          OR CUSTOMDATA3    = 'NULL'
          THEN ''
          ELSE CUSTOMDATA3
        END
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1) <> '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1)  = '') )
      THEN
        (SELECT Description
        FROM PCO_QUALIFICATIONCODE
        WHERE SERVICEID = 1
        AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 4)
        )
      WHEN ((SUBSTRING(CAST(QCODE AS VARCHAR), 1, 3) IS NOT NULL)
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 4, 1) <> '')
      AND (SUBSTRING(CAST(QCODE AS   VARCHAR), 5, 1) <> '') )
      THEN
        CASE
          WHEN (SELECT Description
            FROM PCO_QUALIFICATIONCODE
            WHERE SERVICEID = 1
            AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 4)) IS NOT NULL
          THEN
            (SELECT Description
            FROM PCO_QUALIFICATIONCODE
            WHERE SERVICEID = 1
            AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 4)
            ) + ' / ' +
            (SELECT Description
            FROM PCO_QUALIFICATIONCODE
            WHERE SERVICEID = 1
            AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 5)
            )
          ELSE
            (SELECT Description
            FROM PCO_QUALIFICATIONCODE
            WHERE SERVICEID = 1
            AND CODE        = SUBSTRING(CAST(QCODE AS VARCHAR), 1, 5)
            )
        END
      ELSE ''
    END CUSTOMDATA3,
    COUNT(*) TOTAL
  FROM [SQLPR1].[PREP].[PCO_INBOUNDLOG]
  WHERE [SERVICEID] = 1
  AND RDATE BETWEEN '2017/02/01 00:00:00' AND '2018/02/20 23:59:59'
  GROUP BY QCODE,
    CUSTOMDATA2,
    CUSTOMDATA3
  ) Totales
ON ActividadesActivas.QCODE         = Totales.QCODE
AND ActividadesActivas.CUSTOMDATA2  = Totales.CUSTOMDATA2
AND ActividadesActivas.CUSTOMDATA3  = Totales.CUSTOMDATA3
WHERE SERVICEID                     = 1
AND PCO_QUALIFICATIONCODE.CODE NOT IN (102,103,104,105,106,107,113)
GROUP BY PCO_QUALIFICATIONCODE.CODE,
  ActividadesActivas.CUSTOMDATA2,
  ActividadesActivas.CUSTOMDATA3,
  Totales.TOTAL
ORDER BY CAST(PCO_QUALIFICATIONCODE.CODE AS VARCHAR),
  CUSTOMDATA2 

1 个答案:

答案 0 :(得分:0)

你的问题出在小组中。

你正在屏蔽你的领域&#34; ActividadesActivas。[CUSTOMDATA2]&#34;使用&#34; CUSTOMDATA2&#34;

CASE
    WHEN (ActividadesActivas.[CUSTOMDATA2] IS NULL)
    THEN
      (SELECT Description
      FROM PCO_QUALIFICATIONCODE BIS
      WHERE SERVICEID = 1
      AND BIS.CODE    = SUBSTRING(CAST(PCO_QUALIFICATIONCODE.CODE AS VARCHAR), 1, 3)
      )
    ELSE ActividadesActivas.[CUSTOMDATA2]
  END CUSTOMDATA2,

所以,在小组中,你只能使用&#34; CUSTOMDATA2&#34;而不是&#34; ActividadesActivas。[CUSTOMDATA2]&#34;

GROUP BY PCO_QUALIFICATIONCODE.CODE,
  ActividadesActivas.CUSTOMDATA2,
  ActividadesActivas.CUSTOMDATA3,
  Totales.TOTAL

PS:同样的规则应适用于剩余的字段。

编辑:我不确定这是否是您查询的唯一问题,但这就是您的问题&#34;无效的列名称&#39; CUSTOMDATA2&#39;&#34; < / p>