現在、ac#、MVC3、NHibernate (MSSQL 2008) アプリケーションに取り組んでいます。これを理解するための最良の方法だと思うので、簡単な例を示します。
モデル:
class Stock
{
public Company Company {get;set;}
public Product Product {get;set;}
public int quantity {get;set;}
}
class TransactionHeader
{
public Company FromCompany {get;set;}
public Company ToCompany {get;set;}
public IList<TransactionRow> Rows {get;set;}
}
class TransactionRows
{
public Product Product {get;set;}
public TransactionHeader Header {get;set;}
public int Quantity {get;set;}
}
class Company
{
public Country Country {get;set;}
public State State {get;set;}
public string Name {get;set;}
}
私の最終的な目標は、以下を含むリストを作成することです。
製品名、会社名、国名、州名、現在の在庫数、入荷在庫数、出荷在庫数
次のクエリを使用して、現在の在庫に対してこれを行うことができます。
var stockQuery = session.QueryOver<Stock>(() => s)
.JoinAlias<Company>(() => s.company, () => c, NHibernate.SqlCommand.JoinType.InnerJoin, null)
.JoinAlias<State>(() => c.State, () => state, NHibernate.SqlCommand.JoinType.InnerJoin, null)
.JoinAlias<Country>(() => c.Country, () => country, NHibernate.SqlCommand.JoinType.InnerJoin, null)
.JoinAlias<Product>(() => s.product, () => p, NHibernate.SqlCommand.JoinType.InnerJoin, null);
if (productId.HasValue) { stockQuery = stockQuery.Where(() => p.Id == productId); }
var stock = stockQuery.SelectList(list => list
.Select(() => s.currentStock).WithAlias(() => rowVM.CurrentStock)
.Select(() => c.Description).WithAlias(() => rowVM.CompanyName)
.Select(() => state.Description).WithAlias(() => rowVM.StateName)
.Select(() => country.Description).WithAlias(() => rowVM.CountryName)
.Select(() => p.Description).WithAlias(() => rowVM.ProductName))
.TransformUsing(Transformers.AliasToBean<StockLevelRowVM>())
.List<StockLevelRowVM>();
私が立ち往生しているのは、TransactionHeader / TransactionRow データに基づいて、入庫と出庫の在庫を追加する方法です。入荷在庫数を取得するためのクエリでの私の試みは次のとおりです。
var incommingQuery = session.QueryOver<TransactionRow>(() => itr)
.JoinAlias<TransactionHeader>(() => itr.TransactionHeader, () => ith, NHibernate.SqlCommand.JoinType.InnerJoin, null)
.JoinAlias<Company>(() => ith.ToCompany, () => c, NHibernate.SqlCommand.JoinType.InnerJoin, null)
.JoinAlias<Product>(() => itr.Product, () => p, NHibernate.SqlCommand.JoinType.InnerJoin, null)
.SelectList(list => list
.SelectGroup(() => new { ith.ToCompany, itr.Product })
.Select(() => itr.Quantity).WithAlias(() => detail.Quantity)
.Select(() => p.Id).WithAlias(() => detail.ProductId)
.Select(() => c.Id).WithAlias(() => detail.CompanyId))
複数のプロパティによるグループ化が機能していないようです:
Could not determine member from new <>f__AnonymousType3`2(ToCompany = value(<Stuff>).ith.ToCompany, Product = value(<Stuff>).itr.Product)
したがって、これは2つの質問につながります:
- 私はこの全体について間違っていますか?ストアド プロシージャの方が優れたソリューションでしょうか。
- このアプローチが問題ない場合、どうすれば複数のフィールドでグループ化できますか?
編集:私が生成したいSQLの種類の例:
SELECT c.Description CompanyName,
p.Description ProductName,
cc.Description CountryName,
cs.Description StateName,
tmp.CurrentStock,
tmp.OutgoingStock,
tmp.IncommingStock
FROM
(
SELECT results.CompanyId,
results.ProductId,
SUM(results.CurrentStock) CurrentStock,
SUM(results.IncommingStock) IncommingStock,
SUM(results.OutgoingStock) OutgoingStock
FROM (
SELECT
c.Id CompanyId,
p.Id ProductId,
s.CurrentStock,
0 IncommingStock,
0 OutgoingStock
FROM Stock s
INNER JOIN Company c ON c.Id = s.Company
INNER JOIN [State] cs ON cs.Id = c.[State]
INNER JOIN Country cc ON cc.Id = cs.Country
INNER JOIN Product p ON p.Id = s.Product
WHERE (@CompanyId IS NULL OR c.Id = @CompanyId) AND
(@CountryId IS NULL OR cc.Id = @CountryId) AND
(@StateId IS NULL OR cs.Id = @StateId) AND
(@ProductId IS NULL OR p.Id = @ProductId)
UNION
SELECT
c.Id CompanyId,
p.Id ProductId,
0 CurrentStock,
0 IncommingStock,
tr.Quantity OutgoingStock
FROM TransactionRow tr
INNER JOIN TransactionHeader th ON th.Id = tr.TransactionHeader
INNER JOIN Company c ON c.Id = th.FromCompany
INNER JOIN [State] cs ON cs.Id = c.[State]
INNER JOIN Country cc ON cc.Id = cs.Country
INNER JOIN Product p ON p.Id = tr.Product
WHERE (@CompanyId IS NULL OR c.Id = @CompanyId) AND
(@CountryId IS NULL OR cc.Id = @CountryId) AND
(@StateId IS NULL OR cs.Id = @StateId) AND
(@ProductId IS NULL OR p.Id = @ProductId)
UNION
SELECT
c.Id CompanyId,
p.Id ProductId,
0 CurrentStock,
tr.Quantity IncommingStock,
0 OutgoingStock
FROM TransactionRow tr
INNER JOIN TransactionHeader th ON th.Id = tr.TransactionHeader
INNER JOIN Company c ON c.Id = th.ToCompany
INNER JOIN [State] cs ON cs.Id = c.[State]
INNER JOIN Country cc ON cc.Id = cs.Country
INNER JOIN Product p ON p.Id = tr.Product
WHERE (@CompanyId IS NULL OR c.Id = @CompanyId) AND
(@CountryId IS NULL OR cc.Id = @CountryId) AND
(@StateId IS NULL OR cs.Id = @StateId) AND
(@ProductId IS NULL OR p.Id = @ProductId)
) results
GROUP BY results.CompanyId, results.ProductId
) tmp
INNER JOIN Company c ON c.Id = tmp.CompanyId
INNER JOIN [State] cs ON cs.Id = c.[State]
INNER JOIN Country cc ON cc.Id = cs.Country
INNER JOIN Product p ON p.Id = tmp.CompanyId
編集 2: 目的の SQL の 2 番目のバージョン
SELECT
c.Id CompanyId,
c.Description CompanyName,
p.Id ProductId,
p.Description ProductName,
cs.Description StateName,
cc.Description CountryName,
SUM(s.CurrentStock) CurrentStock,
SUM(ttr.Quantity) IncommingStock,
SUM(ftr.Quantity) OutgoingStock
FROM Stock s
INNER JOIN Company c ON c.Id = s.Company
INNER JOIN [State] cs ON cs.Id = c.[State]
INNER JOIN Country cc ON cc.Id = cs.Country
INNER JOIN Product p ON p.Id = s.Product
LEFT JOIN TransactionHeader fth ON fth.FromCompany = c.Id
LEFT JOIN TransactionRow ftr ON ftr.TransactionHeader = fth.ID
AND ftr.Product = p.Id
LEFT JOIN TransactionHeader tth ON tth.ToCompany = c.Id
LEFT JOIN TransactionRow ttr ON ttr.TransactionHeader = tth.ID
AND ttr.Product = p.Id
WHERE (@CompanyId IS NULL OR c.Id = @CompanyId) AND
(@CountryId IS NULL OR cc.Id = @CountryId) AND
(@StateId IS NULL OR cs.Id = @StateId) AND
(@ProductId IS NULL OR p.Id = @ProductId)
GROUP BY p.Id, c.Id, c.Description, p.Description,
cs.Description, cc.Description