0

我有这个查询:

SELECT  trackingno, COUNT(trackingno) AS NumberOfTimes 
FROM shippinginfo
GROUP BY trackingno HAVING (COUNT(trackingno)>1) 
ORDER BY NumberOfTimes DESC

它报告了一些类似的东西

Trackingno  |  NumberofTimes
12345       |  2
23456       |  2
34567       |  3

有没有办法进行查询,以便我显示其他列数据?

Trackingno  |  OrderId
12345       |  oid1
12345       |  oid2
23456       |  oid3
23456       |  oid4
34567       |  oid5
34567       |  oid6

基本上,我在表中查找任何重复的跟踪号 (trackingno),然后报告重复跟踪号行的跟踪号和 orderid (orderid),以便我可以联系具有重复跟踪号的客户。

4

2 回答 2

2
SELECT *
  FROM shippinginfo
 WHERE trackingno IN (

    SELECT trackingno
      FROM shippinginfo
  GROUP BY trackingno
    HAVING COUNT(*) > 1

)

因此,在内部查询中,您选择所有trackingno重复的内容,然后在外部查询中选择您需要的任何内容

于 2013-07-19T03:32:35.677 回答
1
SELECT Trackingno,orderId 
FROM shippinginfo
WHERE Trackingno IN (
                      SELECT trackingno
                      FROM shippinginfo
                      GROUP BY trackingno
                      HAVING COUNT(*) > 1

                     )
于 2013-07-19T03:35:04.990 回答