我阅读了一些答案,但未能找到以下问题的正确答案。我有以下运行查询:
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 :(得分: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
答案 1 :(得分:0)
MAX适用于您指定的整个组。如果您希望MAX汇总适用于每个唯一cst_recno
,那么您只需要按每个唯一cst_recno
进行分组。
答案 2 :(得分:0)
重复行的问题是因为您的CODE
和ivd_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