如何根据SQL Server另一列中的值获取行?

时间:2018-09-20 15:27:29

标签: sql sql-server

*CHI: (hi) new [/] friend [//] there [/-] [ bch] [ bch ] /[]/ [/=] <new> <there>. %mod: hi there. *CHI: <dude> <there> *CHI: &=sighs <and instead the> [//] and then the gira?e got it and gave it to the elephant . *CHI: <he> [/] <he> [/] he hold it . *CHI: then [/-] the doctor give the [/] money to the man *CHI: and (i)s then (.) the little (.) gira?e is crying because it (i)s sinking

['new', '[/]', 'friend', '[//]', 'there', 'bch', '/[]/']
['dude', 'dude']
['and', 'and', 'instead', 'the', 'the', '[//]', 'and', 'then', 'the', 'gira?e', 'got', 'it', 'and', 'gave', 'it', 'to', 'the', 'elephant', '.']
['he', 'he', '[/]', 'he', 'he', '[/]', 'he', 'hold', 'it', '.']
['then', 'the', 'doctor', 'give', 'the', '[/]', 'money', 'to', 'the', 'man']
['and', 'then', '(.)', 'the', 'little', '(.)', 'gira?e', 'is', 'crying', 'because', 'it', 'sinking']

对于Q,我只需要排行其人员的评分为空且他们各自在不同团队中的人员的评分相同。因为P2在不同的团队eee和eee中,所以他们各自的个人评分是R1。 “各自人的等级”是同一部门中其他人的非零等级吗?

第3季度的输出

id      dept         Person        Rating
------------------------------------------
1       ece            p1           R1  
2       ece            p2           Null  
6       eee            P6           R1             
5       eee            p2           Null
6       Civil          P7           Null
7       Civil          P3           Null 
8       Civil          P8           R5
9       Mech           p7           R2
10      Mech           P3           Null

它不应包含以下行,因为P3在Civil和Mech中都存在,但是人的等级不同,即R5和R2

Q

3 个答案:

答案 0 :(得分:0)

尝试以下操作:

<hr>

答案 1 :(得分:0)

这是我的尝试,就像您的示例输出

 with t1 as    
(
select * from 
(
select 1 as id, 'ece' as dept,  'p1' as Person ,'R1' as Rating
union all
select 2,'ece','p2', Null  
union all
select 6,'eee','P6','R1'
union all
select 5,'eee','p2',Null
union all
select 6,'Civil','P7', Null
union all
select 7,'Civil','P3', Null 
union all
select 8,'Civil','P8','R5'
union all
select 9,'Mech','p7','R2'
union all
select 10,'Mech','P3', Null


) t
) 
, t2 as
(
select dept,Person from t1 where rating is null
) ,
a as
(
select t3.* from t2 t3 join t2 t4 on t3.Person=t4.Person and t3.dept!=t4.dept
) ,

b as 
(
select * from t1 where t1.dept in (select dept from a) and t1.rating is not null
), c as
(
select r1.dept from b r1 join b r2 on r1.rating=r2.rating and r1.dept<>r2.dept
)
,d as
(
select * from t1 where t1.dept in (select dept from c) and t1.rating is null
) 
 select * from d

输出

  id    dept    Person  Rating
  2     ece      p2      null
  5     eee      p2      null

答案 2 :(得分:0)

with team1 as    
(
select * from 
(
select 1 as id, 'ece' as dept,  'p1' as Person ,'R1' as Rating
union all
select 2,'ece','p2', Null  
union all
select 6,'eee','P6','R1'
union all
select 5,'eee','p2',Null
union all
select 6,'Civil','P7', Null
union all
select 7,'Civil','P3', Null 
union all
select 8,'Civil','P8','R5'
union all
select 9,'Mech','p7','R2'
union all
select 10,'Mech','P3', Null


) t
) ,
UniqueRatingCTE as (
 SELECT *
FROM team1
WHERE   dept IN (
  SELECT  dept  
  FROM team1 
  GROUP BY dept
  HAVING COUNT(DISTINCT Rating) =1 
  )) ,


    calculation AS (
        SELECT t1.Person
        FROM UniqueRatingCTE t1
            INNER JOIN UniqueRatingCTE t2 ON t1.dept = t2.dept
        WHERE t1.Rating  IS NULL
        GROUP BY t1.Person
        HAVING COUNT(DISTINCT t2.Rating) = 1
           --AND COUNT(DISTINCT t2.Person) >= 2
    )
SELECT *
FROM UniqueRatingCTE
WHERE Rating IS NULL
  AND Person IN (SELECT Person FROM calculation) order by Person;

这是某种方式的工作。我从Kais那里得到的。