如何根据其他列的不同值进行分组

时间:2018-04-17 15:24:26

标签: sql-server ssms

我有这个输入表

+--------+-----------+------------+----------+
| TaskId | OwnerName | WorkerName | Category |
+--------+-----------+------------+----------+
|      1 | Sara      | Sara       |        1 |
|      1 | Sara      | Maya       |        1 |
|      1 | Sara      | Sara       |        1 |
|      2 | Sara      | Sara       |        0 |
|      2 | Sara      | Sara       |        0 |
|      3 | Sam       | Sam        |        1 |
|      3 | Sam       | Sam        |        1 |
|      3 | Sam       | Sam        |        1 |
|      4 | Ella      | Ella       |        1 |
|      4 | Ella      | Ella       |        1 |
|      5 | Ella      | Ella       |        1 |
|      6 | Ella      | Ella       |        0 |
+--------+-----------+------------+----------+

我想计算所有者名称在类别列高(1)或低(0)中的次数。

任务应该被计为不同,如果任务有所有者名称!=工作人员姓名......整个任务都将被删除。

例如,将删除TaskId 1。

预期产出:

+-----------+------------------+-----------------+-------+------+
| OwnerName | #UniqueHighTasks | #UniqueLowTasks | %High | %Low |
+-----------+------------------+-----------------+-------+------+
| Sara      |                0 |               1 | 0     | 100  |
| Sam       |                1 |               0 | 100   | 0    |
| Ella      |                2 |               1 | 66%   | 33%  |
+-----------+------------------+-----------------+-------+------+

我的尝试没有考虑到不同的ID和任务1将被删除,如何实现?

尝试:

SELECT 
      [OwnerName], 


  (100 * COALESCE(COUNT(CASE
    WHEN [Category] = 1 THEN 1
  END), 0) /
  (COALESCE(COUNT(CASE
    WHEN [Category] = 1 THEN 1
  END), 0) + COALESCE(COUNT(CASE
    WHEN [Category] = 0 THEN 1
  END), 0))) AS %High,

  (100 * COALESCE(COUNT(CASE
    WHEN [Category] = 0 THEN 1
  END), 0) /
  (COALESCE(COUNT(CASE
    WHEN [Category] = 0 THEN 1
  END), 0) + COALESCE(COUNT(CASE
    WHEN [Category] = 1 THEN 1
  END), 0))) AS %Low,

  SUM(case [Category] when 1 then 1 else 0 end) AS '#UniqueHighTasks',
  SUM(case [Category] when 0 then 1 else 0 end) AS '#UniqueLowTasks'


   FROM [dbo].[mytable]
  Group by  [OwnerName]

4 个答案:

答案 0 :(得分:1)

尝试此查询:

declare @t table (TaskId int, OwnerName varchar(10), WorkerName varchar(10), Category int)
insert into @t
values 
    (1, 'Sara','Sara', 1), (1, 'Sara','Maya', 1)
    , (1, 'Sara','Sara', 1), (2, 'Sara','Sara', 0)
    , (2, 'Sara','Sara', 0), (3, 'Sam','Sam', 1)
    , (3, 'Sam','Sam', 1), (3, 'Sam','Sam', 1)
    , (4, 'Ella','Ella', 1), (4, 'Ella','Ella', 1)
    , (5, 'Ella','Ella', 1), (6, 'Ella','Ella', 0)

select
    OwnerName, UniqueHighTasks, UniqueLowTasks
    , [%High] = UniqueHighTasks * 100.0 / (UniqueHighTasks + UniqueLowTasks)
    , [%Low] = UniqueLowTasks * 100.0 / (UniqueHighTasks + UniqueLowTasks)
from (
    select
        OwnerName, UniqueHighTasks = count(distinct case when Category = 1 then TaskId end)
        , UniqueLowTasks =  count(distinct case when Category = 0 then TaskId end)
    from
        @t
    where
        TaskId not in (select TaskId from @t where OwnerName <> WorkerName)
    group by OwnerName
) t

答案 1 :(得分:1)

我认为这样做会

declare @T table (TaskId int, OwnerName varchar(20), WorkerName varchar(20), Category int);
insert into @T values
  (1, 'Sara', 'Sara', 1 )
, (1, 'Sara', 'Maya', 1 )
, (1, 'Sara', 'Sara', 1 )
, (2, 'Sara', 'Sara', 0 )
, (2, 'Sara', 'Sara', 0 )
, (3, 'Sam',  'Sam',  1 )
, (3, 'Sam',  'Sam',  1 )
, (3, 'Sam',  'Sam',  1 )
, (4, 'Ella', 'Ella', 1 )
, (4, 'Ella', 'Ella', 1 )
, (5, 'Ella', 'Ella', 1 )
, (6, 'Ella', 'Ella', 0 );
with cte as 
( select distinct t.TaskId, t.OwnerName, t.Category 
  from @T t
  where TaskID not in (select TaskId from @T where OwnerName <> WorkerName)
)
select cte.OwnerName 
     , count(*) taskCount 
     , sum(category) as highCount
     , count(*) - sum(category) as lowCount 
     , 100.0*sum(category)/count(*) as highPct
     , 100.0*(count(*)-sum(category))/count(*) as lowPct
from cte 
group by cte.OwnerName

OwnerName            taskCount   highCount   lowCount    lowPct                                 highPct
-------------------- ----------- ----------- ----------- --------------------------------------- ---------------------------------------
Ella                 3           2           1           66.666666666666                         33.333333333333
Sam                  1           1           0           100.000000000000                        0.000000000000
Sara                 1           0           1           0.000000000000                          100.000000000000

答案 2 :(得分:0)

获取当前查询,而不是直接查询表,查询仅从表中执行SELECT DISTINCT的CTE或派生表。

答案 3 :(得分:0)

你可以试试这个

WITH nonDup AS (
  SELECT DISTINCT [TaskId], [OwnerName], [WorkerName], [Category] 
  FROM mytable t1
  WHERE NOT EXISTS(SELECT 1 FROM mytable t2 WHERE t1.TaskID = t2.TaskID and t2.OwnerName <> t2.WorkerName)
),
calc AS (
  SELECT 
      OwnerName,
      SUM(Category) AS UniqueHighTasks,
      SUM(CASE WHEN Category = 0 THEN 1 ELSE 0 END) AS UniqueLowTasks
  FROM nonDup
  GROUP BY OwnerName
)
SELECT OwnerName,UniqueHighTasks,UniqueLowTasks,
    CASE WHEN UniqueHighTasks+ UniqueLowTasks<> 0 THEN UniqueHighTasks*1.0/(UniqueHighTasks+ UniqueLowTasks) END AS [%High],
    CASE WHEN UniqueHighTasks+ UniqueLowTasks<> 0 THEN UniqueLowTasks*1.0/(UniqueHighTasks+ UniqueLowTasks) END AS [%Low]
FROM calc