仅查找同一组中的表中的公共列项

时间:2018-05-22 21:54:45

标签: sql sql-server

我有一个有3列的表 Plate_Id,Prod_id和位置

您可以说plate_id是“标题”列。 prod_id将'locations'分组为特定的'plate_id'

给定一组特定的值,我只想在prod_ids中为特定的plate_id选择'COMMON'的位置。

注意:我的表可以有多个plate_ids

我很亲近,但并不完美。 我试图隔离给定plate_id的最小组,然后尝试将其与原始列表内部连接,但是对于我有3个prod_ids且一个位置对于一个组来说常见的情况它是失败的(我只需要位置是严格地在每个prod_id)

以下是我想要的结果,基于我到目前为止所取得的成就,

--     DESIRED RESULT:

--    plate_id    location
--    100            1
--    100            2
--    200            3
--    200            4
--    300            1
--    300            2
--    300            5 


create table #AllTab
(
plate_id int,
prod_id int, 
location int
)

insert into #AllTab
values
(100,10,    1),
(100,10,    2),
(100,10,    3),
(100,10,    4),

(100,20,    1),
(100,20,    2),
(100,20,    3),
(100,20,    4),
(100,20,    5),
(100,20,    6),
(100,20,    9),

(100,30,    1),
(100,30,    2),
(100,30,    9),

(100,40,    1),
(100,40,    2),
(100,40,    12),
(100,40,    14),

(100,40,    1),
(100,40,    2),
(100,40,    25),
(100,40,    30),


-----------------

(200,10,    1),
(200,10,    2),
(200,10,    3),
(200,10,    4),

(200,20,    1),
(200,20,    2),
(200,20,    3),
(200,20,    4),
(200,20,    5),
(200,20,    6),
(200,20,    7),

(200,30,    3),
(200,30,    4),
(200,30,    9),

-----------------

(300,10,    1),
(300,10,    2),
(300,10,    3),
(300,10,    5),

(300,20,    1),
(300,20,    2),
(300,20,    3),
(300,20,    4),
(300,20,    5),
(300,20,    6),
(300,20,    7),
(300,20,    9),

(300,30,    1),
(300,30,    2),
(300,30,    5)


-- The #SubTab table isolates the smallest group from the above table
-- for a particular plate_id

create table #SubTab
(
plate_id int,
prod_id int, 
location int
)

insert into #SubTab
values
(100,30,    1),
(100,30,    2),
(100,30,    9),
------------
(200,30,    3),
(200,30,    4),
(200,30,    9),
------------
(300,30,    1),
(300,30,    2),
(300,30,    5)


select distinct pr.plate_id, pr.prod_id, pr.location from #SubTab pr
inner join #AllTab pl on pr.plate_id = pl.plate_id 
                            and pr.location = pl.location
where pr.Prod_Id <> pl.prod_id
group by  pr.plate_id, pr.prod_id, pr.location

3 个答案:

答案 0 :(得分:0)

此查询返回给定印版的所有产品中的位置:

select plate_id, location
from #alltab a
group by plate_id, location
having count(distinct prod_id) = (select count(distinct prod_id) from #alltab a2 where a2.plate_id = a.plate_id);

这假设表中没有重复 - 给出数据的合理假设。

Here是rextester。

答案 1 :(得分:0)

我有一个解决方案,它可能有点冗长,但有效,

SELECT
       SubGroupCounts.plate_id,   
       LocationSubGroupCounts.location
FROM (
       -- Number of sub-groupping relative to main groupping
       select 
              plate_id,
              count(distinct prod_id) as num
       from AllTab
       group by plate_id
) SubGroupCounts
INNER JOIN (
       -- Count the number of sub-groups each location appears in
       SELECT
       plate_id,
              Location, 
              count(distinct prod_id) as num
       FROM AllTab
       GROUP BY Location, plate_id
) LocationSubGroupCounts on LocationSubGroupCounts.plate_id = SubGroupCounts.plate_id and LocationSubGroupCounts.num = SubGroupCounts.num

答案 2 :(得分:0)

试试这个:

;with cte1
AS
    (
        Select  Plate_Id,Count(DISTINCT prod_id) as ProdCount
        From #AllTab
        Group by Plate_Id
    )
,cte2
AS
    (
        Select Plate_Id,Location,Count(Location) As LocCount
        from #AllTab
        Group by Plate_Id,Location
    )

SELECT t1.plate_id ,t2.location
FROM cte1 t1 JOIN cte2 t2 
ON t1.Plate_Id =t2.Plate_Id
Where LocCount>=ProdCount