2017-10-10 78 views
0

我閱讀了一些答案,但找不到以下問題的正確答案。我有一個運行以下查詢:最大日期將不起作用,替代?

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'。

+0

請在你的問題編輯的代碼是絕對最低,顯示問題(提示:你不需要任何連接,只需要幾個colu mns來實現這一點)。 – Bohemian

回答

0

MAX適用於您指定的整個組。如果您希望MAX聚合爲每個獨特的cst_recno工作,那麼您需要僅由每個獨特的cst_recno進行分組。

+0

如果我僅通過cst_recno分組,它不起作用。它需要按select語句中的所有列進行分組,但具有集合函數(MAX(inv_trx_date)爲'Latest Transaction')的列) – klima

+0

它以什麼方式不起作用? – MBurnham

+0

消息8120,級別16,狀態1,行2 列'mb_membership.mbr_src_code'在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句中。 – klima

1

我想規範這個問題的答案是如下

with AllData as 
(
select ... from ... 
where ... 
) 
select * from allData ad1 
inner join 
(
    select pk1, pk2, pk<n>, max(MaxThing) MaxVal 
    from AllData 
    group by pk1, pk2, pk<n> 
) as ad2 
on (ad1.pk1=ad2.pk1 and ad1.pk2=ad2.pk2 and ad1.pk<n>=ad2.pk<n> 
and ad1.MaxThing=ad2.MaxVal) 

在你的情況cst_recno是PK和inv_trx_date是MaxThing

0

重複行的問題,是因爲你的CODEivd_amount_cp列包含記錄集中的非唯一值。如果他們都包含完全相同的信息,您的MAX函數將工作沒有任何問題。

您將不得不像其他人所建議的那樣使用公共表格表達式。假設您有一張包含特定CustomerID,訂單日期以及其他許多信息的表格。客戶可以在同一日期下訂單,無論出於何種原因,我們只對該客戶的最新訂單感興趣。但是,我們希望看到與該訂單相關的所有信息。

我們要做的第一件事是建立一個CTE來確定哪些訂單是最新:

/* Example table */ 
CREATE TABLE myOrders 
(
    OrderID int IDENTITY(1,1) 
    , CustomerID int 
    , OrderDate datetime 
    , ImportantInfo nvarchar(255) 
) 
; 

/* Some test data to work with */ 
INSERT INTO myOrders 
VALUES (
    1, '01-01-2017', 'We do not want this row' 
), (
    1, '01-02-2017', 'We do not want this row either' 
), (
    1, '01-10-2017', 'Getting closer, but not this one either' 
), (
    1, '01-10-2017', 'This is the one we want!' 
) 
; 

WITH myMaxOrder AS 
(
    SELECT CustomerID, MAX(OrderID) AS MaxOrderID 
    FROM myORders 
    GROUP BY CustomerID 
) 

一旦你確定任何你需要的MAX,你再簡單地用它來識別你的其他數據要檢索,使用CTE使用剛纔在CTE獲得的值加入回原始表:

SELECT 
    o.* 
FROM 
    MyOrders o 
    JOIN myMaxOrder o1 ON o.CustomerID = o1.CustomerID 
    AND o1.MaxOrderID = o.OrderID