0

钢铁订单

OrderNumber
-----------
0000843989    
0000843124    
0000881234    
0000123456    
-----------

定稿印刷贴纸

SN                  |  PanelBuildTime
-------------------------------------
0000843989-8R8-4-0  |  360
0000843989-8R8-4-1  |  200
0000843989-8R8-4-2  |  900    
0000843989-8R8-4-3  |  360
0000843989-8R8-4-4  |  460
0000843989-8S-1-0   |  220    
0000843989-8-2-0    |  360
etc..
-------------------------------------

期望的结果是:

OrderNumber |  PanelBuildTime
-----------------------------
0000843989  |  2860    
etc..
-----------------------------

我尝试了什么?

Select OrderNumber, Sum(PanelBuildTime) from SteelOrders 
INNER JOIN FinalizedPrintedStickers 
ON 
SteelOrders.OrderNumber = LEFT(FinalizedPrintedStickers.SN,10)

不幸的是,我不知道如何WHERE在 steelorder 表中的每个项目上添加一个子句

另外(因为我在我的 C# 代码中使用 oledb 从钢铁订单表中读取)我已经尝试了每次读取OleDbDataReaderExecuting the Select Sum(PanelBuildtime) where ordernumber = reader["OrderNumber"],但这非常慢

4

2 回答 2

2

您可以按订单号分组以显示构建时间的摘要

Select OrderNumber, Sum(PanelBuildTime) from SteelOrders 
INNER JOIN FinalizedPrintedStickers 
ON 
SteelOrders.OrderNumber = LEFT(FinalizedPrintedStickers.SN,10)
GROUP BY SteelOrders.OrderNumber 
于 2012-12-14T13:52:37.003 回答
1

因为您可能经常希望不同的列具有不同的条件,所以您还应该了解以下case语句:

Select OrderNumber, Sum(case when SteelOrders.item = '0000843989' then PanelBuildTime end)
from SteelOrders INNER JOIN
    FinalizedPrintedStickers 
    ON SteelOrders.OrderNumber = LEFT(FinalizedPrintedStickers.SN,10)

在 Access 中(对不起,我错过了标签),你必须使用IIF而不是case.

哦,根据您上面的评论,您想要一个group by

Select OrderNumber, Sum(PanelBuildTime)
from SteelOrders INNER JOIN
    FinalizedPrintedStickers 
    ON SteelOrders.OrderNumber = LEFT(FinalizedPrintedStickers.SN,10)
group by OrderNumber
于 2012-12-14T13:55:33.320 回答