组合没有唯一值的行

时间:2017-03-24 19:44:30

标签: sql sql-server sql-server-2008-r2

我有一张这样的表:

+------+--------+----------+---------+---------+-----------+---------------+----------+
| Temp | UserID | TaskType | DayType | DayPart | TotalTask | PriorityTasks | LineCost |
+------+--------+----------+---------+---------+-----------+---------------+----------+
|    0 |   1001 |        1 | Weekday | AM      |       155 | NULL          |   125.30 |
|    0 |   1001 |        1 | Weekday | AM      |       155 | 155           |   125.30 |
|    0 |   1001 |        1 | Weekday | PM      |       125 | NULL          |   101.58 |
|    0 |   1001 |        1 | Weekday | PM      |       125 | 125           |   101.58 |
|    1 |   1001 |        1 | Weekday | PM      |       125 | NULL          |    51.81 |
|    0 |   1001 |        1 | Weekend | AM      |        45 | 45            |    97.55 |
|    0 |   1001 |        1 | Weekend | PM      |        54 | NULL          |    54.38 |
|    0 |   1001 |        1 | Weekend | PM      |        54 | 54            |    54.38 |
+------+--------+----------+---------+---------+-----------+---------------+----------+

我显然搞砸了我试图对优先级任务进行分组的地方,因为它不应该在它们存在时创建单独的行,而是填写当前行中的值。所以表格看起来应该是这样的:

+------+--------+----------+---------+---------+-----------+---------------+-----------+
| Temp | UserID | TaskType | DayType | DayPart | TotalTask | PriorityTasks | TotalCost |
+------+--------+----------+---------+---------+-----------+---------------+-----------+
|    0 |   1001 |        1 | Weekday | AM      |       155 | 155           |    125.30 |
|    0 |   1001 |        1 | Weekday | PM      |       125 | 125           |    101.58 |
|    1 |   1001 |        1 | Weekday | PM      |       125 | NULL          |     51.81 |
|    0 |   1001 |        1 | Weekend | AM      |        45 | 45            |     97.55 |
|    0 |   1001 |        1 | Weekend | PM      |        54 | 54            |     54.38 |
+------+--------+----------+---------+---------+-----------+---------------+-----------+

我的查询如下:

SELECT DISTINCT Temp, UserID, TaskType, DayType, DayPart,
    SUM(TaskCount) OVER (PARTITION BY UserID, TaskType, DayType, DayPart) as TotalTasks,
    CASE WHEN TaskType='PRIORITY' THEN SUM(TaskCount) OVER (PARTITION BY UserID, TaskType, DayType, DayPart) END as PriorityTasks,
    SUM(Cost) OVER (PARTITION BY Temp, UserID, TaskType, DayType, DayPart) as LineCost
FROM
(
    SELECT * FROM
    (
        SELECT Temp, TaskType, UserID, TaskType, DayType = 'Weekend', DayPart, 
            TaskCount=SUM(WeekendTasks), Cost=CONVERT(DECIMAL(10,2),SUM((CostPerWeek / TasksPerWeek) * WeekendTasks))
        FROM #Temp_CostPerAffiliateByTemp3
        GROUP BY Temp, TaskType, UserID, TaskType, DayPart, CostPerWeek
        UNION ALL
        SELECT Temp, TaskType, UserID, TaskType, DayType = 'Weekday', DayPart, 
            Tasks=SUM(WeekdayTasks), Cost=CONVERT(DECIMAL(10,2),SUM((CostPerWeek / TasksPerWeek) * WeekdayTasks))
        FROM #Temp_CostPerAffiliateByTemp3
        GROUP BY Temp, TaskType, UserID, TaskType, DayPart, CostPerWeek
    ) AS Y
    WHERE (TaskCount > 0)
) AS X
GROUP BY X.UserID, X.TaskType, X.DayType, X.DayPart, X.Temp, X.TaskCount, X.TaskType, X.Cost

我猜它的东西很简单,但是我用STUFF搜索引用的所有东西都不是我正在寻找的东西,或者使用MAX但是我不能在那里使用它认为缺少某些东西

