我们有一个包含三列(public partial class Form1 : Form
{
List<Product> source;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
source = new List<Product>();
}
private void button1_Click(object sender, EventArgs e)
{
decimal price;
if (string.IsNullOrEmpty(txtProduct.Text) || !decimal.TryParse(txtPrice.Text, out price))
{
MessageBox.Show("Invalid values!");
return;
}
var existingProduct = source.Where(x=> x.ProductName==txtProduct.Text).SingleOrDefault();
if(existingProduct!=null)
{
existingProduct.Price += price;
}
else
source.Add(new Product {Price = price, ProductName = txtProduct.Text} );
listBox1.DataSource = null;
listBox1.DataSource = source;
}
}
public class Product
{
public string ProductName { get; set; }
public decimal Price { get; set; }
public override string ToString()
{
return string.Format("{0} = {1}", ProductName, Price.ToString("c"));
}
}
,Name_of_office
,Month
)的表格,如下所示:
Amount
应使用财政年度月计算。
通过公式总是从4月开始到选定的月份。
假设用户将选择6月(从下拉列表)
计算应该像这样进行
Name_Of_Office Month Amount
------------------------------------
DivisionBhopal 04 125
DivisionBhopal 05 50
DivisionBhopal 06 100
DivisionBhopal 10 125
DivisionSagar 04 600
DivisionSagar 05 520
DivisionSagar 06 400
DivisionSagar 10 100
这里我们不会添加oct金额,因为选择的月份是6月我们想要数据到6月
6月份,数据应如下所示:
Name_Of_Office Sum Upto Previous Month Present Month Total Amount
-----------------------------------------------------------------------------------------
DivisionBhopa April + May June April + May+ June
DivisionSagar April + May June April + May+ June
10月的数据应该是(如果用户选择12月):
Name_Of_Office Sum Upto Previous Month Present Month Total Amount
---------------------------------------------------------------------------
DivisionBhopal 175 100 275
DivisionSagar 1120 400 1520
答案 0 :(得分:0)
SELECT A.NAME_OF_OFFICE, B.SUM_UPTO_PREV_MONTH, A.AMOUNT,B.SUM_UPTO_CURR_MONTH
FROM TEMP A
INNER JOIN
(
SELECT NAME_OF_OFFICE, SUM(AMOUNT) SUM_UPTO_PREV_MONTH
FROM TEMP
GROUP BY NAME_OF_OFFICE
WHERE MONTH < 6
) B ON A.NAME_OF_OFFICE = B.NAME_OF_OFFICE
INNER JOIN
(
SELECT NAME_OF_OFFICE, SUM(AMOUNT) SUM_UPTO_CURR_MONTH
FROM TEMP
GROUP BY NAME_OF_OFFICE
WHERE MONTH <= 6
) C ON A.NAME_OF_OFFICE = C.NAME_OF_OFFICE
WHERE A.MONTH = 6
答案 1 :(得分:0)
您可以使用条件聚合:
select office,
sum(amount)as total,
sum(case when month = @passedmonth then amount else 0 end) as present,
sum(case when month < @passedmonth then amount else 0 end) as prev
from table
where month >= 4 and month < @passedmonth
group by office