我们能写出比这更好的查询吗?减少性能问题

时间:2016-01-28 11:09:08

标签: sql sql-server sql-server-2008

select
(select OMH_RECV_BIC from sc_omh_m where sc_omh_trans_sno=103) as 'Sender\Receiver',
(select OMH_MSG_TRM_DT from sc_omh_m where sc_omh_trans_sno=103) as  'Send\Receivedate',
(select omh_msg_type from sc_omh_m where sc_omh_trans_sno=103) as 'Message Type',
(select omd_sfld_val from SC_OMD_T where OMD_FLD_TAG=20 and omd_sfld_ord=1 and omd_trans_sno=103) as  'Senders Reference',
(select omd_sfld_val from SC_OMD_T where OMD_FLD_TAG=50  and omd_sfld_ord=3 and omd_trans_sno=103) as 'Ordering Customer',
(select omd_sfld_val from SC_OMD_T where OMD_FLD_TAG=59 and omd_sfld_ord=3  and OMD_TRANS_SNO=103)  as 'Beneficiary Customer',
(select omd_sfld_val from SC_OMD_T where OMD_FLD_TAG=32 and omd_sfld_ord=2and omd_trans_sno=103) as 'Currency Code',
(select omd_sfld_val from SC_OMD_T where OMD_FLD_TAG=32 and omd_sfld_ord=1and omd_trans_sno=103)  as 'Date',
(select omd_sfld_val  from SC_OMD_T where OMD_FLD_TAG=32 and omd_sfld_ord=3and omd_trans_sno=103) as 'Value'

1 个答案:

答案 0 :(得分:0)

SELECT *
FROM (
    SELECT
        MAX(CASE WHEN OMD_FLD_TAG=20 and omd_sfld_ord=1 THEN omd_sfld_val END) as [Senders Reference],
        MAX(CASE WHEN OMD_FLD_TAG=50 and omd_sfld_ord=3 THEN omd_sfld_val END) as [Ordering Customer],
        MAX(CASE WHEN OMD_FLD_TAG=59 and omd_sfld_ord=3 THEN omd_sfld_val END) as [Beneficiary Customer],
        MAX(CASE WHEN OMD_FLD_TAG=32 and omd_sfld_ord=2 THEN omd_sfld_val END) as [Currency Code],
        MAX(CASE WHEN OMD_FLD_TAG=32 and omd_sfld_ord=1 THEN omd_sfld_val END) as [Date],
        MAX(CASE WHEN OMD_FLD_TAG=32 and omd_sfld_ord=3 THEN omd_sfld_val END) as Value
    FROM SC_OMD_T
    WHERE omd_trans_sno = 103
) t
JOIN (
    SELECT
        MAX(OMH_RECV_BIC) as [Sender\Receiver],
        MAX(OMH_MSG_TRM_DT) as [Send\Receivedate],
        MAX(omh_msg_type) as [Message Type]
    FROM sc_omh_m
    WHERE sc_omh_trans_sno = 103
) t2 ON 1=1