Sql 存在多行时设置列值

Sql 存在多行时设置列值,sql,ms-access,Sql,Ms Access,我有一个扁平化的数据集,其中包括订单#、装运#和装运费用。每个订单可以有多个装运,但装运费用在订单级别收取。以下是一个示例数据集: 1, 1, $5.00 2, 1, $6.00 2, 2, $6.00 3, 1, $10.00 3, 2, $10.00 3, 3, $10.00 4, 1, $4.00 如您所见,订单的发货费用在数据集中对每批货物重复。我需要提出一个查询,当订单上有多个装运时,该查询将装运费用设置为0。生成的数据集如下所示: 1, 1, $5.00 2, 1, $6.00 2

我有一个扁平化的数据集,其中包括订单#装运#装运费用。每个订单可以有多个装运,但装运费用在订单级别收取。以下是一个示例数据集:

1, 1, $5.00
2, 1, $6.00
2, 2, $6.00
3, 1, $10.00
3, 2, $10.00
3, 3, $10.00
4, 1, $4.00
如您所见,订单的发货费用在数据集中对每批货物重复。我需要提出一个查询,当订单上有多个装运时,该查询将装运费用设置为0。生成的数据集如下所示:

1, 1, $5.00
2, 1, $6.00
2, 2, $0.00
3, 1, $10.00
3, 2, $0.00
3, 3, $0.00
4, 1, $4.00
重要的是要注意,装运35;编号并不是每个订单都重置为1。我在示例数据集中这样做是为了更容易理解Shipping#实际上是一个连续整数,每次创建一次装运都会递增,因此一个简单的
更新数据集集ShippingCharges=0,其中Shipping#>1
不是答案

似乎当一个订单有多个装运时,我需要执行
更新
,但仅适用于装运#大于订单最小装运#的行

你知道这个查询是什么样子的吗,特别是对于Microsoft Access

更新数据集集集ShippingCharges=0,其中装运#>1为 答案是

然后,如果该订单的发货量与最低发货量不匹配,则将费用设置为零


这是用Oracle编写的,所以不确定您是否可以在Access中执行类似的操作。 在表中选择子项以查看是否为最小装运,然后使用装运费用或将其清除

select a.order_num, a.shipment_num, 
case when a.shipment_num = (
  select min(b.shipment_num) 
  from order_table b
  where b.order_num = a.order_num
) then max(a.shipment_charges) else '0' end
from order_table a
group by order_num, shipment_num
order by order_num, shipment_num

如果您将
1
2
设置为零(在您的
$10.00
示例中),并保留与记录计数匹配的数字值,会怎么样?非常好,谢谢!我不知道DMin函数,试图用Access中的纯SQL实现它,但失败得很惨。这很有效,不客气。当您有空闲时间时,请查看其他访问域功能:
DLookup
<代码>DMax<代码>数据计数;它们之所以有用的原因之一是它们可以帮助克服Access的查询不可更新的问题。但它们不能移植到其他SQL方言中。
DMin("[Shipment#]", "dataset", "[Order#]='" & [Order#] & "'")
select a.order_num, a.shipment_num, 
case when a.shipment_num = (
  select min(b.shipment_num) 
  from order_table b
  where b.order_num = a.order_num
) then max(a.shipment_charges) else '0' end
from order_table a
group by order_num, shipment_num
order by order_num, shipment_num