-4

有人可以告诉我为什么我收到此代码错误吗?

 SqlCommand scGetClaimedDetails = new SqlCommand(
    "SELECT SUM(isclaimable) as claimable, SUM(isclaimed) as claimed,SUM(total) as total from" +
    " ( SELECT CASE WHEN claimed = 'Y' THEN inv_amt *.45 END as isclaimed, (inv_amt *.45) as inclaimable, inv_amt as total from invasset" +
    " INNER JOIN Invoice ON invoice.invoice = invasset.invoice WHERE invasset.asset_no = @AssetNumber ) as D2", DataAccess.AssetConnection);
4

3 回答 3

1

在子查询中指定 inv_amt 列来自哪里(是 invoice.inv_amt 还是 invasset.inv_amt)

于 2012-07-12T13:07:23.887 回答
1

不明确的列名 inv_amt

它像两个表一样接缝,invasset并且Invoice都包含 column inv_amt,您必须将其引用到内部SELECT语句中的别名,例如:invoice.inv_amtinvasset.inv_amt

SELECT SUM(isclaimable) as claimable, SUM(isclaimed) as claimed, 
       SUM(total) as total 
FROM
( 
   SELECT CASE WHEN claimed = 'Y' THEN invoice.inv_amt *.45 END as isclaimed, 
   (inv_amt *.45) as  inclaimable, inv_amt as total 
   from invasset INNER JOIN Invoice  ON invoice.invoice = invasset.invoice 
   WHERE invasset.asset_no = @AssetNumber 
) as D2
于 2012-07-12T13:08:26.667 回答
0

当我将您的代码格式化为人类可读的...

SqlCommand scGetClaimedDetails = new SqlCommand(
    "SELECT
         SUM(isclaimable) AS claimable,
         SUM(isclaimed) AS claimed,
         SUM(total) AS total
     FROM
         (SELECT
              CASE WHEN claimed = 'Y' THEN inv_amt *.45 END AS isclaimed,
              (inv_amt *.45) AS inclaimable,
              inv_amt AS total
          FROM
              invasset
              INNER JOIN Invoice ON invoice.invoice = invasset.invoice
          WHERE
              invasset.asset_no = @AssetNumber) AS D2",
    DataAccess.AssetConnection);

我不禁注意到你的外部SELECT正在寻找一个名为的列isclaimable,而你的内部SELECT正在返回一个名为inclaimable. 你的代码有错别字。

编辑:针对您对问题的评论,哪些表包含该字段inv_amt?显然,您正在引用两个拥有它的表。由于您引用的唯一表是invasset并且Invoice显然这两个表都有一个名为inv_amt. 您必须在查询中指定您想要的。

所以而不是:

`inv_amt AS total`

你会想要这样的东西:

`invasset.inv_amt AS total`

(假设这是您想要的表,否则使用另一个表。)

于 2012-07-12T13:09:05.833 回答