我不明白爲什麼我能做到這一點:爲什麼不能在我的內表上選擇?
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')
)
我得到以下錯誤:但
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;CORRELATION NAME, DRIVER=3.53.70 SQLState: 42601 ErrorCode: -104 Error: DB2 SQL Error: 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
不知道db2,但嘗試給你的子查詢別名。類似於SELECT y FROM(...)T – ericpap
備註 - 以這種方式提取年份和月份的分組將忽略索引。加入虛擬/永久性日曆表可能會帶來更好的性能(並且不需要包裝這部分查詢)。永久日曆表對於這類事情是非常有用的。 –