简体   繁体   English

SQL-如何选择2列中具有相同值的不同行

[英]SQL-How to select distinct rows that have same value in 2 columns

I have this input table 我有这个输入表

+--------+---------+---------+-------+-----------+
| TaskId | member1 | member2 | score | functions |
+--------+---------+---------+-------+-----------+
|      1 | Jack    | Jack    |   100 | marketing |
|      1 | Jack    | Jack    |   100 | marketing |
|      2 | Jack    | Steve   |    90 | interior  |
|      2 | Jack    | Steve   |    90 | interior  |
|      3 | Steve   | Jack    |    70 | program   |
|      3 | Steve   | Jack    |    70 | program   |
|      4 | Jack    | Mia     |    30 | develop   |
|      4 | Jack    | Mia     |    30 | develop   |
|      5 | Mia     | Jack    |    20 | interior  |
|      5 | Mia     | Jack    |    20 | interior  |
+--------+---------+---------+-------+-----------+

I calculated 2 things 我计算了两件事

  • UniqueHighTasks :Distinct #Of tasks that (for eg:) Jack existed in score(75-100) whether he is in member 1 or 2 UniqueHighTasks :不同的#Of任务(例如:)杰克存在于得分(75-100)中,无论他是在成员1还是2
  • UniqueLowTasks :Distinct #Of tasks that (for eg:) Jack existed in score(0-100) whether he is in member 1 or 2 UniqueLowTasks :不同的#Of任务(例如:)杰克在得分(0-100)中是否存在于成员1或2中
  • I excluded if member1 = member2 for that calculation 我排除了member1 = member2用于该计算

Now , I want to count the distinct number of functions that Jack exists in whether he is in member1 or member2 现在 ,我要算独特的一些功能 ,杰克存在于他是否在member1member2

  • For example: Jack as in member1 is in (interior,develop) 例如:成员1中的杰克在(内部,开发)

  • Jack as in member 2 is in (program, interior) 成员2中的杰克在(程序,室内)

The count of my final result is 4 which is wrong, how can i get The distinct count which is 3 for Jack which are (interior,develop,program) , the same for the rest of the names. 我的最终结果的数量是4,这是错误的,我怎么能得到杰克的3个杰出计数(内部,开发,程序),其余名称相同。

SQL Fiddle Code SQL小提琴代码

Instead of applying your logic for individual sets, you can combine both the sets together using UNION ALL and apply you logic at a single place. 您可以使用UNION ALL将两个集合组合在一起,而不是将逻辑应用于单个集合,而是在单个位置应用逻辑。

SELECT member, 
       uniquehightasks, 
       uniquelowtasks, 
       [%High] = uniquehightasks * 100.0 / ( uniquehightasks + uniquelowtasks ), 
       [%Low] = uniquelowtasks * 100.0 / ( uniquehightasks + uniquelowtasks ), 
       functions 
FROM   (SELECT member, 
               Sum(uniquehightasks) AS 'UniqueHighTasks', 
               Sum(uniquelowtasks)  AS 'UniqueLowTasks', 
               Sum(functions)       AS 'functions' 
        FROM   (SELECT member, 
                       UniqueHighTasks = Count(DISTINCT CASE WHEN score >= 75 THEN [taskid] END), 
                       UniqueLowTasks = Count(DISTINCT CASE  WHEN score < 75 THEN  [taskid] END), 
                       functions=Count(DISTINCT functions) 
                FROM   (SELECT [taskid], member1 AS Member, functions, score 
                        FROM   mytable 
                        WHERE  member1 != member2 
                        UNION ALL
                        SELECT [taskid], member2 AS member,functions, score 
                        FROM   mytable 
                        WHERE  member1 != member2) t22 
                GROUP  BY member)t3 
        GROUP  BY t3.member) t4 

Output 产量

+--------+-----------------+----------------+-----------------+------------------+-----------+
| member | UniqueHighTasks | UniqueLowTasks | %High           | %Low             | functions |
+--------+-----------------+----------------+-----------------+------------------+-----------+
| Jack   | 1               | 3              | 25.000000000000 | 75.000000000000  | 3         |
+--------+-----------------+----------------+-----------------+------------------+-----------+
| Mia    | 0               | 2              | 0.000000000000  | 100.000000000000 | 2         |
+--------+-----------------+----------------+-----------------+------------------+-----------+
| Steve  | 1               | 1              | 50.000000000000 | 50.000000000000  | 2         |
+--------+-----------------+----------------+-----------------+------------------+-----------+

DEMO DEMO

