2014-06-06 84 views
0

我有以下查詢:SQL服務器:合併同類行

SELECT DISTINCT [WL].[Id] 
    ,[WL].[UserId] 
    ,[WL].[DIF] 
    ,[WL].[MW] 
    ,[WL].[Notes] 
    ,[WL].[WDate] 
    ,[WL].[CB] 
    ,[WL].[MPH] 
    ,[U].[Id] 
    ,[U].[UserName] 
    ,[U].[We] 
    ,[U].[SLength] 
    ,[U].[UP] 
    ,[U].[PU] 
    ,[U].[ANumber] 
    ,[U].[G_CK] 
FROM 
    [Wsite].[dbo].[WLog] as WL 
INNER JOIN 
    [Wsite].[dbo].[Users] AS U ON [U].[Id] = [WL].[UserId] 
WHERE 
    [WL].[WDate] >= CONVERT(datetime, '2012-01-01 00:00:00', 120) 
    AND [WL].[WDate] <= CONVERT(datetime, GETDATE(), 120) 
GROUP BY 
    [WL].[UserId] 

而我得到的錯誤是:

列「Wsite.dbo.WLog.Id」是在選擇無效 列表,因爲它不包含在聚合函數或 GROUP BY子句中。

我想要的只是在列表中有多個UserID的情況下合併數據。

作爲一個例子:

Id | UserId | .... | Id | UserName | SLength | .... 
5843| 99304 | .... | 99304| Bob Barker | 14  | .... 
5844| 06300 | .... | 06300| Dean Martin | 104  | .... 
5845| 99304 | .... | 99304| Bob Barker | 8  | .... 
5846| 99304 | .... | 99304| Bob Barker | 11  | .... 
5847| 7699 | .... | 7699 | John Doe | 0  | .... 

因此,它應該是這樣的:

Id | UserId | .... | Id | UserName | SLength | .... 
5843| 99304 | .... | 99304| Bob Barker | 33  | .... 
5844| 06300 | .... | 06300| Dean Martin | 104  | .... 
5847| 7699 | .... | 7699 | John Doe | 0  | .... 

注意鮑勃巴克的SLength合併(14 + 8 + 11 = 33)。

任何幫助將是偉大的!謝謝!

UPDATE

