SQL查询-COUNT个表中两个或多个列的组合

时间:2019-01-19 06:36:23

标签: sql-server sql-server-2008 sql-server-2012 sql-server-2008-r2

我有一个表,其中的列和行值如下,并想要一个如下所示的结果集。我尝试了许多查询,但无法获得所需的结果集。 有没有最简单的方法来实现这一目标。

Column1 Column2  Column3 Column4 Column5
AAA     000000      BG1   12345   North
AAA     111111      BG2   23456   South
BBB     000000      BG3   12346   EAST
AAA     000000      BG2   12345   West

Select Column1,Count( Distinct Column1,Column2),Count(Distinct Column1,Column3),Count(Column1,Column4,Column5) From #Temp

预期结果集:

Name C2Count C3Count C4Count
AAA    2      2        3     
BBB    1      1        1

3 个答案:

答案 0 :(得分:1)

我仍然看不到要避免进行两个单独的聚合。一个可以处理单个不同的计数,而另一个可以处理两个列的计数:

WITH cte1 AS (
    SELECT
        Column1 AS Name,
        COUNT(DISTINCT Column2) AS C2Count,
        COUNT(DISTINCT Column3) AS C3Count
    FROM yourTable
    GROUP BY Column1
),
cte2 AS (
    SELECT Name, COUNT(*) AS C45Count
    FROM
    (
        SELECT DISTINCT Column1 AS Name, Column4, Column5 FROM yourTable
    ) t
    GROUP BY Name
)

SELECT
    t1.Name,
    t1.C2Count,
    t1.C3Count,
    t2.C45Count
FROM cte1 t1
INNER JOIN cte2 t2
    ON t1.Name = t2.Name;

Demo

答案 1 :(得分:0)

也许您在寻找这个? :

DECLARE 
    @tb TABLE
    (
        Column1 VARCHAR(50)
    ,   Column2 VARCHAR(50)
    ,   Column3 VARCHAR(50)
    ,   Column4 INT
    ,   Column5 VARCHAR(50)
    )
INSERT INTO @tb VALUES 
('AAA','000000','BG1',12345,'North'),
('AAA','111111','BG2',23456,'South'),
('BBB','000000','BG3',12346,'EAST'),
('AAA','000000','BG2',12345,'West')

SELECT
    Column1 [Name]
,   COUNT(DISTINCT Column2) C2Count 
,   COUNT(DISTINCT Column3) C3Count 
,   SUM(C45Count) C4Count
FROM (
    SELECT *, COUNT(Column1) OVER(PARTITION BY Column1, Column4, Column5) C45Count
    FROM @tb 
) D
GROUP BY 
    Column1

答案 2 :(得分:0)

我对如何计算C4count感到有些困惑。使用此数据(感谢@ iSR5):

x = np.arange(301)
y = np.cumsum(np.random.randn(len(x)))
y = (y-y.min())/(y.max()-y.min()) * 7000 + 8000

fig, ax = plt.subplots()
ax.plot(x,y)

ax.yaxis.set_minor_locator(GeomLocator(1.01, None))
ax.grid(which="minor", axis="y")
plt.show()

您可以使用一个非常基本的语句来达到预期的结果:

CREATE TABLE #Table
    (
        Column1 VARCHAR(50)
    ,   Column2 VARCHAR(50)
    ,   Column3 VARCHAR(50)
    ,   Column4 INT
    ,   Column5 VARCHAR(50)
    )
INSERT INTO #Table VALUES 
('AAA','000000','BG1',12345,'North'),
('AAA','111111','BG2',23456,'South'),
('BBB','000000','BG3',12346,'EAST'),
('AAA','000000','BG2',12345,'West')

结果:

SELECT   Column1 AS [Name]
        ,COUNT(DISTINCT t.Column2) AS C2Count
        ,COUNT(DISTINCT t.Column3) AS C3Count
        ,COUNT(t.Column4) AS C4Count
FROM #Table t
GROUP BY t.Column1

这将计算第二列和第三列的AAA和BBB每列中有多少不同的匹配项,并计算第四列的整体匹配项。您想要的结果在column5中什么都没有显示。

如果您要进行其他计算,请清楚描述您要查找的内容,以便我调整代码以显示您要查找的内容。