insert into MemberGroup (MemberId, MemberGroupId, MemberJoinDate, MemberExpiryDate)
SELECT DISTINCT
TransMemberId, 404, MAX(transaction_time),
dateadd(yy,1,MAX(transaction_time))
FROM
DataTrans
INNER JOIN
DataTransItem ON DataTransId = DataTransItemId
LEFT JOIN
MemberGroup ON MemberId = DataTransItemId
INNER JOIN
MemberMembership ON MemberId = MembershipId
INNER JOIN
LinkedMember ON MembershipId = LinkedMemberId
WHERE
membership_clubid = 1
AND TransMemberId IN (select LinkedMemberId from LinkedMember)
AND TransMemberId IN (select LinkedMemberId from LinkedMember
where MemberGroupId NOT IN (1, 2, 3, 4, 5))
AND TransMemberId IN (select MemberLinkedId from LinkedMember
where DataTransItemId = 2414)
GROUP BY
TransMemberId
为什么这会返回以下错误,即使它在insert语句中使用了distinct,也在最后将TransMemberId
分组以选择一个。
违反PRIMARY KEY约束' PK_MembershipGroup'。无法在对象' dbo.MemberGroup'中插入重复的密钥。重复键值为(FFASED487948KXWC1MH7,404)。
DataTransId
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
TransMemberId
F123TK1TSXX7
F123TK1TSXX7
F123TK1TSXX7
KS723423CLG8LSQ
KS723423CLG8LSQ
KS723423CLG8LSQ
KS723423CLG8LSQ
KS723423CLG8LSQ
48232423KXWC1MH7
48232423KXWC1MH8
48232423KXWC1MH9
F2P3XQTTRJ44232423
F2P3XQTTRJ44232424
F2P3XQTTRJ44232425
F2P3XQTTRJ44232426
F2P3XQTTRJ44232427
DataTransItemId
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
1.24322E+11
MembershipId
F123TK1TSXX7
F123TK1TSXX7
F123TK1TSXX7
KS723423CLG8LSQ
KS723423CLG8LSQ
KS723423CLG8LSQ
KS723423CLG8LSQ
KS723423CLG8LSQ
48232423KXWC1MH7
48232423KXWC1MH8
48232423KXWC1MH9
F2P3XQTTRJ44232423
F2P3XQTTRJ44232424
F2P3XQTTRJ44232425
F2P3XQTTRJ44232426
F2P3XQTTRJ44232427
LinkedMemberId
F123TK1TSXX7
F123TK1TSXX7
F123TK1TSXX7
KS723423CLG8LSQ
KS723423CLG8LSQ
KS723423CLG8LSQ
KS723423CLG8LSQ
KS723423CLG8LSQ
48232423KXWC1MH7
48232423KXWC1MH7
48232423KXWC1MH7
F2P3XQTTRJ44232423
F2P3XQTTRJ44232423
F2P3XQTTRJ44232423
F2P3XQTTRJ44232423
F2P3XQTTRJ44232423
MemberLinkedId
48KXWC1MH7ASGER
48KXWC1MH7ASGER
48KXWC1MH7ASGER
LKJDJX2RQ26Y7VD
LKJDJX2RQ26Y7VD
LKJDJX2RQ26Y7VD
LKJDJX2RQ26Y7VD
LKJDJX2RQ26Y7VD
F1TK1TSXX723FSDE
F1TK1TSXX723FSDE
F1TK1TSXX723FSDE
Z3VHVL846QQWER
Z3VHVL846QQWER
Z3VHVL846QQWER
Z3VHVL846QQWER
Z3VHVL846QQWER
答案 0 :(得分:0)
我认为您的查询可以是这样的:
insert into MemberGroup (MemberId, MemberGroupId, MemberJoinDate, MemberExpiryDate)
(SELECT TransMemberId , 404, MAX(transaction_time), dateadd(yy,1,MAX(transaction_time))
FROM DataTrans
INNER JOIN DataTransItem ON DataTransId = DataTransItemId
LEFT JOIN MemberGroup ON MemberId = DataTransItemId
INNER JOIN MemberMembership ON MemberId = MembershipId
INNER JOIN LinkedMember ON MembershipId = LinkedMemberId
WHERE membership_clubid = 1
AND TransMemberId IN (select LinkedMemberId from LinkedMember where MemberGroupId NOT IN (1, 2, 3, 4, 5))
AND TransMemberId IN (select MemberLinkedId from LinkedMember where DataTransItemId = 2414)
GROUP BY TransMemberId)
你可以检查;这个线索的结果:
WHERE TransMemberId IN (select LinkedMemberId from LinkedMember)
AND TransMemberId IN (select LinkedMemberId from LinkedMember where MemberGroupId NOT IN (1, 2, 3, 4, 5))
与此线索相同:
WHERE TransMemberId IN (select LinkedMemberId from LinkedMember where MemberGroupId NOT IN (1, 2, 3, 4, 5))