我尝试使用以下SQL创建查询以计算订单历史记录表中的价格变化。 Access给我一个警告"最多可以返回一条记录......"它用" #Name?"返回了一条记录在每个领域。有人可以帮我解决这个问题吗?非常感谢。
SELECT History.PartNumber,History.Datetime, History.Price, History.Quantity, (SELECT TOP 1 Price FROM History AS History1
WHERE History1.Datetime < History.Datetime
AND History1.PartNumber = History.PartNumber ORDER BY History1.Datetime DESC) as PreviousPrice,
[PreviousPrice]-[Price] as PriceDiff
FROM History
ORDER BY History.PartNumber, History.Datetime DESC
我测试了以下代码作为查询的子集,并收到以下错误: Error dialog box 点击OK按钮后,我得到了这个: #Name? in all fields
SELECT
history.partnumber,
history.datetime,
history.price,
history.quantity,
(SELECT TOP 1 price
FROM history AS History1
WHERE History1.datetime < history.datetime
AND History1.partnumber = history.partnumber
ORDER BY History1.datetime DESC
) AS PreviousPrice
FROM history
答案 0 :(得分:0)
您无法在 list<string> mylist = new list<string>;
foreach(string item in mylist)
{
///To get letters/alphabets
var letters = new String(item.Where(Char.IsLetter).ToArray());
///to get special characters
var letters = new String(item.Where(Char.IsSymbol).ToArray())
}
上计算价格变化,因为PreviousPrice
是PreviousPrice
子查询上的别名
使用子查询制作select
列
您可以尝试此查询。
PreviousPrice
如果不包含SELECT t.*,
(t.previousprice - t.price) AS PriceDiff
FROM (
SELECT
history.partnumber,
history.datetime,
history.price,
history.quantity,
(
SELECT TOP 1 price
FROM history AS History1
WHERE History1.datetime < history.datetime
AND History1.partnumber = history.partnumber
ORDER BY History1.datetime DESC
) AS PreviousPrice
FROM history
)t
ORDER BY t.partnumber,
t.datetime DESC
,我认为你需要使用coalesce
函数。
PreviousPrice
sqlfiddle:http://sqlfiddle.com/#!18/52a7b/5
修改强>
在我的访问中,此查询效果很好。
如果您想在 SELECT t.partnumber,
t.datetime,
t.price,
t.quantity,
coalesce(t.PreviousPrice,0) as 'PreviousPrice',
coalesce((t.previousprice - t.price),0) AS 'PriceDiff'
FROM (
SELECT
history.partnumber,
history.datetime,
history.price,
history.quantity,
(
SELECT TOP 1 price
FROM history AS History1
WHERE History1.datetime < history.datetime
AND History1.partnumber = history.partnumber
ORDER BY History1.datetime DESC
) AS PreviousPrice
FROM history
)t
ORDER BY t.partnumber,
t.datetime DESC
列添加货币符号,可以使用Format功能。
你可以试试这个。
PriceDiff
来源数据
查询结果
sqlfiddle:http://sqlfiddle.com/#!18/bfddb/6
修改#2 强>
我看到了您的源数据,源数据中有许多重复的SELECT t.*,
abs(t.previousprice - t.price) AS PriceDiff
FROM (
SELECT
history.partnumber,
history.datetime,
history.price,
history.quantity,
(
SELECT TOP 1 price
FROM history AS History1
WHERE History1.datetime < history.datetime
AND History1.partnumber = history.partnumber
ORDER BY History1.datetime DESC
) AS PreviousPrice
FROM history
)t
WHERE t.PreviousPrice is not null
ORDER BY t.partnumber,
t.datetime
。您不能在子查询上使用datetime
进行比较,否则子查询中会有多行。
因此您可以与datetime
列进行比较,因为它是您的ID
。
您可以使用PK
代替ID
datetime
sqlfiddle:http://sqlfiddle.com/#!18/bdeb91/1