我不明白为什么我能这样做:
SELECT YEAR(CREATION_TIME) AS y, MONTH(CREATION_TIME) as m, COUNTRY_CODE
FROM PRODUCTS INNER JOIN COMPANIES
ON COMPANIES.COMPANY_KEY = PRODUCTS.CUSTOMER_ID
WHERE CREATED_BY IN ('VOLVO','SAAB')
但不是
SELECT y FROM
(
SELECT YEAR(CREATION_TIME) AS y, MONTH(CREATION_TIME) AS m, COUNTRY_CODE
FROM PRODUCTS INNER JOIN COMPANIES
ON COMPANIES.COMPANY_KEY = PRODUCTS.CUSTOMER_ID
WHERE CREATED_BY IN ('VOLVO','SAAB')
)
我收到以下错误:
错误:DB2 SQL错误:SQLCODE = -104,SQLSTATE = 42601, SQLERRMC = ;; CORRELATION NAME,DRIVER = 3.53.70 SQLState:42601 ErrorCode:-104错误:DB2 SQL错误:SQLCODE = -514,SQLSTATE = 26501, SQLERRMC = SQL_CURLH200C1,DRIVER = 3.53.70 SQLState:26501 ErrorCode: -514
我的最终目标是做一些事情:
SELECT y, m, COUNTRY_CODE, count(*)
FROM
(
SELECT YEAR(CREATION_TIME) AS y, MONTH(CREATION_TIME) AS m, COUNTRY_CODE
FROM PRODUCTS INNER JOIN COMPANIES
ON COMPANIES.COMPANY_KEY = PRODUCTS.CUSTOMER_ID
WHERE CREATED_BY IN ('VOLVO','SAAB')
)
GROUP BY y, m, COUNTRY_CODE
答案 0 :(得分:1)
第二个查询中的语法不正确。如果不引用该表,则无法引用列别名。这应该有效:
SELECT t.y FROM
(
SELECT YEAR(CREATION_TIME) AS y, MONTH(CREATION_TIME) AS m, COUNTRY_CODE
FROM PRODUCTS INNER JOIN COMPANIES
ON COMPANIES.COMPANY_KEY = PRODUCTS.CUSTOMER_ID
WHERE CREATED_BY IN ('VOLVO','SAAB')
) AS t
答案 1 :(得分:1)
您必须运行一个非常旧的DB2版本。在某些时候,子选择需要相关名称,所以试试这个:
SELECT t.y FROM
(
SELECT YEAR(CREATION_TIME) AS y, MONTH(CREATION_TIME) AS m, COUNTRY_CODE
FROM PRODUCTS INNER JOIN COMPANIES
ON COMPANIES.COMPANY_KEY = PRODUCTS.CUSTOMER_ID
WHERE CREATED_BY IN ('VOLVO','SAAB')
) AS t