我有一个文件名,该文件的smalldatetime标记,以及我的数据库中的表中该文件中包含的值,每个文件都有多个值,但文件名和时间戳将重复包含在其中的每个值表中的那个文件。
这是我需要做的。
我必须1)找到包含该值的最早的文件,然后2)计算该值在其他文件中出现的次数,将所有这些计数加起来,然后显示如下所示的表。
| | FileName | TimeStamp | Duplicates
| 1 | file1.txt | first | 30000
| 2 | file2.txt | second | 20000
等
我还必须以第二种方式格式化表格,就像这样......
| | FileName | file1.txt | file2.txt | file3.txt
| 1 | file1.txt | NULL | 15000 | 15000
| 2 | file2.txt | NULL | NULL | 20000
| 3 | file3.txt | NULL | NULL | NULL
任何帮助将不胜感激
select FileName
,min(TimeStamp) as TimeStamp
,count(Value) as Duplicates
from Table with (nolock)
where TimeStamp > min(TimeStamp)
group by FileName
having count(Value) > 1
我得到的错误是
Msg 147, Level 15, State 1, Line 11 An aggregate may not appear in the
WHERE clause unless it is in a subquery contained in a HAVING clause
or a select list, and the column being aggregated is an outer reference.
答案 0 :(得分:1)
好像你只想在where语句中没有聚合。 您可能需要更改时间戳的名称,或者只是将其添加到having语句中。
试试这个:
select FileName,
min(TimeStamp) as TimeStamp,
count(Value) as Duplicates
from Table with (nolock)
group by FileName
having count(Value) > 1
and TimeStamp > min(TimeStamp)