SQL Server 2018如何在不同的栏中分组信息

时间:2018-09-24 14:44:03

标签: sql sql-server group-by

我有一个表格(这种格式非常混乱)。这是表中间列的快照,这就是为什么数据中有大孔(其他列中有这些行的东西)的原因。

+---------------+--------------------+--------------+--------------+
|    upsell1    |      upsell2       |   upsell3    |   upsell4    |
+---------------+--------------------+--------------+--------------+
|               |                    | Car Kit      | Scented Tabs |
|               |                    |              |              |
|               |                    |              |              |
|               | Fiters             |              |              |
| NULL          | NULL               | NULL         | NULL         |
|               |                    |              |              |
|               | Car Kit            | Scented Tabs |              |
|               |                    |              |              |
|               |                    |              |              |

+---------------+--------------------+--------------+--------------+

我希望获得一个输出,该输出对每个产品进行分组和计数,无论它出现在哪一列中。

+--------------+---------------+
|   Product    | Product Count |
+--------------+---------------+
| Car Kit      |             2 |
| Scented Tabs |             2 |
| Fiters       |             1 |
+--------------+---------------+

通常,如果所有内容都在同一列中,那么使用group by命令将X作为第1列并将count(X)作为第2列就足够容易了,但是我正在空白中获取这些完全不同的信息正确地分组在一起。

注意:我无法更改表的结构(这让我非常恼火)

4 个答案:

答案 0 :(得分:3)

我可以使用apply

select v.upsell, count(*)
from t cross apply
     (values (upsell1), (upsell2), (upsell3), (upsell4)) v(upsell)
where v.upsell is not null
group by v.upsell;

您的问题尚不清楚空白是什么。您可能想要:

where v.upsell is not null and v.upsell <> ''

甚至:

where v.upsell is not null and ltrim(rtrim(v.upsell)) <> ''

答案 1 :(得分:1)

假设您无法控制可怕的架构...

with CTE as
(
select upsell1 as product, 1 as counter
from MyTable
where upsell1 is not null

union all
select upsell2, 1
from MyTable
where upsell2 is not null

union all
select upsell3, 1
from MyTable
where upsell3 is not null

union all
select upsell4, 1
from MyTable
where upsell4 is not null
)
select product, sum(counter)
from CTE
group by product

答案 2 :(得分:0)

尝试一下。

    declare @test table (
    upsell1 varchar(100),
    upsell2 varchar(100),       
    upsell3 varchar(100),
    upsell4 varchar(100)

)
insert into @test values ('','','Car Kit','Scented Tabs')
insert into @test values ('','','','')
insert into @test values ('','','','')
insert into @test values ('','Fiters','','')
insert into @test values (null,null,null,null)
insert into @test values ('','','','')
insert into @test values ('','Car Kit','Scented Tabs','')
insert into @test values ('','','','')
insert into @test values ('','','','')

select 
    abc,
    count(*) 
from (
select *
from @test) as src
unpivot (
abc for anyThing in (upsell1,upsell2,upsell3,upsell4)
) as ttt
group by abc

答案 3 :(得分:0)

由于没有类型假设,因此我将选择UNPIVOT的安全方法:

SELECT * INTO #t FROM (VALUES
    ('','','Car Kit','Scented Tabs'),
    ('','','',''),
    ('','','',''),
    ('','Fiters','',''),
    (null,null,null,null),
    ('','','',''),
    ('','Car Kit','Scented Tabs',''),
    ('','','',''),
    ('','','','')
) T(upsell1,upsell2,upsell3,upsell4)

SELECT Product, COUNT(Product) [Product Count] FROM
(
    SELECT
        CAST(upsell1 AS nvarchar(MAX)) upsell1,
        CAST(upsell2 AS nvarchar(MAX)) upsell2,
        CAST(upsell3 AS nvarchar(MAX)) upsell3,
        CAST(upsell4 AS nvarchar(MAX)) upsell4
    FROM #t
) T
UNPIVOT (Product for X IN (upsell1,upsell2,upsell3,upsell4)) P
WHERE Product != ''
GROUP BY Product

结果

Product         Product Count
--------------- -------------
Car Kit         2
Fiters          1
Scented Tabs    2