正如标题所说。我想计算每个分支的每月总销售额。
每个var variable = "keyname";
angular.forEach($scope.arrCategory[0][variable], function(k,d) {
angular.forEach(k, function(kk, dd) {
alert(dd);
});
});
都有Branch
(配额)
我有这个数据库架构:
Monthly Target
我希望有一个结果显示每月每个分支的TotalSales,包括没有`Sales.Branch`
- Id
- Name
Sales.BranchTarget
- Id
- BranchId
- Month
- Year
Sales.Transaction
- Id
- Date
- BranchId
Sales.TransactionItem
- Id
- Pages
- Rate
的月份,只要它们有Transaction
。
在图片上,如果我在2016年1月至3月期间为分支机构添加了Target 即使没有交易,我仍然应该看到三月,或者月份是未来一个月。
我有这个查询,但没有交易
就没有返回月份Target
更新:作为Bhavesh Harsora的查询答案
select
b.Name,
SUM(ti.Pages * ti.Rate) as TotalSales,
SUM(ti.Pages) as TotalPages,
bt.Amount,
bt.Month,
bt.Year
from
Sales.BranchTarget bt
left join Sales.[Transaction] t on bt.BranchId = t.BranchId
left join Sales.TransactionItem ti on ti.TransactionId = t.Id
left join Sales.Branch b on b.Id = t.BranchId
where
MONTH(t.Date) = bt.Month
AND YEAR(t.Date) = bt.Year
group by bt.Month, bt.Year, bt.Amount, b.Name
order by bt.Month, bt.Year, b.Name
但结果是
select
b.Name,
SUM(ti.Pages * ti.Rate) as TotalSales,
SUM(ti.Pages) as TotalPages,
bt.Amount,
bt.Month,
bt.Year
from Sales.BranchTarget bt
left join Sales.[Transaction] t
join Sales.Branch b
on b.Id = t.BranchId
on bt.BranchId = t.BranchId
AND MONTH(t.Date) = bt.Month
AND YEAR(t.Date) = bt.Year
left join Sales.TransactionItem ti
on ti.TransactionId = t.Id
group by bt.Month, bt.Year, bt.Amount, b.Name
order by bt.Month, bt.Year, b.Name
任何建议都将不胜感激。
谢谢。
答案 0 :(得分:1)
以下是修改过的查询:
select
b.Name,
SUM(ti.Pages * ti.Rate) as TotalSales,
SUM(ti.Pages) as TotalPages,
bt.Amount,
bt.Month,
bt.Year
from Sales.BranchTarget bt
left join Sales.[Transaction] t
on bt.BranchId = t.BranchId
AND MONTH(t.Date) = bt.Month
AND YEAR(t.Date) = bt.Year
left join Sales.TransactionItem ti
on ti.TransactionId = t.Id
left join Sales.Branch b
on b.Id = bt.BranchId
group by bt.Month, bt.Year, bt.Amount, b.Name
order by bt.Month, bt.Year, b.Name