2012-06-01 2 views
2

J'essaie d'obtenir toutes les relations un à un dans une table plusieurs à plusieurs.Obtenir des enregistrements uniques sur les deux champs dans une relation plusieurs-à-plusieurs

Dans l'exemple ci-dessous le seul document que je veux revenir serait le dernier (3,3)

PaymentID InvoiceID 
1   1 
1   2 
2   2 
3   3 

Le plus proche que je l'ai obtenu est

Select * from Table where PaymentID in (
select PaymentID from Table t 
inner join (
    select InvoiceId from Table 
    group by InvoiceId 
    having count(InvoiceId) = 1 
) inv on t.InvoiceId = inv.InvoiceId 
group by PaymentId 
having count(PaymentId) = 1 
) 

Cela renverra 1 et 3

Des idées très appréciés

Merci.

Répondre

4
SELECT * 
FROM (
     SELECT *, 
       COUNT(*) OVER (PARTITION BY paymentId) AS pcnt, 
       COUNT(*) OVER (PARTITION BY invoiceId) AS icnt 
     FROM mytable 
     ) q 
WHERE pcnt = 1 
     AND icnt = 1 
Questions connexes