2014-11-21 64 views
0

我有一個包含日期,客戶編號和支付類型信息的數據集。單個客戶可以在相同或不同的日期使用不同的付款方式。我想找出使用​​多種支付方式的客戶的客戶號碼。示例如下:SQL Server中重複客戶的數量

Date CustNum PaymentType 
26/7/2014 1 type1 
27/7/2014 2 type1 
28/7/2014 3 type1 
29/7/2014 4 type2 
24/7/2014 2 type2 
24/7/2014 4 type2 
25/7/2014 6 type3 
30/7/2014 9 type3 
2/7/2014 1 type3 
26/7/2014 4 type4 
2/7/2014 2 type4 
3/7/2014 6 type4 
26/7/2014 3 type5 
26/7/2014 4 type5 

在上述數據集中,可以看到多個客戶已使用多種付款類型。我想要獲得所有這些客戶的客戶編號,以及他們使用的付款類型和每種付款類型的交易數量。

這裏是理想的結果:

PaymentType Custnum NumTransactions 
type1 1 1 
type3 1 1 
type1 2 1 
type2 2 1 
type4 2 1 
type1 3 1 
type5 3 1 
type2 4 2 
type4 4 1 
type5 4 1 

在這方面的任何幫助,將不勝感激。

+0

你的措辭似乎混淆起來,要回答你的問題的人。你能否在你的問題中發佈想要的結果以避免誤解? – Lamak 2014-11-21 18:45:43

+0

感謝您的建議,我已更新了該問題。 – Patthebug 2014-11-21 19:00:29

回答

1

現在你更新你的問題,這裏有一個辦法:

SELECT PaymentType, 
     CustNum, 
     COUNT(*) NumTransactions 
FROM dbo.YourTable A 
WHERE EXISTS(SELECT 1 
      FROM dbo.YourTable 
      WHERE CustNum = A.CustNum 
      GROUP BY CustNum 
      HAVING MIN(PaymentType) <> MAX(PaymentType)) 
GROUP BY PaymentType, 
     CustNum 
+0

優秀,這似乎工作。非常感謝你的幫助。 – Patthebug 2014-11-21 19:13:00

1

試試這個:

對於

誰使用一個以上的支付類型的客戶的數量。

select COUNT(PaymentType) CountOfCust,PaymentType,CustNum from Customers 
Having COUNT(PaymentType)>1 
GRPUP BY PaymentType,CustNum