[英]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 我计算了两件事
Now , I want to count the distinct number of functions that Jack exists in whether he is in member1
or member2
现在 ,我要算独特的一些功能 ,杰克存在于他是否在
member1
或member2
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个杰出计数(内部,开发,程序),其余名称相同。
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 |
+--------+-----------------+----------------+-----------------+------------------+-----------+
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.