SELECT DISTINCT 
    MIN([WL].[Id]) AS [WLID] 
    ,MIN([WL].[UserId]) AS [WLUD] 
    ,SUM([WL].[DIF]) AS [DIF] 
    ,SUM([WL].[MW]) AS [MW] 
    ,[WL].[Notes] 
    ,[WL].[WDate] 
    ,SUM([WL].[CB]) AS [CB] 
    ,SUM([WL].[MPH]) AS [MPH] 
    ,MIN([U].[Id] AS [UID] 
    ,MIN([U].[UserName] AS [UUserName] 
    ,[U].[We] 
    ,SUM([U].[SL]) AS [SL] 
    ,[U].[UP] 
    ,[U].[PU] 
    ,[U].[ANumber] 
    ,[U].[G_CK] 
    FROM [Wsite].[dbo].WLog as WL 
INNER JOIN [Wsite].[dbo].[Users] AS U 
    ON [U].[Id] = [WL].[UserId] 
WHERE [WL].[WDate] >= CONVERT(datetime, '2012-01-01 00:00:00', 120) 
    AND [WL].[WDate] <= CONVERT(datetime, GETDATE(), 120) 
GROUP BY 
    [WL].[Notes] 
    ,[WL].[WDate] 
    ,[U].[We] 
    ,[U].[UP] 
    ,[U].[PU] 
    ,[U].[ANumber] 
    ,[U].[G_CK] 
ORDER BY [U].[UserName] DESC 
+0

爲所有其它領域也會包含除'Id'場和'SLength'字段相同的數據的? – Linger

回答

1

當使用GROUP BY,則意味着要相似的元件組在一起並呈現某種聚合要顯示的其餘項目的(COUNT,SUM,MIN,等)。 Becaus eof this,如果你在選擇列表中包含[WL]。[Id],你將基本上得到沒有任何聚合的完整選擇(因爲[WL]。[Id]是唯一的)。

你需要做類似如下:

SELECT MIN([WL].[Id]) AS ID 
    ,[WL].[UserId] 
    ,[U].[Id] 
    ,[U].[UserName] 
    ,SUM([SLength]) AS SLength 
FROM [Wsite].[dbo].[WLog] as WL 
INNER JOIN [Wsite].[dbo].[Users] AS U 
     ON [U].[Id] = [WL].[UserId] 
    WHERE [WL].[WDate] >= CONVERT(datetime, '2012-01-01 00:00:00', 120) 
     AND [WL].[WDate] <= CONVERT(datetime, GETDATE(), 120) 
    GROUP BY [WL].[UserId] 
    ,[U].[Id] 
    ,[U].[UserName] 

(爲簡便起見,我並沒有包括所有字段)。另外請注意,因爲[WL]。[Id]似乎是唯一的,所以在這種情況下,除非你想顯示它的第一個出現(然後你可以添加MIN([WL]。[Id]),這是沒有意義的)到SELECT列表)。

+0

這似乎並沒有把它們結合起來? – StealthRT

+0

哪個不合並哪裏?請提供詳情。 –

+0

UserName仍然不止一次顯示。它沒有被組合在一起。 – StealthRT

0

如果您使用的是GROUP BY,那麼是不是在分組的列必須以某種方式(SUMMINMAX等)

儘量彙總這個:

SELECT 
    [WL].[Id] 
    ,[WL].[UserId] 
    ,[WL].[DIF] 
    ,[WL].[MW] 
    ,[WL].[Notes] 
    ,[WL].[WDate] 
    ,[WL].[CB] 
    ,[WL].[MPH] 
    ,Sum ([U].[SLength]) SLength 
FROM [Wsite].[dbo].[WLog] as WL 
INNER JOIN [Wsite].[dbo].[Users] AS U 
    ON [U].[Id] = [WL].[UserId] 
WHERE [WL].[WDate] >= CONVERT(datetime, '2012-01-01 00:00:00', 120) 
    AND [WL].[WDate] <= CONVERT(datetime, GETDATE(), 120) 
GROUP BY 
    [WL].[Id] 
    ,[WL].[UserId] 
    ,[WL].[DIF] 
    ,[WL].[MW] 
    ,[WL].[Notes] 
    ,[WL].[WDate] 
    ,[WL].[CB] 
    ,[WL].[MPH] 

這裏是你的聚合函數可以使用:

http://msdn.microsoft.com/en-us/library/ms173454.aspx

+0

就像@Dave Jemiison一樣,用戶名仍然不止一次出現。它沒有被組合在一起 – StealthRT

+0

我拿出了一些列並編輯了查詢。嘗試一下。 –

-1

您使用的GROUP BY是不正確的。 SELECT中使用的所有列應該在GROUP BY子句中,或者應該是聚合函數。
您的查詢應該如下所示。

SELECT DISTINCT MIN([WL].[Id]) [Id] 
    ,[WL].[UserId] 
    ,[WL].[DIF] 
    ,[WL].[MW] 
    ,[WL].[Notes] 
    ,[WL].[WDate] 
    ,[WL].[CB] 
    ,[WL].[MPH] 
    ,[U].[Id] 
    ,[U].[UserName] 
    ,[U].[We] 
    ,SUM([U].[SLength]) SLength 
    ,[U].[UP] 
    ,[U].[PU] 
    ,[U].[ANumber] 
    ,[U].[G_CK] 
FROM [Wsite].[dbo].[WLog] as WL 
    INNER JOIN [Wsite].[dbo].[Users] AS U 
     ON [U].[Id] = [WL].[UserId] 
WHERE [WL].[WDate] >= CONVERT(datetime, '2012-01-01 00:00:00', 120) 
     AND [WL].[WDate] <= CONVERT(datetime, GETDATE(), 120) 
GROUP BY [WL].[UserId] 
    ,[WL].[DIF] 
    ,[WL].[MW] 
    ,[WL].[Notes] 
    ,[WL].[WDate] 
    ,[WL].[CB] 
    ,[WL].[MPH] 
    ,[U].[Id] 
    ,[U].[UserName] 
    ,[U].[We] 
    ,[U].[UP] 
    ,[U].[PU] 
    ,[U].[ANumber] 
    ,[U].[G_CK] 
+0

它看起來像[WL]。[Id]是一個標識字段,因此包括在選擇列表中將導致所有結果不顯示聚合。 –

+0

@DaveJemison謝謝,修復它。 – user2989408

0

不確定,但這可能適用於您的情況。

SELECT DISTINCT [WL].[Id] 
    ,[WL].[UserId] 
    ,[WL].[DIF] 
    ,[WL].[MW] 
    ,[WL].[Notes] 
    ,[WL].[WDate] 
    ,[WL].[CB] 
    ,[WL].[MPH] 
    ,[U].[Id] 
    ,[U].[UserName] 
    ,[U].[We] 
    ,SUM([U].[SLength]) OVER (PARTITION BY [WL].[UserId]) 
    ,[U].[UP] 
    ,[U].[PU] 
    ,[U].[ANumber] 
    ,[U].[G_CK] 
      FROM [Wsite].[dbo].[WLog] as WL 
    INNER JOIN [Wsite].[dbo].[Users] AS U 
      ON [U].[Id] = [WL].[UserId] 
     WHERE [WL].[WDate] >= CONVERT(datetime, '2012-01-01 00:00:00', 120) 
      AND [WL].[WDate] <= CONVERT(datetime, GETDATE(), 120)