SSRS或SQL仅传递组的第一个元素

时间:2017-08-09 13:03:55

标签: sql sql-server reporting-services

我有桌子:

Taskname         |Checklist| Start        | End     |Training | StartDT                 | EndDT                   | User    | Answertype
QuizMC1708060531 | QuizMC | 1502033469885 | 1502033478839 | 1 | 2017-08-06 17:31:09.883 | 2017-08-06 17:31:18.837 | adminEn | Multiple
QuizMC1708060531 | QuizMC | 1502033469885 | 1502033478839 | 1 | 2017-08-06 17:31:09.883 | 2017-08-06 17:31:18.837 | adminEn | Text
QuizMC1708060533 | QuizMC | 1502033603938 | 1502033609396 | 1 | 2017-08-06 17:33:23.937 | 2017-08-06 17:33:29.393 | adminEn | Multiple
QuizMC1708060533 | QuizMC | 1502033603938 | 1502033609396 | 1 | 2017-08-06 17:33:23.937 | 2017-08-06 17:33:29.393 | adminEn | Text
QuizMC1708071122 | QuizMC | 1502097788914 | 1502114086923 | 1 | 2017-08-07 11:23:08.913 | 2017-08-07 15:54:46.920 | adminEn | Multiple
QuizMC1708071122 | QuizMC | 1502097788914 | 1502114086923 | 1 | 2017-08-07 11:23:08.913 | 2017-08-07 15:54:46.920 | adminEn | Text
QuizMC1708070354 | QuizMC | 1502114092033 | 1502173089869 | 1 | 2017-08-07 15:54:52.030 | 2017-08-08 08:18:09.867 | adminEn | Multiple
QuizMC1708070354 | QuizMC | 1502114092033 | 1502173089869 | 1 | 2017-08-07 15:54:52.030 | 2017-08-08 08:18:09.867 | adminEn | Text

所以我想(通过SQL或SSRS)按Taskname对所有条目进行分组,这样每个任务名称只存在一个条目。在这种情况下,它意味着每一秒都将被丢弃。这个表的大小可以不同,这意味着一个Taskname组可以有1个条目,2,5,10。对我来说无关紧要的是我得到它只是每组只有一行。

编辑:我不能丢失任何行,添加新行是可以的。

3 个答案:

答案 0 :(得分:1)

试试这个

SELECT Taskname  ,Checklist, Start ,End,Training, StartDT, EndDT, User , Answertype 
FROM
(
SELECT Taskname  ,Checklist, Start ,End,Training, StartDT, EndDT, User , Answertype ,
ROW_NUMBER()OVER(PArtition by Taskname ORder by StartDT DESC) AS Dup
FROm YourTable
)
DT WHERE dt.Dup=1

答案 1 :(得分:1)

您可以查询如下:

word="TODO:"
with open("todolist.txt") as file:    
    for line in file:
        if word in line:
            print("#TODO:" + line.split(word)[-1])

答案 2 :(得分:1)

[1]因为原始要求是为每个任务名称获取一行:

  

对我来说哪一行没关系

我会将ROW_NUMBER函数用于OVER( ... ORDER BY (SELECT NULL)),其中ORDER BY (SELECT NULL)表示行的顺序无关紧要:

;WITH BaseQuery 
AS (
SELECT  t1.Taskname, ... other columns..., ROW_NUMBER() OVER(PARTITION BY t1.Taskname ORDER BY (SELECT NULL)) AS RowNum
FROM    dbo.Table1 AS t1
)
SELECT ...
FROM BaseQuery bq
WHERE bq.RowNum = 1

[2] OVER(ORDER BY ColumnName)OVER(ORDER BY (SELECT NULL))之间的差异可以在以下示例中看到,我们可以看到OVER(ORDER BY ColumnName)向执行计划添加一个运算符:Sort其中({1}}通常)很贵:

enter image description here

底线:ROW_NUMBER() OVER(ORDER BY (SELECT NULL))ROW_NUMBER() OVER(ORDER BY ColumnName)便宜。

警告:不要使用OVER(... ORDER BY (SELECT NULL))行的顺序很重要!