CASE WHEN TaskType='PRIORITY' THEN SUM(TaskCount) OVER (PARTITION BY UserID, TaskType, DayType, DayPart) END as PriorityTasks

但当然,当我收到错误时,我无法做到这一点:

*窗口函数不能在另一个窗口函数或聚合的上下文中使用。 *

提前致谢!

2 个答案:

答案 0 :(得分:2)

为什么其中一个按Temp划分而其他人没划分?

SELECT ....
 ...SUM(TaskCount) OVER (PARTITION BY UserID, TaskType, DayType, DayPart) as TotalTasks,
 ...SUM(TaskCount) OVER (PARTITION BY UserID, TaskType, DayType, DayPart) END as PriorityTasks,
 ...SUM(Cost) OVER (PARTITION BY Temp, UserID, TaskType, DayType, DayPart) as LineCost
FROM
...

看起来你可以使用max(),除非我遗漏了某些内容......

select 
    id = min(id)
  , Temp
  , UserID
  , TaskType
  , DayType
  , DayPart
  , TotalTask
  , PriorityTasks = max(PriorityTasks)
  , LineCost
from t
group by 
    Temp
  , UserID
  , TaskType
  , DayType
  , DayPart
  , TotalTask
  , LineCost
order by min(id)

rextester演示:http://rextester.com/UERVTJ42678

返回:

+----+------+--------+----------+---------+---------+-----------+---------------+----------+
| id | Temp | UserID | TaskType | DayType | DayPart | TotalTask | PriorityTasks | LineCost |
+----+------+--------+----------+---------+---------+-----------+---------------+----------+
|  1 |    0 |   1001 |        1 | Weekday | AM      |       155 | 155           | 125,30   |
|  3 |    0 |   1001 |        1 | Weekday | PM      |       125 | 125           | 101,58   |
|  5 |    1 |   1001 |        1 | Weekday | PM      |       125 | NULL          | 51,81    |
|  6 |    0 |   1001 |        1 | Weekend | AM      |        45 | 45            | 97,55    |
|  7 |    0 |   1001 |        1 | Weekend | PM      |        54 | 54            | 54,38    |
+----+------+--------+----------+---------+---------+-----------+---------------+----------+

答案 1 :(得分:0)

declare @foo table([Temp] int, UserID int, TaskType int, DayType varchar(20), 
                   DayPart varchar(10), TotalTask int, PriorityTasks int, LineCost decimal(10,2));
insert into @foo values
(0, 1001, 1, 'Weekday', 'AM', 155, NULL, 125.30),
(0, 1001, 1, 'Weekday', 'AM', 155,  155, 125.30),
(0, 1001, 1, 'Weekday', 'PM', 125, NULL, 101.58),
(0, 1001, 1, 'Weekday', 'PM', 125,  125, 101.58),
(1, 1001, 1, 'Weekday', 'PM', 125, NULL,  51.81),
(0, 1001, 1, 'Weekend', 'AM',  45,   45,  97.55),
(0, 1001, 1, 'Weekend', 'PM',  54, NULL,  54.38),
(0, 1001, 1, 'Weekend', 'PM',  54,   54,  54.38);

SELECT [Temp], UserID, TaskType, DayType, DayPart, TotalTask, SUM(PriorityTasks) PriorityTasks, LineCost
FROM   @foo
GROUP BY [Temp], UserID, TaskType, DayType, DayPart, TotalTask, LineCost
GO
Temp | UserID | TaskType | DayType | DayPart | TotalTask | PriorityTasks | LineCost
---: | -----: | -------: | :------ | :------ | --------: | ------------: | :-------
   0 |   1001 |        1 | Weekday | AM      |       155 |           155 | 125.30  
   0 |   1001 |        1 | Weekday | PM      |       125 |           125 | 101.58  
   0 |   1001 |        1 | Weekend | AM      |        45 |            45 | 97.55   
   0 |   1001 |        1 | Weekend | PM      |        54 |            54 | 54.38   
   1 |   1001 |        1 | Weekday | PM      |       125 |          null | 51.81   

Warning: Null value is eliminated by an aggregate or other SET operation.

dbfiddle here