4

这是我的示例表:

|"id","singlesaleid","quantity"
-------------------------------------
| 1,   "00278e0f",    37
| 2,   "00278e0f",    38
| 3,   "002ebd2e",    37
| 4,   "00380783",    37
| 5,   "003b3c35",    37
| 6,   "003b3c35",    38
| 7,   "0042170b",    38
| 8,   "00421b89",    37
| 9,   "00421b89",    38
| 10,  "00587f02",    37
| 11,  "00799433",    38
| 12,  "00799433",    37

如何选择仅选择和分组 singlesaleid 列中至少有两个重复值的行?

结果应该是:

|"id","singlesaleid","quantity"
-------------------------------------
| 1,   "00278e0f",      37
| 6,   "003b3c35",      38
| 9,   "00421b89",      38
| 12,  "00799433",      37

有人可以帮忙吗?

4

3 回答 3

3
SELECT  a.*
FROM    TableName a
        INNER JOIN
        (
            SELECT  singlesaleid, MIN(quantity) min_val
            FROM    TableName
            GROUP   BY singlesaleid
            HAVING  COUNT(*) > 1
        ) b a.singlesaleid = b.singlesaleid AND
            a.quantity = b.Min_Val
于 2013-03-19T09:15:35.293 回答
0

singlesaleid's如果您想要仅具有至少 2 个重复项的任何记录(仅一个),请尝试以下代码..

select * from table where rowid in 
(select min(rowid) from table
group by singlesaleid
having count(*)>=2)
于 2013-03-19T09:40:52.347 回答
0

您也可以将选项与EXISTS()运算符一起使用。虽然,我不太明白,应该从组中选择哪一行,在请求中选择了一个带有 MAX(t2.Id) 的行

SELECT *
FROM Tablename t1
WHERE EXISTS (
              SELECT *
              FROM Tablename t2
              WHERE t1.singlesaleid = t2.singlesaleid
              HAVING COUNT(*) > 1 AND MAX(t2.Id) = t1.id
              )

SQLFiddle上的演示

于 2013-03-19T13:22:32.423 回答