0

I am having problems with a SQL query, i need a list of consumers having purchased at least 3 different products whose suppliers are from a certain city (Lets say new york).

Tabels Columns:

Tb_Consumer..........Con_ID(PK), Name, City

Tb_Supplier.............Supp_ID(PK), Name, City

Tb_Transactions.....Tran_ID(PK), Supp_ID(FK), Con_ID(FK), PROD_ID(FK)

Tb_Products............Prod(ID(PK), Name

What i have so far:

var query8Result = (from c in context.Tb_Consumer
                                join t in context.Tb_Transactions on c.Con_ID equals t.Con_ID
                                join s in context.Tb_Supplier on t.Supp_ID equals s.Supp_ID
                                join p in context.Tb_Product on t.Prod_ID equals p.Prod_ID
                                where s.City == "New York" 
                                select new { Name = c.Name }).Distinct();
4

1 回答 1

0

我认为你需要一些 Group-by's

var query8Result = (from c in context.Tb_Consumer
                                join t in context.Tb_Transactions on c.Con_ID equals t.Con_ID
                                join s in context.Tb_Supplier on t.Supp_ID equals s.Supp_ID
                                join p in context.Tb_Product on t.Prod_ID equals p.Prod_ID
                                where s.City == "New York" 
                                group c by new { c.Name, t.Prod_ID } into customerProducts
                                group customerProducts by new { customerProducts.Key.Name } into customers
                                where customers.Count() > 3
                                select g.Key);

抱歉,如果这不是 100% 正确 - 测试这个有点困难......

于 2012-05-01T17:13:58.167 回答