The reason you are getting count as 4 for Jack is bacause both part of your UNION are giving you a count 2 and then you are doing a sum of it. 你杰克被计为4的原因是因为你的UNION的一部分给你一个计数2 ,然后你正在做一个总和。

Try this, but this will also give count 2 for Mia instead of 1. 试试这个,但这也会给Mia而不是1的计数2。

http://sqlfiddle.com/#!18/49461/10 http://sqlfiddle.com/#!18/49461/10

SELECT member1, 
       uniquehightasks, 
       uniquelowtasks, 
       [%High] = uniquehightasks * 100.0 / ( uniquehightasks + uniquelowtasks ), 
       [%Low] = uniquelowtasks * 100.0 / ( uniquehightasks + uniquelowtasks ), 
       functions 
FROM   (SELECT member1, 
               Sum(uniquehightasks)      AS 'UniqueHighTasks', 
               Sum(uniquelowtasks)       AS 'UniqueLowTasks', 
               Count(DISTINCT functions) AS 'functions' 
        FROM   (SELECT member1, 
                       UniqueHighTasks = Count(DISTINCT CASE 
                                                          WHEN score >= 75 THEN 
                                                          [taskid] 
                                                        END), 
                       UniqueLowTasks = Count(DISTINCT CASE 
                                                         WHEN score < 75 THEN 
                                                         [taskid] 
                                                       END), 
                       functions 
                FROM   mytable 
                WHERE  member1 != member2 
                GROUP  BY member1, 
                          functions 
                UNION 
                SELECT member2, 
                       UniqueHighTasks = Count(DISTINCT CASE 
                                                          WHEN score >= 75 THEN 
                                                          [taskid] 
                                                        END), 
                       UniqueLowTasks = Count(DISTINCT CASE 
                                                         WHEN score < 75 THEN 
                                                         [taskid] 
                                                       END), 
                       functions 
                FROM   mytable 
                WHERE  member1 != member2 
                GROUP  BY member2, 
                          functions)t3 
        GROUP  BY t3.member1) t4 

Here's another approach. 这是另一种方法。 My result for Mia differs from yours 我对Mia的结果与你的不同

select
    member, UniqueHighTasks, UniqueLowTasks
    , High = UniqueHighTasks * 100.0 / (UniqueHighTasks + UniqueLowTasks)
    , Low = UniqueLowTasks * 100.0 / (UniqueHighTasks + UniqueLowTasks)
    , functions
from (
    select
        member = case when n = 1 then member1 else member2 end
        , UniqueHighTasks = count(distinct case when score >= 75 then taskid end)
        , UniqueLowTasks = count(distinct case when score < 75 then taskid end)
        , functions = count(distinct functions)
    from
        mytable
        cross apply (values (1), (2)) q(n)
    where
        member1 <> member2
    group by case when n = 1 then member1 else member2 end
) t

Output 产量

member  UniqueHighTasks   UniqueLowTasks   High             Low                functions
-----------------------------------------------------------------------------------------
Jack    1                 3                25.000000000000  75.000000000000    3
Mia     0                 2                0.000000000000   100.000000000000   2
Steve   1                 1                50.000000000000  50.000000000000    2

I think this should solve your problem: 我认为这应该可以解决你的问题:

;with cte as (
  select member1,member2,functions from mytable
  where member1 <> member2
)

select member1, count(distinct functions) from (
  select member1, functions from cte
  union all
  select member2, functions from cte
) [a] group by member1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用SQL / mySQL选择2列具有相同值的多个且1列具有不同值的行? - How do I use SQL/mySQL to select rows where 2 columns have multiple of the same value and 1 column has a distinct value? 如何选择不同的行,其中一列可能具有许多相同的值,但所有第二列均具有相同的值? - How do I select distinct rows where a column may have a number of the same values but all their 2nd columns have the same value? SQL - 选择在两列中具有相同值的行 - SQL - select rows that have the same value in two columns 需要一个 SQL 选择语句来返回在一个列中具有相同 id 而在另一列中具有不同值的行 - Need a SQL select statement to return rows that have the same id in one column and distinct value in another column 在Oracle / SQL中如何选择列的值保持不变的行 - In Oracle/SQL how to select rows where the value of the columns remained the same SQL Select 具有相同列值的行 - SQL Select Rows that have same column value SQL select 所有行都具有相同的值 - SQL select all rows have the same value SQL-如何从另一个表中选择多个值的行 - SQL-how to select rows which much multiple values from another table 如何在 SQL 中选择具有 1 个以上不同值的行? - How to select rows that have more than 1 distinct values in SQL? 在同一查询中选择2行不同和其他列 - Select 2 rows distinct and other columns on same query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM