总计计数访问 - Sql

时间:2015-03-29 15:55:50

标签: sql-server-2012

我遇到了一个场景,我对如何让它发挥作用感到困惑。

我如何从SUM结果中COUNT两个不同的组?

Select Count(VisitID), Types 
From Customers
Group by Types;

结果如下:

Type VisitID
A    10
B    20
C    1

我希望结果如下:

Type VisitID
A+C  11

感谢。

2 个答案:

答案 0 :(得分:0)

例如CTE

WITH CustomerCounts ([Count], [Types])
AS
(
    Select Count(VisitID), [Types] 
    from Customers
    Group by [Types]
)
Select SUM([Count]) 
From CustomerCounts
Where [Types] in ('A', 'C')

答案 1 :(得分:0)

试试这个

仅选择您想要的类型,然后使用pivot将行转换为列,然后像这样汇总列SELECT pv.[a] + pv.[c] from (..........)