TSQL - 存在的列名无效

时间:2016-07-21 19:51:20

标签: sql-server tsql

我不是世界上最伟大的SQL作者,但到目前为止,我认为我至少还不错。我正在尝试编写一个查询,该查询根据输入参数对列的计数求和,并在日期落在开始日期和结束日期之间找到一些。

但是,我无法引用日期列,即使该列存在于我查询的其中一个表中。我从SQL Server Studio中收到一条错误,指出"无效的列名称"。请注意,此列名在两个表之间是唯一的,并且我使用限定名称,因此我认为命名冲突的可能性很低。以下是我使用的SQL:

USE [DB_RPE_CONFIG]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @STARTDATE DATETIME
DECLARE @ENDDATE DATETIME
DECLARE @DOCTYPE INT

-- Leave unset to select all form type
-- SET @DOCTYPE = 700000
SET @STARTDATE = CONVERT(datetime, '2016-06-08 00:00:00')
SET @ENDDATE   = CONVERT(datetime, '2016-06-10 23:59:59')

SELECT r_sdf_definition.code,
       Sum("Forms Filed with Payment")    AS "Forms Filed with Payment", 
       Sum("Forms Filed without Payment") AS "Forms Filed without Payment"
FROM   (SELECT sdf_definition_key,
               Count(*) AS "Forms Filed with Payment", 
               0        AS "Forms Filed without Payment" 
        FROM   its_extnl_staging_document 
        WHERE  sdf_definition_key = Isnull(@DOCTYPE, sdf_definition_key) 
               AND remittance_amount > 0 
        GROUP  BY sdf_definition_key 
        UNION 
        SELECT sdf_definition_key, 
               0        AS "Forms Filed with Payment", 
               Count(*) AS "Forms Filed without Payment" 
        FROM   its_extnl_staging_document 
        WHERE  sdf_definition_key = Isnull(@DOCTYPE, sdf_definition_key) 
               AND remittance_amount = 0 
        GROUP  BY sdf_definition_key) its_extnl_staging_document 
       LEFT JOIN r_sdf_definition 
              ON its_extnl_staging_document.sdf_definition_key = 
                 r_sdf_definition.sdf_definition_key
       WHERE its_extnl_staging_document.submitted_dttm BETWEEN @STARTDATE AND @ENDDATE
GROUP  BY r_sdf_definition.code

当我尝试使用没有WHERE子句的查询时,结果会很好。我得到了各种表单类型的总计,无论我是专门指定表单类型还是默认为存在的所有表单类型。

任何人都可以提出任何建议或指出我正确的方向吗?

编辑--------------------

通过在内部选择中添加where规范的建议过程,我得到以下内容。但是外部选择仍然以同样的方式出错:

SELECT r_sdf_definition.code,
       Sum("Forms Filed with Payment")    AS "Forms Filed with Payment", 
       Sum("Forms Filed without Payment") AS "Forms Filed without Payment"
FROM   (SELECT sdf_definition_key,
               Count(*) AS "Forms Filed with Payment", 
               0        AS "Forms Filed without Payment", 
               "submitted_dttm" AS submitted_dttm
        FROM   its_extnl_staging_document 
        WHERE  sdf_definition_key = Isnull(@DOCTYPE, sdf_definition_key) 
               AND remittance_amount > 0
               AND (its_extnl_staging_document.submitted_dttm BETWEEN @STARTDATE AND @ENDDATE)
        GROUP  BY sdf_definition_key 
        UNION 
        SELECT sdf_definition_key, 
               0        AS "Forms Filed with Payment", 
               Count(*) AS "Forms Filed without Payment",
               "submitted_dttm" AS submitted_dttm 
        FROM   its_extnl_staging_document 
        WHERE  sdf_definition_key = Isnull(@DOCTYPE, sdf_definition_key) 
               AND remittance_amount = 0
               AND (its_extnl_staging_document.submitted_dttm BETWEEN @STARTDATE AND @ENDDATE)
        GROUP  BY sdf_definition_key
) its_extnl_staging_document 
LEFT JOIN r_sdf_definition 
       ON its_extnl_staging_document.sdf_definition_key = 
          r_sdf_definition.sdf_definition_key
WHERE its_extnl_staging_document.submitted_dttm BETWEEN @STARTDATE AND @ENDDATE
GROUP  BY r_sdf_definition.code

2 个答案:

答案 0 :(得分:0)

如果你在谈论最后的WHERE子句,它实际上不存在。

WHERE its_extnl_staging_document.submitted_dttm BETWEEN @STARTDATE AND @ENDDATE

问题是你在UNION加入的两个select子句中没有名为“submitted_dttm”的列。

您需要将其包含在每个内部SELECT中,或者将WHERE子句放在每个SELECT中。你可以单独留下外部的SELECT。

另一个选项是submitted_dttm列位于另一个与LEFT JOINed相关的表上。

更新

这是一种稍微不同的方法,但可能更容易理解:

SELECT  sdf_definition_key, RemittanceType, COUNT(*)
FROM    its_extnl_staging_document
    LEFT JOIN r_sdf_definition 
        ON its_extnl_staging_document.sdf_definition_key = r_sdf_definition.sdf_definition_key
WHERE   sdf_definition_key = Isnull(@DOCTYPE, sdf_definition_key) 
GROUP BY r_sdf_definition.code,
    CASE    
        WHEN (remittance_amount = 0) THEN 1
        ELSE 2
    END AS RemittanceType

这应该为您提供两种类型的行:

Key    RemittanceType    COUNT

然后你可以把它包装在另一个选择中。

答案 1 :(得分:0)

有时缩进很重要......

_.without