SQL服务器,如何获取一些不同的项目

时间:2016-10-17 10:21:09

标签: sql sql-server sql-server-2008

我正在使用SQL服务器,

  id          3     | 4    | 5 | 6
  items       1 2 3 | 2 3 5| 6 | 1 2 5
    -------------------------
  # of items      3 | 4    | 5 | 5

所以,每个id都有项目(例如,3有3个项目 - 1,2,3) 对于每个项目,我想获得不同项目的数量。

所以,3有3个不同的项目 - 1,2,3 4有4个不同的项目 - 1,2,3,5 5有5个不同的项目 - 1,2,3,5,6 6有5个不同的项目 - 1,2,3,5,6

我可以通过执行计数(不同的项目)来执行此操作,1到2,1到3,1到5和1到6 但是我希望自动化这个过程并在一次运行中获得相同的结果。

这个想法是创建一个临时表并在其中放入一个项目,同时检查该项目是否已经存在于临时表中,并为每个id打印不同项目的数量。

4 个答案:

答案 0 :(得分:2)

CREATE TABLE TEST
(
    id int, items int
)

INSERT INTO TEST
VALUES
(3, 1),
(3, 2),
(3, 3),
(4, 2),
(4, 3),
(4, 5),
(5, 6),
(6, 1),
(6, 2),
(6, 5)

SELECT B.id, COUNT(DISTINCT(A.ITEMS)) AS itemCount
FROM TEST A
INNER JOIN TEST B ON A.id <= B.id
GROUP BY B.ID

DROP TABLE TEST

Output: 
id  itemCount
3   3
4   4
5   5
6   5

答案 1 :(得分:1)

假设您的数据格式如下:

Declare @table table
(
id int,
items varchar(10)
)

insert into @table values (3, '1 2 3');
insert into @table values (4, '2 3 5');
insert into @table values (5, '6');
insert into @table values (6, '1 2 5');

with cte as
(
Select id, b.Item
from @table a
cross apply [dbo].[Split] (items, ' ') b
)

Select y.id, count(distinct(x.Item)) AS [# of items]
from cte x
join cte y on x.id <= y.id
group by y.id

使用LINK中的表值函数[dbo].[Split]

答案 2 :(得分:0)

您可以如下所示:

DECLARE @Tbl TABLE (Id VARCHAR(10), Column3 VARCHAR(100), Column4 VARCHAR(100), Column5 VARCHAR(100), Column6 VARCHAR(100))
INSERT @Tbl
VALUES 
('items', '1 2 3', '2 3 5', '6', '1 2 5')   

;WITH CTE1
AS
(
    SELECT T.Id, T.Column3 AS ColumnId, CAST('<X>' + REPLACE(T.Column3,' ','</X><X>') + '</X>' as XML) AS FilterColumn FROM  @Tbl T UNION ALL
    SELECT T.Id, T.Column4 AS ColumnId, CAST('<X>' + REPLACE(T.Column4,' ','</X><X>') + '</X>' as XML) AS FilterColumn FROM  @Tbl T UNION ALL
    SELECT T.Id, T.Column5 AS ColumnId, CAST('<X>' + REPLACE(T.Column5,' ','</X><X>') + '</X>' as XML) AS FilterColumn FROM  @Tbl T UNION ALL
    SELECT T.Id, T.Column6 AS ColumnId, CAST('<X>' + REPLACE(T.Column6,' ','</X><X>') + '</X>' as XML) AS FilterColumn FROM  @Tbl T 
), CTE2
AS 
(
    SELECT 
        A.*,
        B.SplitData
    FROM
        CTE1 A CROSS APPLY
        (SELECT fdata.D.value('.','varchar(50)') AS SplitData FROM A.FilterColumn.nodes('X') as fdata(D)) B
)

SELECT
    T.Id ,
    (SELECT COUNT(DISTINCT C.SplitData) FROM CTE2 C WHERE C.Id = T.Id AND C.ColumnId IN (T.Column3)) Column3OfDistinct,
    (SELECT COUNT(DISTINCT C.SplitData) FROM CTE2 C WHERE C.Id = T.Id AND C.ColumnId IN (T.Column3, T.Column4)) Column4OfDistinct,
    (SELECT COUNT(DISTINCT C.SplitData) FROM CTE2 C WHERE C.Id = T.Id AND C.ColumnId IN (T.Column3, T.Column4, T.Column5)) Column5OfDistinct,
    (SELECT COUNT(DISTINCT C.SplitData) FROM CTE2 C WHERE C.Id = T.Id AND C.ColumnId IN (T.Column3, T.Column4, T.Column5, T.Column6)) Column6OfDistinct    
FROM
    @Tbl T

结果:

Id         Column3OfDistinct Column4OfDistinct Column5OfDistinct Column6OfDistinct
---------- ----------------- ----------------- ----------------- -----------------
items      3                 4                 5                 5

答案 3 :(得分:-1)

这可以帮到你:

    select
        id,
        count(items)
    from table_name
    group by id