【发布时间】:2013-12-13 20:01:37
【问题描述】:
使用经典的 ASP 和 MS-SQL server 2008
我要做的是选择重复的记录,并对它们进行计数,这样我就可以更新第一个记录并删除其余记录。
我的查询选择了重复项,但一旦我尝试引入计数,它就会停止使用消息“列 'calendar.id' 在选择列表中无效,因为它不包含在聚合函数或GROUP BY 子句。”
我试图重写这个并将计数添加到第二个 SELECT 并将 id 添加到 GROUP BY (以及我能想到的尽可能多的这类事情的组合,但我仍然得到错误。我看过的答案在 stackoverflow 上的解决方案似乎有点复杂,而且并不真正适用(或者我不是在寻找正确的东西)
我在哪里做错了? (这也是在小表(20 万行)中查找和编辑重复项的最佳方法吗?)
Dim strSQL_dup, rsSQL_dup, SQL_dupRecords, RecCount
strSQL_dup = "SELECT id, COUNT(id) AS RecCount FROM calendar WHERE start_date IN ( SELECT start_date FROM calendar WHERE pId = '" & pId & "' GROUP BY start_date HAVING (COUNT(start_date ) > 1) ) "
Set rsSQL_dup = conn.Execute(strSQL_dup)
While Not rsSQL_dup.EOF
If RecCount = 1 Then
'will eventually update the row
response.write(rsSQL_dup("id")) ' id of first'
Else
'will eventually delete the other rows
response.write(rsSQL_dup("id")) ' id of subsiquet rows
End If
rsSQL_dup.MoveNext
Wend
【问题讨论】:
-
@Mihai - 这是 calendar.id,没有下划线
-
与问题评论无关:您的代码(可能)非常容易受到 SQL 注入攻击。尝试将其放在 SP 中并避免使用(无参数)动态查询。
标签: sql sql-server asp-classic