1

我有 3 个表:Deliveries、IssuedWarehouse、ReturnedStock。

Deliveries: ID, OrderNumber, Material, Width, Gauge, DelKG
IssuedWarehouse: OrderNumber, IssuedKG
ReturnedStock: OrderNumber, IssuedKG

我想做的是按材料、宽度和规格对所有订单进行分组,然后将交付的数量相加,发给仓库并发回库存。

这是非常接近的 SQL:

SELECT 
    DELIVERIES.Material, 
    DELIVERIES.Width, 
    DELIVERIES.Gauge, 
    Count(DELIVERIES.OrderNo) AS [Orders Placed],
    Sum(DELIVERIES.DeldQtyKilos) AS [KG Delivered], 
    Sum(IssuedWarehouse.[Qty Issued]) AS [Film Issued], 
    Sum([Film Retns].[Qty Issued]) AS [Film Returned],
    [KG Delivered]-[Film Issued]+[Film Returned] AS [Qty Remaining]

FROM (DELIVERIES 
INNER JOIN IssuedWarehouse
    ON DELIVERIES.OrderNo = IssuedWarehouse.[Order No From]) 
INNER JOIN [Film Retns] 
    ON DELIVERIES.OrderNo = [Film Retns].[Order No From]
GROUP BY Material, Width, Gauge, ActDelDate
HAVING ActDelDate Between [start date] And [end date]
ORDER BY DELIVERIES.Material;

这几乎完美地组合了产品。但是,如果您查看结果:

Material    Width   Gauge   Orders Placed   Delivered Qnty Kilos    Film Issued Film Returned   Qty Remaining
COEX-GLOSS  590     75      1               534                     500         124             158
COEX-MATT   1080    80      1               4226                    4226        52              52
CPP         660     38      8               6720                    2768        1384            5336
CPP         666     47      1               5677                    5716        536             497
CPP         690     65      2               1232                    717         202             717
CPP         760     38      3               3444                    1318        510             2636
CPP         770     38      4               4316                    3318        2592            3590
CPP         786     38      2               672                     442         212             442
CPP         800     47      1               1122                    1122        116             116
CPP         810     47      1               1127                    1134        69              62
CPP         810     47      2               2250                    1285        320             1285
CPP         1460    38      12              6540                    4704        2442            4278
LD          975     75      1               502                     502         182             182
LDPE        450     50      1               252                     252         50              50
LDPE        520     70      1               250                     250         95              95
LDPE        570     65      2               504                     295         86              295
LDPE        570     65      2               508                     278         48              278
LDPE        620     50      1               252                     252         67              67
LDPE        660     50      1               256                     256         62              62
LDPE        670     75      1               248                     248         80              80
LDPE        690     47      1               476                     476         390             390
LDPE        790     38      2               2104                    1122        140             1122
LDPE        790     50      1               286                     286         134             134
LDPE        790     50      1               250                     250         125             125
LDPE        810     30      1               4062                    4062        100             100
LDPE        843     33      1               408                     408         835             835
LDPE        850     80      1               412                     412         34              34
LDPE        855     30      1               740                     740         83              83
LDPE        880     60      1               304                     304         130             130
LDPE        900     70      2               1000                    650         500             850
LDPE        1017    60      1               1056                    1056        174             174
OPP         25      1100    1               381                     381         95              95
OPP         1000    30      2               1358                    1112        300             546
OPP         1000    30      1               1492                    1491        100             101
OPP         1200    20      1               418                     417         461             462
PET         760     12      3               1227                    1876        132             -517

您会看到有些材料具有相同的宽度和规格,但它们没有分组。我认为这是因为订单上的交付数量不同。例如:

Material    Width   Gauge   Orders Placed   Delivered Qnty Kilos    Film Issued Film Returned   Qty Remaining
LDPE        620     50      1               252                     252         67               67
LDPE        660     50      1               256                     256         62               62

我希望将这两行分组。它们具有相同的材料、宽度和规格,但交货数量不同,因此未分组。

谁能帮我把这些奇怪的行分组?

4

2 回答 2

1

您的“问题”是交付发生在不同的日期,并且您正在分组,ActDelDate因此数据拆分,但由于您没有选择该ActDelDate列,这并不明显。

修复方法是:按列表从组中删除ActDelDate


您还应该删除第一个连接周围不必要的括号,并更改

HAVING ActDelDate Between [start date] And [end date]

WHERE ActDelDate Between [start date] And [end date]

_GROUP BY

于 2012-06-13T14:06:24.360 回答
0

您按交货日期分组,这导致行被拆分。要么从结果中省略交货日期并分组,要么采用交货日期的最小值/最大值。

于 2012-06-13T13:43:00.967 回答