-3

编辑(按要求):我更新了示例数据以显示我在数据库上运行 SELECT 时获得的所有真实数据。我可以确认数据很糟糕 - 它包含重复的记录。应用程序中存在错误,并且数据库对 (question,attempt,track_number) 没有唯一约束。我正在尝试清除不良数据 - 重复记录。为此,我需要获取那些不良记录的 tbl_survey.id (PK) 值。

桌子:

CREATE TABLE tbl_survey(
    id [bigint] IDENTITY(1,1) NOT NULL,
    question [bigint] NOT NULL,
    attempt [bigint] NOT NULL,
    track_number [int] NOT NULL,
CONSTRAINT tbl_survey_id_pk PRIMARY KEY CLUSTERED ([id] ASC)
)

数据:

id      question  attempt track_number  track_number_count
315 8418    2   2
316 8418    1   2
317 8418    2   2
318 8418    2   2
319 8418    1   2
320 8418    1   2
321 8418    1   2
323 8418    1   2
324 8418    1   2
325 8418    1   2
326 8418    1   2
327 8418    2   2
328 8418    1   2
329 8418    1   2
330 8418    1   2
331 8418    1   2
332 8418    1   2
333 8418    1   2
334 8418    1   2
335 8418    1   2
336 8418    1   2
337 8418    1   2
338 8418    1   2
339 8418    1   2
340 8418    1   2
341 8418    1   2
342 8418    1   2
343 8418    1   2
344 8418    1   2
345 8418    1   2
346 8418    1   2
347 8418    1   2
348 8418    1   2
349 8418    1   2
350 8418    2   2
351 8418    1   2
352 8418    2   2
353 8418    1   2
355 8418    1   2
357 8418    1   2
358 8418    1   2
359 8418    1   2
360 8418    1   2
361 8418    1   2
362 8418    1   2
363 8418    1   2
364 8418    1   2
365 8418    1   2
366 8418    1   2
367 8418    1   2
368 8418    1   2
369 8418    1   2
370 8418    1   2
371 8418    1   2
372 8418    1   2
373 8418    1   2
375 8418    1   2
376 8418    1   2
377 8418    2   2
378 8418    1   2
379 8418    2   2

使用上面的 MSSQL 2008 R2 表和数据,此查询将检索到的数据限制为我想要的行(即上面的数据):

SELECT
    question,
    attempt,
    track_number,
    COUNT (track_number) AS track_number_count
FROM tbl_survey
WHERE attempt = 8418
GROUP BY
    question,
    attempt,
    track_number
HAVING (COUNT(track_number_count) > 1 )
ORDER BY attempt, question;

如何更改该 SELECT 查询,以便它还为我提供该表中返回的每一行的“id”列?

目前我得到:

question  attempt  track_number  track_number_count
315       8418     2             2
317       8418     1             2

我想要额外的 id 列:

id      question  attempt  track_number  track_number_count
476585  315       8418     2             2
476606  317       8418     1             2

我做错了什么?如何让 id 列显示?

谢谢。

4

3 回答 3

2

我认为,一旦您获得所需的详细信息,您就可以将其返回到符合条件的那些 ID:

SELECT id,      question,  attempt,  track_number,  track_number_count
from 
tbl_survey ts
inner join 
(
    SELECT
    question,
    attempt,
    track_number,
    COUNT (track_number) AS track_number_count
FROM tbl_survey
WHERE attempt = 8418
GROUP BY
    question,
    attempt,
    track_number
HAVING (COUNT(track_number_count) > 1 )
) as matching
on
(ts.question=matching.question and ts.attempt=matching.attempt and ts.track_number=matching.track_number)
ORDER BY ts.attempt, ts.question;

无论如何都是这样的,但我不能 100% 确定它是否有意义。

于 2013-05-31T10:49:15.840 回答
0

这对我有用:

SELECT
    MAX(id),
    question,
    attempt,
    track_number,
    COUNT(track_number) AS track_number_count
FROM
    tbl_survey
WHERE
    attempt = 8418
GROUP BY
    question,
    attempt,
    track_number
HAVING
    (COUNT(track_number_count) > 1 )
ORDER BY
    attempt,
    question;
于 2013-06-03T06:48:52.627 回答
-1

我不确定,我无法让查询在 sql fiddle 上运行,但看起来你正在降低组的粒度,因此产生的计数更低。

较低的计数不大于 1。因此,您缺少行。

尝试将此处的最后一行更改为 > = 1:

HAVING (COUNT(track_number_count) >= 1 )
于 2013-05-31T10:40:08.277 回答