この問題が発生しました.次の形式の CSV ファイルがあります (顧客、購入したアイテムのペア):
customer1 item1
customer1 item2
customer1 item3
customer2 item4
customer2 item2
customer3 item5
customer3 item1
customer3 item2
customer4 item1
customer4 item2
customer5 item5
customer5 item1
今、クエリ結果に表示したい:
item x; item y; how many customers have bought itemx and item together
例えば:
item1 item2 3 (because cust1 and cust2 and cust3 bought item1 and item2 together)
item1 item5 1 (because cust5 and cust3 bought item1 and item5 together)
このクエリは、顧客がペアで購入したアイテムの可能なすべての組み合わせを返します。また、Pair(x, y) は Pair(y, x) と同じであることに注意してください。
SQL クエリは次のようになります。
SELECT a1.item_id, a2.item_id, COUNT(a1.cust_id) AS how_many_custs_bought_both
FROM data AS a1
INNER JOIN data AS a2
ON a2.cust_id=a1.cust_id AND a2.item_id<>a1.item_id AND a1.item_id<a2.item_id
GROUP BY a1.item_id, a2.item_id
C# で 1) 通常の for/foreach ループを使用し、2) LINQ を使用してそれを行うにはどうすればよいでしょうか?
最初にLINQで試してみましたが、LINQが結合句で複数のequalsキーワードをサポートしていないことに気付いたときに行き詰まりました。その後、通常のループを使用して実行しようとしましたが、非効率になり、1 秒間に 30 行 (CSV ファイルの行) しか処理できなくなりました。
お知らせ下さい!