SQL Server - 删除具有不同排序的重复项

时间:2017-09-27 14:00:23

标签: sql sql-server duplicates

我有一张桌子,其中包含一起购买的物品对和配对发生的次数。

item_1    item_2    count
  123       234        5
  345       567       22
  567       345       22
  890       345        6

有些配对只是顺序不同(即第2行和第3行)。

有没有一种简单的方法可以减少这张桌子的费用?

3 个答案:

答案 0 :(得分:0)

您可以对一对相同的分类进行分类,并将其与两者中最小和最大的相比较。并选择其中一个。

select item_1,item_2,count 
from (select t.*
      ,row_number() over(partition by case when item_1<item_2 then item_1 else item_2 end,
                                      case when item_1>item_2 then item_1 else item_2 end,
                         count 
                         order by item_1) as rnum
      from tbl t
     ) t
where rnum=1

编辑:根据戈登的评论,如果只有当计数相同时必须删除重复项,请使用

Process:               Drum Hero [66758]
Path:                  /Volumes/VOLUME/*/Drum Hero.app/Contents/MacOS/Drum Hero
                                       Identifier:            MF.Drum-Hero
                                       Version:               1.0 (1)
                                       Code Type:             X86-64 (Native)
                                       Parent Process:        ??? [1]
                                       Responsible:           Drum Hero [66758]
                                       User ID:               1682957694

                                       Date/Time:             2017-09-13 10:33:16.940 -0400
                                       OS Version:            Mac OS X 10.12.6 (16G29)
                                       Report Version:        12
                                       Anonymous UUID:        B0637F3C-A9B3-E010-DB95-C3584B6B7A8F


                                       Time Awake Since Boot: 1100000 seconds

                                       System Integrity Protection: enabled

                                       Notes:                 Translocated Process

                                       Crashed Thread:        0  Dispatch queue: com.apple.main-thread

                                       Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
                                       Exception Codes:       KERN_INVALID_ADDRESS at 0x00007fef6bbffff8
                                       Exception Note:        EXC_CORPSE_NOTIFY

                                       Termination Signal:    Segmentation fault: 11
                                       Termination Reason:    Namespace SIGNAL, Code 0xb
                                       Terminating Process:   exc handler [0]

                                       VM Regions Near 0x7fef6bbffff8:
                                       Stack                  0000700002a04000-0000700002a86000 [  520K] rw-/rwx SM=COW  thread 8
                                       -->
                                       MALLOC_TINY            00007fef6bc00000-00007fef6c000000 [ 4096K] rw-/rwx SM=PRV

                                       Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
                                       0   libsystem_platform.dylib          0x00007fffe6b421c6 _platform_memmove$VARIANT$Nehalem + 486
                                       1   com.apple.SpriteKit               0x00007fffd781c405 std::__1::vector<SKCNode*, std::__1::allocator<SKCNode*> >::insert(std::__1::__wrap_iter<SKCNode* const*>, SKCNode* const&) + 131
                                       2   com.apple.SpriteKit               0x00007fffd781c326 SKCNode::insertChildAtIndex(SKCNode*, int) + 294
                                       3   com.apple.SpriteKit               0x00007fffd784ffd2 -[SKNode insertChild:atIndex:] + 294
                                       4   com.apple.SpriteKit               0x00007fffd784fe8b -[SKNode addChild:] + 68
                                       5   MF.Drum-Hero                      0x0000000105769cb9 0x105761000 + 36025
                                       6   MF.Drum-Hero                      0x0000000105769a48 0x105761000 + 35400
                                       7   MF.Drum-Hero                      0x0000000105769685 0x105761000 + 34437
                                       8   MF.Drum-Hero                      0x0000000105776724 0x105761000 + 87844
                                       9   com.apple.GameplayKit             0x00007fffd2a9975e -[GKStateMachine updateWithDeltaTime:] + 62
                                       10  MF.Drum-Hero                      0x000000010576ce44 0x105761000 + 48708
                                       11  com.apple.SpriteKit               0x00007fffd7823dd6 -[SKScene _update:] + 365
                                       12  com.apple.SpriteKit               0x00007fffd77cee6a -[SKView _update:] + 1156
                                       13  com.apple.SpriteKit               0x00007fffd77cb920 __51-[SKView _vsyncRenderForTime:preRender:postRender:]_block_invoke.385 + 285
                                       14  com.apple.SpriteKit               0x00007fffd77cad17 -[SKView _vsyncRenderForTime:preRender:postRender:] + 658
                                       15  com.apple.SpriteKit               0x00007fffd77ccc8e __29-[SKView setUpRenderCallback]_block_invoke + 291
                                       16  com.apple.SpriteKit               0x00007fffd784783f -[SKDisplayLink _callbackForNextFrame:] + 324
                                       17  com.apple.SpriteKit               0x00007fffd7847a42 ___Z22_cvDisplayLinkCallbackP15__CVDisplayLinkPK11CVTimeStampS3_yPyPv_block_invoke + 36
                                       18  libdispatch.dylib                 0x00007fffe6906524 _dispatch_call_block_and_release + 12
                                       19  libdispatch.dylib                 0x00007fffe68fd8fc _dispatch_client_callout + 8
                                       20  libdispatch.dylib                 0x00007fffe690aaac _dispatch_main_queue_callback_4CF + 925
                                       21  com.apple.CoreFoundation          0x00007fffd0c3fd69 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
                                       22  com.apple.CoreFoundation          0x00007fffd0c0104d __CFRunLoopRun + 2221
                                       23  com.apple.CoreFoundation          0x00007fffd0c00544 CFRunLoopRunSpecific + 420
                                       24  com.apple.HIToolbox               0x00007fffd015febc RunCurrentEventLoopInMode + 240
                                       25  com.apple.HIToolbox               0x00007fffd015fcf1 ReceiveNextEventCommon + 432
                                       26  com.apple.HIToolbox               0x00007fffd015fb26 _BlockUntilNextEventMatchingListInModeWithFilter + 71
                                       27  com.apple.AppKit                  0x00007fffce6f8a54 _DPSNextEvent + 1120
                                       28  com.apple.AppKit                  0x00007fffcee747ee -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2796
                                       29  com.apple.AppKit                  0x00007fffce6ed3db -[NSApplication run] + 926
                                       30  com.apple.AppKit                  0x00007fffce6b7e0e NSApplicationMain + 1237
                                       31  libdyld.dylib                     0x00007fffe6933235 start + 1

                                       Thread 1:: CVDisplayLink
                                       0   libsystem_kernel.dylib            0x00007fffe6a61bf2 __psynch_cvwait + 10
                                       1   libsystem_pthread.dylib           0x00007fffe6b4d833 _pthread_cond_wait + 769
                                       2   com.apple.CoreVideo               0x00007fffd22da977 CVDisplayLink::waitUntil(unsigned long long) + 233
                                       3   com.apple.CoreVideo               0x00007fffd22da762 CVDisplayLink::runIOThread() + 520
                                       4   libsystem_pthread.dylib           0x00007fffe6b4c93b _pthread_body + 180
                                       5   libsystem_pthread.dylib           0x00007fffe6b4c887 _pthread_start + 286
                                       6   libsystem_pthread.dylib           0x00007fffe6b4c08d thread_start + 13

                                       Thread 2:: com.apple.NSEventThread
                                       0   libsystem_kernel.dylib            0x00007fffe6a5a34a mach_msg_trap + 10
                                       1   libsystem_kernel.dylib            0x00007fffe6a59797 mach_msg + 55
                                       2   com.apple.CoreFoundation          0x00007fffd0c01874 __CFRunLoopServiceMachPort + 212
                                       3   com.apple.CoreFoundation          0x00007fffd0c00cf1 __CFRunLoopRun + 1361
                                       4   com.apple.CoreFoundation          0x00007fffd0c00544 CFRunLoopRunSpecific + 420
                                       5   com.apple.AppKit                  0x00007fffce845f02 _NSEventThread + 205
                                       6   libsystem_pthread.dylib           0x00007fffe6b4c93b _pthread_body + 180
                                       7   libsystem_pthread.dylib           0x00007fffe6b4c887 _pthread_start + 286
                                       8   libsystem_pthread.dylib           0x00007fffe6b4c08d thread_start + 13

                                       Thread 3:
                                       0   libsystem_kernel.dylib            0x00007fffe6a6244e __workq_kernreturn + 10
                                       1   libsystem_pthread.dylib           0x00007fffe6b4c621 _pthread_wqthread + 1426
                                       2   libsystem_pthread.dylib           0x00007fffe6b4c07d start_wqthread + 13

                                       Thread 4:
                                       0   libobjc.A.dylib                   0x00007fffe6042cdb search_method_list(method_list_t const*, objc_selector*) + 107
                                       1   libobjc.A.dylib                   0x00007fffe60450a5 lookUpImpOrForward + 383
                                       2   libobjc.A.dylib                   0x00007fffe6044a94 _objc_msgSend_uncached + 68
                                       3   com.apple.Foundation              0x00007fffd2615716 +[NSConcreteNotification newTempNotificationWithName:object:userInfo:] + 123
                                       4   com.apple.Foundation              0x00007fffd261565f -[NSNotificationCenter postNotificationName:object:userInfo:] + 42
                                       5   MF.Drum-Hero                      0x000000010576a413 0x105761000 + 37907
                                       6   com.apple.CoreFoundation          0x00007fffd0c1654c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
                                       7   com.apple.CoreFoundation          0x00007fffd0c1644b _CFXRegistrationPost + 427
                                       8   com.apple.CoreFoundation          0x00007fffd0c161b2 ___CFXNotificationPost_block_invoke + 50
                                       9   com.apple.CoreFoundation          0x00007fffd0bd4782 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 2018
                                       10  com.apple.CoreFoundation          0x00007fffd0bd376b _CFXNotificationPost + 667
                                       11  com.apple.Foundation              0x00007fffd2615677 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
                                       12  MF.Drum-Hero                      0x000000010576df60 0x105761000 + 53088
                                       13  MF.Drum-Hero                      0x000000010576de5f 0x105761000 + 52831
                                       14  MF.Drum-Hero                      0x000000010576dd34 0x105761000 + 52532
                                       15  com.apple.audio.midi.CoreMIDI     0x00000001057b6d5e LocalMIDIReceiverList::HandleMIDIIn(unsigned int, unsigned int, void*, MIDIPacketList const*) + 120
                                       16  com.apple.audio.midi.CoreMIDI     0x00000001057b6c12 MIDIProcess::RunMIDIInThread() + 210
                                       17  com.apple.audio.midi.CoreMIDI     0x00000001057c3a9a XThread::RunHelper(void*) + 10
                                       18  com.apple.audio.midi.CoreMIDI     0x00000001057c7053 CAPThread::Entry(CAPThread*) + 85
                                       19  libsystem_pthread.dylib           0x00007fffe6b4c93b _pthread_body + 180
                                       20  libsystem_pthread.dylib           0x00007fffe6b4c887 _pthread_start + 286
                                       21  libsystem_pthread.dylib           0x00007fffe6b4c08d thread_start + 13

                                       Thread 5:: com.apple.coreaudio.AQClient
                                       0   libsystem_kernel.dylib            0x00007fffe6a5a34a mach_msg_trap + 10
                                       1   libsystem_kernel.dylib            0x00007fffe6a59797 mach_msg + 55
                                       2   com.apple.CoreFoundation          0x00007fffd0c01874 __CFRunLoopServiceMachPort + 212
                                       3   com.apple.CoreFoundation          0x00007fffd0c00cf1 __CFRunLoopRun + 1361
                                       4   com.apple.CoreFoundation          0x00007fffd0c00544 CFRunLoopRunSpecific + 420
                                       5   com.apple.audio.toolbox.AudioToolbox    0x00007fffcf9d1899 GenericRunLoopThread::Entry(void*) + 189
                                       6   com.apple.audio.toolbox.AudioToolbox    0x00007fffcf9d172b CAPThread::Entry(CAPThread*) + 85
                                       7   libsystem_pthread.dylib           0x00007fffe6b4c93b _pthread_body + 180
                                       8   libsystem_pthread.dylib           0x00007fffe6b4c887 _pthread_start + 286
                                       9   libsystem_pthread.dylib           0x00007fffe6b4c08d thread_start + 13

                                       Thread 6:: AQConverterThread
                                       0   libsystem_kernel.dylib            0x00007fffe6a61bf2 __psynch_cvwait + 10
                                       1   libsystem_pthread.dylib           0x00007fffe6b4d7fa _pthread_cond_wait + 712
                                       2   com.apple.audio.toolbox.AudioToolbox    0x00007fffcf9ff256 CAGuard::Wait() + 52
                                       3   com.apple.audio.toolbox.AudioToolbox    0x00007fffcf9f9f0d AQConverterManager::AQConverterThread::Run() + 707
                                       4   com.apple.audio.toolbox.AudioToolbox    0x00007fffcf9f9c3f AQConverterManager::AQConverterThread::ConverterThreadEntry(void*) + 9
                                       5   com.apple.audio.toolbox.AudioToolbox    0x00007fffcf9d172b CAPThread::Entry(CAPThread*) + 85
                                       6   libsystem_pthread.dylib           0x00007fffe6b4c93b _pthread_body + 180
                                       7   libsystem_pthread.dylib           0x00007fffe6b4c887 _pthread_start + 286
                                       8   libsystem_pthread.dylib           0x00007fffe6b4c08d thread_start + 13

                                       Thread 7:: AudioStreamerImpl::sIOWorkerProcess
                                       0   libsystem_kernel.dylib            0x00007fffe6a5a386 semaphore_wait_trap + 10
                                       1   libdispatch.dylib                 0x00007fffe6915a77 _os_semaphore_wait + 16
                                       2   libdispatch.dylib                 0x00007fffe6906891 _dispatch_semaphore_wait_slow + 103
                                       3   com.apple.audio.units.Components    0x0000000105e8ce25 CASemaphore::Wait() + 23
                                       4   com.apple.audio.units.Components    0x0000000105e8b94f AudioStreamerImpl::sIOWorkerProcess(void*) + 735
                                       5   com.apple.audio.units.Components    0x0000000105ed16e5 CAPThread::Entry(CAPThread*) + 85
                                       6   libsystem_pthread.dylib           0x00007fffe6b4c93b _pthread_body + 180
                                       7   libsystem_pthread.dylib           0x00007fffe6b4c887 _pthread_start + 286
                                       8   libsystem_pthread.dylib           0x00007fffe6b4c08d thread_start + 13

                                       Thread 8:: com.apple.audio.IOThread.client
                                       0   libsystem_kernel.dylib            0x00007fffe6a5a34a mach_msg_trap + 10
                                       1   libsystem_kernel.dylib            0x00007fffe6a59797 mach_msg + 55
                                       2   com.apple.audio.CoreAudio         0x00007fffd0769935 HALB_MachPort::SendMessageWithReply(unsigned int, unsigned int, unsigned int, unsigned int, mach_msg_header_t*, bool, unsigned int) + 127
                                       3   com.apple.audio.CoreAudio         0x00007fffd07698a6 HALB_MachPort::SendSimpleMessageWithSimpleReply(unsigned int, unsigned int, int, int&, bool, unsigned int) + 42
                                       4   com.apple.audio.CoreAudio         0x00007fffd07668d0 HALC_ProxyIOContext::IOWorkLoop() + 1196
                                       5   com.apple.audio.CoreAudio         0x00007fffd0766264 HALC_ProxyIOContext::IOThreadEntry(void*) + 128
                                       6   com.apple.audio.CoreAudio         0x00007fffd0765fa3 HALB_IOThread::Entry(void*) + 75
                                       7   libsystem_pthread.dylib           0x00007fffe6b4c93b _pthread_body + 180
                                       8   libsystem_pthread.dylib           0x00007fffe6b4c887 _pthread_start + 286
                                       9   libsystem_pthread.dylib           0x00007fffe6b4c08d thread_start + 13

                                       Thread 9:
                                       0   libsystem_kernel.dylib            0x00007fffe6a6244e __workq_kernreturn + 10
                                       1   libsystem_pthread.dylib           0x00007fffe6b4c48e _pthread_wqthread + 1023
                                       2   libsystem_pthread.dylib           0x00007fffe6b4c07d start_wqthread + 13

                                       Thread 10:
                                       0   libsystem_pthread.dylib           0x00007fffe6b4c070 start_wqthread + 0
                                       1   ???                               0x0000700002771ad0 0 + 123145343670992

                                       Thread 11:: com.apple.audio.IOThread.client
                                       0   libsystem_kernel.dylib            0x00007fffe6a5a34a mach_msg_trap + 10
                                       1   libsystem_kernel.dylib            0x00007fffe6a59797 mach_msg + 55
                                       2   com.apple.audio.CoreAudio         0x00007fffd0769935 HALB_MachPort::SendMessageWithReply(unsigned int, unsigned int, unsigned int, unsigned int, mach_msg_header_t*, bool, unsigned int) + 127
                                       3   com.apple.audio.CoreAudio         0x00007fffd07698a6 HALB_MachPort::SendSimpleMessageWithSimpleReply(unsigned int, unsigned int, int, int&, bool, unsigned int) + 42
                                       4   com.apple.audio.CoreAudio         0x00007fffd07668d0 HALC_ProxyIOContext::IOWorkLoop() + 1196
                                       5   com.apple.audio.CoreAudio         0x00007fffd0766264 HALC_ProxyIOContext::IOThreadEntry(void*) + 128
                                       6   com.apple.audio.CoreAudio         0x00007fffd0765fa3 HALB_IOThread::Entry(void*) + 75
                                       7   libsystem_pthread.dylib           0x00007fffe6b4c93b _pthread_body + 180
                                       8   libsystem_pthread.dylib           0x00007fffe6b4c887 _pthread_start + 286
                                       9   libsystem_pthread.dylib           0x00007fffe6b4c08d thread_start + 13

                                       Thread 0 crashed with X86 Thread State (64-bit):
                                       rax: 0x00007fef6bd94fd8  rbx: 0x00007fef6bd94fd8  rcx: 0x0000000000000008  rdx: 0xffffffffffe6b028
                                       rdi: 0x00007fef6bc00040  rsi: 0x00007fef6bc00038  rbp: 0x00007fff5a49c860  rsp: 0x00007fff5a49c860
                                       r8: 0x0000000000000040   r9: 0x00007fffec5954f0  r10: 0x00007fffecee1b60  r11: 0x00007fef6bd94fc8
                                       r12: 0x00007fef720cd3e8  r13: 0x00007fef6bd95090  r14: 0x00007fef6bd94fd0  r15: 0x00007fff5a49c8f0
                                       rip: 0x00007fffe6b421c6  rfl: 0x0000000000010202  cr2: 0x00007fef6bbffff8

                                       Logical CPU:     2
                                       Error Code:      0x00000004
                                       Trap Number:     14

答案 1 :(得分:0)

如果&#34; dups&#34;只能在任一方向出现一次,那么方便的方法是:

select t.*
from t
where t.item_1 <= t.item_2
union all
select t.*
from t t
where t.item_1 > t.item2 and
      not exists (select 1
                  from t t2
                  where t2.item_1 = t.item_2 and t.item_2 = t.item_1 and t2.count = t.count
                 );

答案 2 :(得分:0)

您可以使用此脚本。

DECLARE @T TABLE (item_1  INT, item_2  INT , [count] INT)
INSERT INTO @T
VALUES
  (123       ,234,        5),
  (345       ,567,       22),
  (567       ,345,       22),
  (890       ,345,        6)

;WITH BASE AS
(
    SELECT RN = ROW_NUMBER() OVER(ORDER BY item_1),  * FROM @T
)
SELECT T1.item_1, T1.item_2, T1.count FROM BASE T1
    OUTER APPLY (SELECT TOP 1 * 
                    FROM BASE T2 
                 WHERE T2.RN > T1.RN AND T1.item_1 = T2.item_2 AND T1.item_2 = T2.item_1) X
WHERE X.RN IS NULL

结果

item_1      item_2      count
----------- ----------- -----------
123         234         5
567         345         22
890         345         6