2014-10-16 3 views
0

많은 검색을 수행하여 직접 작성하려고했습니다. 나는 중복 된 항목의 수를 세는 SQL 쿼리에 쓰고 싶다. 그리고 중복 된 항목의 수를 첫 번째 항목의 수량에 더한다. 이것은 내가하고있는 게임을위한 것입니다.중복 된 레코드 수를 SQL로 계산 한 다음 레코드 수에 중복 레코드 수를 추가하고 나머지를 삭제합니다.

첫 번째 항목은 표시 할 첫 번째 CustomerID, ItemID, Var1입니다. 우리는이 모든 것을 3 개의 필드로 정렬 할 필요가 있기 때문입니다.

중복 된 항목은 ItemID와 Var1이 동일한 경우 중복 된 레코드 수량을 첫 번째 레코드에 추가하려는 경우에 사용합니다. 인벤토리 ID는 기본 키이므로이를 알지 못합니다. 우리는 열이

CREATE TABLE [dbo].[UsersInventory] 
    ([InventoryID] bigint NOT NULL IDENTITY(1,1), 
     [CustomerID] int NOT NULL , 
     [CharID] int NOT NULL DEFAULT ((0)) , 
     [BackpackSlot] int NOT NULL DEFAULT ((0)), 
     [ItemID] int NOT NULL, 
     [LeasedUntil] datetime NOT NULL, 
     [Quantity] int NOT NULL DEFAULT ((1)), 
     [Var1] int NOT NULL DEFAULT ((-1)), 
     [Var2] int NOT NULL DEFAULT ((-1)), 
     [Durability] int NULL) 
+1

을 남겨 항목의 수량을 갱신 할 필요가 본질적으로

? 또한, 어떤 RDBMS를 사용하고 있습니까? – Mureinik

+0

"복제본"이란 무엇을 의미하는지 지정해야합니다. 테이블에있는 열을 표시하는 것만이 아닙니다. –

+0

Navicat Premium을 사용하고 있습니다. 여기서 구조 '코드 TABLE [DBO]. UsersInventory ( 는 [InventoryID] NOT NULL IDENTITY (1,1 BIGINT) [CustomerID를]가 [CharID]는 INT, NULL NOT int로 NOT NULL DEFAULT 생성 (인 (0)), [BackpackSlot]을 ((0)), [항목 ID]가 는 [LeasedUntil] [수량] NOT NULL DEFAULT를 날짜 NULL NOT INT, NULL NOT int로 NOT NULL DEFAULT에서 INT ((1)), [VAR1] NOT NULL DEFAULT를 int로 ((-1)), [변수 2를 대입 할 때 변수]가 NOT NULL DEFAULT를 int로 ((-1)), [내구성] INT NULL의 )' – Zonfire

답변

0

을 소리 이하. 당신이 먼저 모든 고유 항목의 합계를 계산 모든 중복을 제거하고 마지막으로 당신이 당신의 테이블의 구조를 공유하시기 바랍니다 수

-- Temporary table to hold the sums 
declare @tempSum table 
     (CustomerID int, 
     ItemID int, 
     Var1 int, 
     InventoryID int, 
     Quantity int) 

-- Get the sum of quantity per Customer, Item and Var1 
-- This also get the first InvetoryID, assuming that the smallest number is the first one 
insert @tempSum 
    (CustomerID, 
     ItemID, 
     Var1, 
     InventoryID, 
     Quantity) 
select CustomerID, 
     ItemID, 
     Var1, 
     min(InventoryID), 
     sum(Quantity) 
    from UsersInventory 
group by CustomerID, 
      ItemID, 
      Var1 

begin transaction 
    -- Remove duplicate items 
    delete usi 
    from UsersInventory usi 
      join 
     @tempSum  tmp on tmp.CustomerID = usi.CustomerID 
          and tmp.ItemID  = usi.ItemID 
          and tmp.Var1   = usi.Var1 
          and tmp.InventoryID <> usi.InventoryID -- This ensures all items get deleted that are didn't mark as our firsts 

    -- Update the quantity that we summed earlier 
    update usi 
    set Quantity = tmp.Quantity 
    from UsersInventory usi 
      join 
     @tempSum  tmp on tmp.CustomerID = usi.CustomerID 
          and tmp.ItemID  = usi.ItemID 
          and tmp.Var1  = usi.Var1 
          and tmp.InventoryID = usi.InventoryID 

commit transaction 
+0

정말 고마워요, 제가 찾고 있던 곳입니다. :) +1 – Zonfire

+0

그래, 내가 좋아하는 경우, 하나의 문제가 스태킹하지 않는 Var2를 제외하고, 그것은 -1 또는 어떤 ID라도 게임이 로그인하면 그것을 검사하기 때문에 중요하지 않습니다. 어떻게 다른 아이템들과도 쌓을 수 있을까요[email protected] – Zonfire

+0

항목 사이의 수량 분할의 일부이기도하다는 것을 의미하는 경우 수량 합계를 계산하는 데 사용되는 열 목록에 추가해야합니다. –

0

이 시도됩니다

다른 CustomerIDs

이 그 중 하나 기록을 추가 할 수 없습니다 : 당신은 코드와 같은 뭔가가 필요 같은 문제의 당신의 묘사에서

create table T1 as (
select t1.*,count(*)-1 as records_quantity from T t1 group by t1.CustomerID,t1.ItemID,t1.Var1); 

delete from T; 

insert into T (select * from T1);