是否可以将不同的项目分组并将它们统计在一起?

时间:2013-04-11 15:34:42

标签: sql ms-access

鉴于下表:

Chain    Name
123      Company 1
124      Other Company 1
123      Whatever Company
125      This One
126      That One
125      Another One
127      Last One

当我在Chain列上执行Count时,我得到以下结果:

123      2
124      1
125      2
126      1
127      1

是否可以对Chain 123和124进行分组,以便将它们统计在一起?同组125和126?修改后的结果如下所示:

123/124  3
125/126  3
127      1

我的SQL看起来像这样:

SELECT Table1.Chain, Count(*) as [Count]
FROM Table1 LEFT JOIN Table2 on Table1.Chain = Table2.Chain
WHERE (((Table1.Chain) IN (Table2.Chain)))
GROUP BY Table1.Chain
ORDER BY Table1.Chain;

谢谢!

5 个答案:

答案 0 :(得分:3)

根据您的需要,这可能有点像黑客,但我可能会添加一个表来存储您正在寻找的Chain和ChainGroup。像这样:

Chain  ChainGroup
123      123/124
124      123/124
125      125/126
126      125/126
127      127/128

然后,在查询中,我将加入此表,而不是按链分组,我将按ChainGroup分组。

我更喜欢这样的东西,比如嵌套的IIF语句,因为那些调试非常困难,而且你可能会在将来有额外的分组,这些分组很容易添加到表中,并且会自动显示新的分组在查询中。

答案 1 :(得分:2)

是的,你可以:

SELECT min(Table1.Chain) & '/' & max(Table1.Chain) as chain, Count(*) as [Count]
FROM Table1 LEFT JOIN Table2 on Table1.Chain = Table2.Chain
WHERE (((Table1.Chain) IN (Table2.Chain)))
GROUP BY int((Table1.Chain-1)/2)
ORDER BY min(Table1.Chain);

答案 2 :(得分:1)

您可以使用嵌套的Iif语句。希望我的所有括号都在下面! : - )

SELECT Iif(Table1.Chain="123", "123/124",
         Iif(Table1.Chain="124", "123/124",
           Iif(Table1.Chain="125", "125/126",
             Iif(Table1.Chain="126", "125/126", Table1.Chain)))) as [Chain]
 , Count(*) as [Count]
FROM Table1 LEFT JOIN Table2 on Table1.Chain = Table2.Chain
WHERE (((Table1.Chain) IN (Table2.Chain)))
GROUP BY Iif(Table1.Chain="123", "123/124",
           Iif(Table1.Chain="124", "123/124",
             Iif(Table1.Chain="125", "125/126",
               Iif(Table1.Chain="126", "125/126", Table1.Chain))))
ORDER BY Table1.Chain;

如果您不想在查询中将其写入两次,也可以将case语句移动到from子句中的子查询或公用表表达式中。

答案 3 :(得分:1)

考虑如下:

SELECT
chain_group, COUNT(*) FROM (
SELECT 
Table1.Chain, 
switch(Table1.Chain IN("123","124"), "123/124",
Table1.Chain IN("125","126"),"125/126",
Table1.Chain) AS chain_group
FROM 
Table1 INNER JOIN 
Table2 ON 
Table1.Chain = Table2.Chain) t
GROUP BY chain_group
ORDER BY chain_group

答案 4 :(得分:0)

您可以看到以下示例 -----制作主表

CREATE TABLE #test 
( id int , Name varchar(100))

INSERT #test(id,Name)
values (123,'Company 1'),
(124,'Other Company 1'),
(123,      'Whatever Company'),
(125,      'This One'),
(126 ,     'That One'),
(125,      'Another One'),
(127,      'Last One')

CREATE TABLE #temp
(rowID INT IDENTITY(1,1) , ID INT ,cnt INT )

CREATE TABLE #tempResult
(ID VARCHAR(20) ,cnt INT )

INSERT INTO #temp(ID,cnt)
SELECT  ID ,COUNT(1) cnt FROM  #test GROUP BY ID 

DECLARE @rowCnt INT , @TotalCnt INT , @even INT , @odd INT , 
@idNum VARCHAR(20) , @valueCnt INT , @inStart INT = 1

SET @rowCnt  = 1
SET @even = 1 
SET @odd = 2

SELECT @TotalCnt = COUNT(1) FROM #temp

WHILE @rowCnt <= @TotalCnt
BEGIN
    SET @inStart = 1
    SET @odd = @rowCnt 
    SET @even = @rowCnt + 1
    SET @idNum = ''
    SET @valueCnt = 0

    WHILE @inStart <= 2
    BEGIN

    IF @inStart = 1
    Begin
     SELECT @idNum = Convert(VARCHAR(5),ID) , @valueCnt = cnt 
     FROM #temp WHERE rowID = @odd
    End 
    ELSE
    BEGIN
    SELECT @idNum = @idNum + '/' + Convert(VARCHAR(5),ID) , @valueCnt = @valueCnt  + cnt 
FROM #temp WHERE rowID = @even  
    END
        SET @inStart = @inStart + 1
    END
    INSERT INTO #tempResult (ID, Cnt) 
    VALUES (@idNum,@valueCnt)

    SET @rowCnt = @rowCnt + 2
END

SELECT *
FROM #tempResult