在SQL Server上获取内部联接语句的重复值

时间:2013-05-29 04:36:51

标签: sql-server duplicates inner-join

我有一个内部连接语句来获取在3进程上运行的id并登录到三个表,

Select  a.InquiryAccount ,a.InquiryAmount,a.InquiryCustName,a.InquiryLang, b.PayAmountPaid,b.PayScreenText,b.PayReceiptText, c.RevRefNo
from
    inquiry a
        inner join
    Payment b
        on a.InquiryAccount = b.PayAccount
        inner join 
    Reversal c
        on a.InquiryAccount = c.RevAccount

order by c.RevRefNo desc

并且它给出了我想要的结果,但是有些值加倍或重复,我想跟踪在内连接reult上复制的帐户。

我尝试在我的内部联接上做这样的事情:

SELECT *
FROM Inquiry
WHERE InquiryAccount IN (
 SELECT InquiryAccount
 FROM Inquiry
 GROUP BY InquiryAccount 
 HAVING (COUNT(InquiryAccount ) > 1)
) 

显示唯一重复的帐户。 但是当我在group by上尝试Inner Join时会收到错误,group by上的inner join是否可以?或者在inner join声明中找到重复值的解决方案吗?

这是我的内部联接声明的结果:

InquiryAccount  InquiryAmount   InquiryLang
    10176601              124070    0
    12344556              160050    1
    10445654              160050    1
    23456789              160050    1
    22456666              160050    1
    45324681              160050    1
    14356890              160050    1
    44566666              160050    1
    22456666              160050    1
    10176601              160050    1

这是我想要从我的内部联接语句中生成的示例,它只获得重复的帐户:

InquiryAccount  InquiryAmount   InquiryLang
10176601              124070    0
10176601              160050    1
22456666              160050    1
22456666              160050    1

1 个答案:

答案 0 :(得分:2)

这可能对你有所帮助 -

SELECT DISTINCT a.InquiryAccount
        ,a.InquiryAmount
        ,a.InquiryCustName
        ,a.InquiryLang
        ,b.PayAmountPaid
        ,b.PayScreenText
        ,b.PayReceiptText
        ,c.RevRefNo 
FROM dbo.inquiry a 
JOIN dbo.Payment b ON a.InquiryAccount = b.PayAccount 
JOIN dbo.Reversal c ON a.InquiryAccount = c.RevAccount
ORDER BY c.RevRefNo DESC