我正在尝试运行一个将计算" TOTAL COST"的查询。我目前的代码是:
SELECT [Main Query].SaltInfo_ID, Sum([Main Query].Tons) AS [Total Of Tons], Sum([Main Query].BlastTons) AS [Total Of BlastTons], Sum([Main Query].LiqGal) AS [Total Of LiqGal], Sum([Main Query].LiqBlastGal) AS [Total Of LiqBlastGal], IIf([Main Query].[EventDate]<#7/31/2016#,((Nz([Total Of Tons],0)+Nz([Total of BlastTons],0))*66.63)+((Nz([Total Of LiqGal],0)+Nz([Total of LiqBlastGal],0))*0.182),((Nz([Total Of Tons],0)+Nz([Total of BlastTons],0))*50.4)+((Nz([Total Of LiqGal],0)+Nz([Total of LiqBlastGal],0))*0.1594)) AS [TOTAL COST]
FROM [Main Query]
WHERE ((([Main Query].EventDate) Is Not Null))
GROUP BY [Main Query].SaltInfo_ID;
这很好用;但是我需要更改计算的[TOTAL COST]以根据[EventDate]计算值。如果活动日期在2016年7月31日之前,则Tons和BlastTons的成本为66.63美元,而LiqGal和LiqBlastGal的成本为0.182美元。 2016年7月31日之后,成本分别变为50.40美元和0.1594美元。你能帮忙正确编码,以便正确计算吗?我试过这个,但它没有用:
using System.Collections.Generic;
using System.Windows.Forms;
namespace FindTheCheckedBoxes
{
public partial class Form1 : Form
{
List<TestObject> list = new List<TestObject>();
List<int> positionId = new List<int>();
public Form1()
{
InitializeComponent();
PopulateDataGrid();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((bool)row.Cells[0].Value == true)
positionId.Add((int)row.Cells[1].Value);
}
// sets the window title to the columns found ...
this.Text = string.Join(", ", positionId);
}
void PopulateDataGrid()
{
list.Add(new TestObject { tick = false, LineNum = 1 });
list.Add(new TestObject { tick = true, LineNum = 2 });
list.Add(new TestObject { tick = false, LineNum = 3 });
list.Add(new TestObject { tick = true, LineNum = 4 });
dataGridView1.DataSource = list;
}
}
class TestObject
{
public bool tick { get; set; }
public int LineNum { get; set; }
}
}
当我尝试运行查询时,我收到错误:&#34;您试图执行不包含指定表达式的查询&lt; IIf([主要查询]。[EventDate]&lt;# 7/31/2016#,((Nz([总吨数],0)+ Nz([BlastTons总数],0))* 66.63)+((Nz([LiqGal总和],0)+ Nz( [LiqBlastGal的总和],0))* 0.182),((Nz([总吨数],0)+ Nz([BlastTons总数],0))* 50.4)+((Nz([LiqGal的总和] ,0)+ Nz([总和Li&#39;作为聚合函数的一部分。&#34;
我做错了什么?任何帮助将不胜感激!
答案 0 :(得分:0)
查询不是放置这种逻辑的地方。您需要另一张表来保存您的定价:
create table ProductCosts (Product varchar(255), StartDate datetime, EndDate DateTime, Cost Currency)
insert into ProductCosts(Product, Cost, StartDate,EndDate)
Select 'BlastTons', 66.63, '1/1/1990','7/31/2016'
union select 'LiqGal', 0.182, '1/1/1990','7/31/2016'
union select 'LiqBlastGal', 0.182,'1/1/1990','7/31/2016'
union Select 'BlastTons', 50.40, '8/1/2016','1/1/2050'
union select 'LiqGal', 0.1594, '8/1/2016','1/1/2050'
union select 'LiqBlastGal', 0.1594, '8/1/2016','1/1/2050'
然后你的最终查询可能是这样的:
SELECT [Main Query].SaltInfo_ID, Sum([Main Query].Tons) AS [Total Of Tons]
, Sum([Main Query].BlastTons) AS [Total Of BlastTons]
, Sum([Main Query].LiqGal) AS [Total Of LiqGal]
, Sum([Main Query].LiqBlastGal) AS [Total Of LiqBlastGal]
, ((Nz([Total Of Tons])+Nz([Total of BlastTons]))*BlastTons.Cost)+((Nz([Total Of LiqGal])*LiqGal.Cost + Nz([Total of LiqBlastGal]))*LiqBlastGal.Cost) AS [TOTAL COST]
FROM [Main Query] mq
inner join ProductCosts BlastTons on BlastTons.StartDate <= mq.[EventDate] and BlastTons.EndDate > mq.[EventDate] and BlastTons.Product = 'BlastTons'
inner join ProductCosts LiqGal on BlastTons.StartDate <= mq.[EventDate] and BlastTons.EndDate > mq.[EventDate] and BlastTons.Product = 'LiqGal'
inner join ProductCosts LiqBlastGal on BlastTons.StartDate <= mq.[EventDate] and BlastTons.EndDate > mq.[EventDate] and BlastTons.Product = 'LiqBlastGal'
WHERE ((([Main Query].EventDate) Is Not Null))
GROUP BY [Main Query].SaltInfo_ID;
理想情况下,每种类型的产品都没有联接,但这会让您首先更改Main Query
。