web-dev-qa-db-ja.com

select unionをorder byで使用しようとしましたが、今ではそれを選択できますが、日付順ではないようですが、なぜですか?

SELECT *
FROM (
    SELECT sale.NAME AS "Name"
        ,sale.responsible AS "Responsible:data:200"
        ,sale.action_type AS "Action Type:data:200"
        ,sale.id_customer AS "Id_Customer"
        ,sale.workflow_state AS "Workflow:data:200"
        ,sale.close_date AS "Close Date:data:200"
        ,sale.modified_by AS "Done By:data:200"
    FROM `tabsales_action` sale
    WHERE sale.workflow_state = 'Done'
    ORDER BY sale.close_date DESC
    ) `tabsales_action`

UNION

SELECT *
FROM (
    SELECT mfi.NAME AS "Name"
        ,mfi.responsible AS "Responsible:data:200"
        ,mfi.action_type AS "Action Type:data:200"
        ,mfi.id_customer AS "Id_Customer"
        ,mfi.workflow_state AS "Workflow:data:200"
        ,mfi.close_date AS "Close Date:data:200"
        ,mfi.modified_by AS "Done By:data:200"
    FROM `tabmfi_loan_action` mfi
    WHERE mfi.workflow_state = 'Done'
    ORDER BY mfi.close_date DESC
    ) `tabmfi_loan_action`

UNION

SELECT *
FROM (
    SELECT credit.NAME AS "Name"
        ,credit.responsible AS "Responsible:data:200"
        ,credit.action_type AS "Action Type:data:200"
        ,credit.id_customer AS "Id_Customer"
        ,credit.workflow_state AS "Workflow:data:200"
        ,credit.close_date AS "Close Date:data:200"
        ,credit.modified_by AS "Done By:data:200"
    FROM `tabin_house_credit_action` credit
    WHERE credit.workflow_state = 'Done'
    ORDER BY credit.close_date DESC
    ) `tabin_house_credit_action`

UNION

SELECT *
FROM (
    SELECT service.NAME AS "Name"
        ,service.responsible AS "Responsible:data:200"
        ,service.action_type AS "Action Type:data:200"
        ,service.id_customer AS "Id_Customer"
        ,service.workflow_state AS "Workflow:data:200"
        ,service.close_date AS "Close Date:data:200"
        ,service.modified_by AS "Done By:data:200"
    FROM `tabservices_planning` service
    WHERE service.workflow_state = 'Done'
    ORDER BY service.close_date DESC
    ) `tabservices_planning`

UNION

SELECT *
FROM (
    SELECT account.NAME AS "Name"
        ,account.responsible AS "Responsible:data:200"
        ,account.action_type AS "Action Type:data:200"
        ,account.id_customer AS "Id_Customer"
        ,account.workflow_state AS "Workflow:data:200"
        ,account.close_date AS "Close Date:data:200"
        ,account.modified_by AS "Done By:data:200"
    FROM `tabaccounting_action` account
    WHERE account.workflow_state = 'Done'
    ORDER BY account.close_date DESC
    ) `tabaccounting_action`
1
J.H

すべてのテーブルのclose_date列でDESC、次にUNIONの順に並べ替えているようです。このアプローチの代わりに、すべてのテーブルをSELECT UNIONし、最終結果では、ORDER BYDESCとしてClose Date:data:200できます。

機能するクエリは次のとおりです。

SELECT T.*
FROM (
    SELECT sale.NAME AS "Name"
        ,sale.responsible AS "Responsible:data:200"
        ,sale.action_type AS "Action Type:data:200"
        ,sale.id_customer AS "Id_Customer"
        ,sale.workflow_state AS "Workflow:data:200"
        ,sale.close_date AS "Close Date:data:200"
        ,sale.modified_by AS "Done By:data:200"
    FROM `tabsales_action` sale
    WHERE sale.workflow_state = 'Done'

    UNION

    SELECT mfi.NAME AS "Name"
        ,mfi.responsible AS "Responsible:data:200"
        ,mfi.action_type AS "Action Type:data:200"
        ,mfi.id_customer AS "Id_Customer"
        ,mfi.workflow_state AS "Workflow:data:200"
        ,mfi.close_date AS "Close Date:data:200"
        ,mfi.modified_by AS "Done By:data:200"
    FROM `tabmfi_loan_action` mfi
    WHERE mfi.workflow_state = 'Done'

    UNION

    SELECT credit.NAME AS "Name"
        ,credit.responsible AS "Responsible:data:200"
        ,credit.action_type AS "Action Type:data:200"
        ,credit.id_customer AS "Id_Customer"
        ,credit.workflow_state AS "Workflow:data:200"
        ,credit.close_date AS "Close Date:data:200"
        ,credit.modified_by AS "Done By:data:200"
    FROM `tabin_house_credit_action` credit
    WHERE credit.workflow_state = 'Done'

    UNION

    SELECT service.NAME AS "Name"
        ,service.responsible AS "Responsible:data:200"
        ,service.action_type AS "Action Type:data:200"
        ,service.id_customer AS "Id_Customer"
        ,service.workflow_state AS "Workflow:data:200"
        ,service.close_date AS "Close Date:data:200"
        ,service.modified_by AS "Done By:data:200"
    FROM `tabservices_planning` service
    WHERE service.workflow_state = 'Done'

    UNION

    SELECT account.NAME AS "Name"
        ,account.responsible AS "Responsible:data:200"
        ,account.action_type AS "Action Type:data:200"
        ,account.id_customer AS "Id_Customer"
        ,account.workflow_state AS "Workflow:data:200"
        ,account.close_date AS "Close Date:data:200"
        ,account.modified_by AS "Done By:data:200"
    FROM `tabaccounting_action` account
    WHERE account.workflow_state = 'Done'
) AS T
ORDER BY T.`Close Date:data:200` DESC
1
Arulkumar

追加のレイヤーSELECT * FROM (...)は必要ありません。これを行います

( SELECT ... ) -- without any ORDER BY
UNION
( SELECT ... ) -- without any ORDER BY
UNION
...
UNION
( SELECT ... ) -- without any ORDER BY
ORDER BY ...

括弧があるため、ORDER BYは、最後のUNIONではなく、SELECTの結果に属します。

同じスキーマを持つ複数のテーブルを持つことは通常「間違っています」。それらのテーブルを1つに結合することを検討してください。その後、UNIONsを取り除くことができます。

0
Rick James

外側のSELECT * FROMは、内側のクエリのORDER BYを尊重する必要はありません。

サブクエリ内のORDER BYは無効/不要であると見なされるため、他のRDBMSは実際にはこの状況でエラーになります。

修正するには、次の形式になるようにクエリを変更します。

SELECT * FROM (SELECT sale.name as "Name",
-- rest of subquery
ORDER BY account.close_date desc) tabaccounting_action
ORDER BY "Close Date:data:200" DESC

ああ、またUNIONSは状況に影響を与えます-誰かが画面上で病気になったように質問がフォーマットされたので、私はそれらを逃しました。

0
Philᵀᴹ