我閱讀了一些答案,但找不到以下問題的正確答案。我有一個運行以下查詢:最大日期將不起作用,替代?
SELECT
mbr_src_code as 'C',
cst_recno as 'ID',
ind_first_name as 'FN',
ind_last_name as 'LN',
cst_org_name_dn as 'Company',
cst_ixo_title_dn as 'Title',
MAX(inv_trx_date) as 'Latest Transaction',
inv_add_user as 'User',
pyd_type as 'Type',
bat_code as 'Code',
mbr_add_user 'Add User',
mbr_rejoin_date as 'rejoin',
mbt_code,
adr_state as 'state',
adr_country as 'country',
ivd_amount_cp
FROM
mb_membership
JOIN
co_customer ON cst_key = mbr_cst_key AND mbr_delete_flag = 0
LEFT JOIN
mb_member_type ON mbr_mbt_key = mbt_key
LEFT JOIN
co_customer_x_address ON cxa_key = cst_cxa_key
LEFT JOIN
co_address ON cxa_adr_key = adr_key
LEFT JOIN
co_individual ON ind_cst_key = cst_key
LEFT JOIN
mb_membership_x_ac_invoice ON mxi_mbr_key = mbr_key
LEFT JOIN
ac_invoice ON mxi_inv_key = inv_key
LEFT JOIN
ac_invoice_detail ON ivd_inv_key = inv_key
LEFT JOIN
ac_payment_detail ON pyd_ivd_key = ivd_key
LEFT JOIN
ac_payment ON pyd_pay_key = pay_key
LEFT JOIN
ac_batch ON pay_bat_key = bat_key
LEFT JOIN
ac_payment_info ON pay_pin_key = pin_key
LEFT JOIN
co_customer_x_customer ON cxc_cst_key_1 = co_customer.cst_key
AND (cxc_end_date IS NULL OR DATEDIFF(dd, GETDATE(), cxc_end_date) >= 0)
AND cxc_rlt_code = 'Chapter Member'
LEFT JOIN
co_chapter ON cxc_cst_key_2 = chp_cst_key
WHERE
(mbr_src_code LIKE N'%1DMFY18%' OR mbr_src_code LIKE N'%2DMFY18%'
OR mbr_src_code LIKE N'%INPhoneFY18%' OR mbr_src_code LIKE N'%OBTMFY18%'
OR mbr_src_code LIKE N'%3DMFY18%')
AND cst_recno = '20239'
GROUP BY
mbr_key, mbr_src_code, cst_recno,
ind_first_name, ind_last_name, cst_org_name_dn, cst_ixo_title_dn,
inv_add_user, pyd_type, bat_code, mbr_add_user, mbr_rejoin_date,
mbt_code, adr_state, adr_country, pin_cc_number_display, pin_cc_cardholder_name,
ivd_amount_cp, chp_name
ORDER BY
ind_last_name
,我得到以下結果(樣品):
C ID FN LN Company Title Latest transaction User Type Code Add User rejoin mbt_code state country ivd_amount_cp
2DMFY18 20239 Gus Bauman Beveridge & Diamond Attorney 2013-09-23 00:00:00 Membership Renewal Payment 2013-09-23-ULI-USD-C-SP-01 ULI_Conversion NULL Associate Member DC UNITED STATES 430.00
2DMFY18 20239 Gus Bauman Beveridge & Diamond Attorney 2014-08-04 00:00:00 Membership Renewal Payment 2014-08-04-ULI-USD-C-SP-01 ULI_Conversion NULL Associate Member DC UNITED STATES 430.00
2DMFY18 20239 Gus Bauman Beveridge & Diamond Attorney 2015-09-02 00:00:00 Membership Renewal Payment 2015-09-02-ULI-USD-C-SP-02 ULI_Conversion NULL Associate Member DC UNITED STATES 440.00
2DMFY18 20239 Gus Bauman Beveridge & Diamond Attorney 2016-09-12 00:00:00 Membership Renewal Payment 2016-09-12-ULI-USD-C-SP-01 ULI_Conversion NULL Associate Member DC UNITED STATES 440.00
2DMFY18 20239 Gus Bauman Beveridge & Diamond Attorney 2017-09-22 00:00:00 Membership Renewal Payment 2017-09-22-ULI-USD-C-SP-01 ULI_Conversion NULL Associate Member DC UNITED STATES 440.00
所以我MAX功能不起作用(可能是因爲有不同的價值等欄目,就像在inv_trx_date中一樣),那麼最好的選擇是什麼?我想基本上接受整個查詢,並選擇MAX(inv_trx_date)作爲每個唯一cst_recno的'最新交易'作爲'ID'。
請在你的問題編輯的代碼是絕對最低,顯示問題(提示:你不需要任何連接,只需要幾個colu mns來實現這一點)。 – Bohemian