1

Given the following table:

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

I get the following results when I do a Count on the Chain column:

123      2
124      1
125      2
126      1
127      1

Is it possible to group Chain 123 and 124 so they're counted together? Also group 125 and 126? The modified results would look like this:

123/124  3
125/126  3
127      1

My SQL looks like this:

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;

Thank you!

4

5 回答 5

3

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

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

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

我更喜欢嵌套 IIF 语句之类的东西,因为它们很难调试,而且将来您可能会有额外的分组,这些分组很容易添加到表中,并且新分组会自动出现在查询中.

于 2013-04-11T15:48:00.910 回答
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);
于 2013-04-11T15:45:17.923 回答
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

于 2013-04-11T15:39:57.047 回答
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
于 2013-04-11T16:02:30.353 回答
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
于 2013-04-11T16:33:28.247 回答