SELECT DISTINCT
T_SERIAL_SET_INFO.SET_IRON_NO,
T_PRODUCTS_M.PRODUCTS_HEAD,
T_SERIAL_SET_INFO.IRON_SET_DATE,
T_SERIAL_SET_INFO.SET_LIFT_COM
FROM T_SERIAL_SET_INFO
INNER JOIN T_SERIAL_INFO
ON T_SERIAL_SET_INFO.SET_IRON_NO=T_SERIAL_INFO.SERIAL_NO
INNER JOIN T_PRODUCTS_COMPONENT_M
ON T_SERIAL_INFO.PARTS_CODE=T_PRODUCTS_COMPONENT_M.PARTS_CODE
INNER JOIN T_PRODUCTS_M
ON T_PRODUCTS_COMPONENT_M.PRDT_CODE=T_PRODUCTS_M.PRDT_CODE
WHERE T_SERIAL_SET_INFO.IRON_NO='0123456789'
ORDER BY
CASE T_PRODUCTS_M.PRODUCTS_HEAD
WHEN 'P' THEN 20
WHEN 'A' THEN 30
WHEN 'AS' THEN 40
WHEN 'S' THEN 50
ELSE CAST(T_PRODUCTS_M.PRODUCTS_HEAD as integer)
END
錯誤信息是
ERROR: SELECT DISTINCTではORDER BYの式はSELECTリスト內になければなりません
LINE 15: CASE T_PRODUCTS_M.PRODUCTS_HEAD
^
(在用SELECT DISTINCT時,用ORDER BY排列的列必須在SELECT列表裡)
我知道這個寫法是錯的,但是不知道該怎麼改
SELECT DISTINCT
T_SERIAL_SET_INFO.SET_IRON_NO,
T_PRODUCTS_M.PRODUCTS_HEAD,
T_SERIAL_SET_INFO.IRON_SET_DATE,
T_SERIAL_SET_INFO.SET_LIFT_COM,CASE T_PRODUCTS_M.PRODUCTS_HEAD
WHEN 'P' THEN 20
WHEN 'A' THEN 30
WHEN 'AS' THEN 40
WHEN 'S' THEN 50
ELSE CAST(T_PRODUCTS_M.PRODUCTS_HEAD as integer)
FROM T_SERIAL_SET_INFO
INNER JOIN T_SERIAL_INFO
ON T_SERIAL_SET_INFO.SET_IRON_NO=T_SERIAL_INFO.SERIAL_NO
INNER JOIN T_PRODUCTS_COMPONENT_M
ON T_SERIAL_INFO.PARTS_CODE=T_PRODUCTS_COMPONENT_M.PARTS_CODE
INNER JOIN T_PRODUCTS_M
ON T_PRODUCTS_COMPONENT_M.PRDT_CODE=T_PRODUCTS_M.PRDT_CODE
WHERE T_SERIAL_SET_INFO.IRON_NO='0123456789'
ORDER BY
CASE T_PRODUCTS_M.PRODUCTS_HEAD
WHEN 'P' THEN 20
WHEN 'A' THEN 30
WHEN 'AS' THEN 40
WHEN 'S' THEN 50
ELSE CAST(T_PRODUCTS_M.PRODUCTS_HEAD as integer)
END
指定distinct的時候,order by子句中的項就必須出現在選擇列表中 。