我有这个问题:
SELECT COUNT (DISTINCT CUSTOMER_ACCOUNT.ID) AS NUMBER_OF_ACCOUNTS
FROM CUSTOMER_ACCOUNT
INNER JOIN ACCOUNT ON CUSTOMER_ACCOUNT.ACCOUNT_ID=ACCOUNT.ID
INNER JOIN CUSTOMER_ACCOUNT ON LOAD_ACCOUNT_DETAIL_0.ID = CUSTOMER_ACCOUNT.ID
WHERE Convert(datetime, convert(char(10), [CUSTOMER_ACCOUNT].CREATED_ON, 101)) BETWEEN '2009-01-01' AND '2009-05-1'
AND CUSTOMER_ACCOUNT.DELETED!='1'
AND ACCOUNT.DELETED !='1'and LOAD_ACCOUNT_DETAIL_0.ACCOUNT_STATUS='1'
我收到此错误:
Msg 1013,Level 16,State 1,Line 1 FROM子句中的对象“CUSTOMER_ACCOUNT”和“CUSTOMER_ACCOUNT”具有相同的公开名称。使用相关名来区分它们。
不确定如何解决 - 任何想法?
答案 0 :(得分:5)
我认为你在SQL中犯了一个错误。这一行:
INNER JOIN CUSTOMER_ACCOUNT ON LOAD_ACCOUNT_DETAIL_0.ID = CUSTOMER_ACCOUNT.ID
应该是这个吗?
INNER JOIN LOAD_ACCOUNT_DETAIL_0 ON LOAD_ACCOUNT_DETAIL_0.ID = CUSTOMER_ACCOUNT.ID
除非您的意图实际上是自我加入,否则您需要对表名进行别名(正如其他人所提到的那样)
答案 1 :(得分:1)
您在查询中指定了两次表“CUSTOMER ACCOUNT”用作FROM以及INNER JOIN。
如果您使用CUSTOMER_ACCOUNT表的别名更改查询,那么您应该没问题。
示例:
FROM CUSTOMER_ACCOUNT AS cust_acct
INNER JOIN CUSTOMER_ACCOUNT as cust_acct_join
然后在引用列时,请务必使用您创建的别名
示例:
SELECT COUNT (DISTINCT **cust_acct.**CUSTOMER_ACCOUNT.ID) AS NUMBER_OF_ACCOUNTS
答案 2 :(得分:0)
你有CUSTOMER_ACCOUNT个自我加入。您需要至少为其中一个别名。
SELECT COUNT (DISTINCT CUSTOMER_ACCOUNT.ID) AS NUMBER_OF_ACCOUNTS
FROM CUSTOMER_ACCOUNT CA1 INNER JOIN
ACCOUNT ON CA1.ACCOUNT_ID=ACCOUNT.ID INNER JOIN
CUSTOMER_ACCOUNT CA2 ON LOAD_ACCOUNT_DETAIL_0.ID = CA2.CUSTOMER_ACCOUNT.ID
WHERE
Convert(datetime,convert(char(10),[CA1].CREATED_ON,101))
BETWEEN '2009-01-01' AND '2009-05-1' AND CA1.DELETED!='1' AND
ACCOUNT.DELETED !='1'and LOAD_ACCOUNT_DETAIL_0.ACCOUNT_STATUS='1'
答案 3 :(得分:0)
如果你自己加入一个表,你必须使用别名:
SELECT … FROM CUSTOMER_ACCOUNT AS C1 … CUSTOMER_ACCOUNT AS C2 …
答案 4 :(得分:0)
做类似的事情:
SELECT COUNT (DISTINCT t1.ID) AS NUMBER_OF_ACCOUNTS
FROM CUSTOMER_ACCOUNT t1 INNER JOIN
ACCOUNT ON t1.ACCOUNT_ID=ACCOUNT.ID INNER JOIN
CUSTOMER_ACCOUNT t2 ON LOAD_ACCOUNT_DETAIL_0.ID = t2.ID
WHERE Convert(datetime,convert(char(10),t1.CREATED_ON,101))
BETWEEN '2009-01-01' AND '2009-05-1' AND t1.DELETED!='1' AND
ACCOUNT.DELETED !='1' and LOAD_ACCOUNT_DETAIL_0.ACCOUNT_STATUS='1'