用作子查询时的额外记录:Access
Extra records when used as a sub query: Access
我是一名具有基本 SQL 经验的新手开发人员,这个问题在过去几天一直 'doing my head in'。我已经在这里问了几次问题,我想……还没有……继续尝试。
我有一个 table:
ID
Store
Product_Type
Delivery_Window
Despatch_Time
Despatch_Type
Pallets
Cartons
- 许多其他列(
start_week
和 day_num
是其中两个)
我的目标是通过 product_type
获取商店列表,其中最小值 despatch_time
以及所有其他列信息。
我已经测试了基本查询。
SELECT Product_Type, Store, Min(Despatch_Time) as MinDes
FROM table
GROUP BY Store, Product_Type
效果很好,我按预期获得了 200 行。
现在我想要那 200 行有其他相关记录信息:Delivery_Window
、start_week
、等等
我尝试了以下方法。
SELECT * FROM Table WHERE EXISTS
(SELECT Product_Type, Store, Min(Despatch_Time) as MinDes
FROM table
GROUP BY Store, Product_Type)
我试过做内连接和右连接都返回了 200 多条记录,这是我原来的数量。
我检查了额外的记录,其中商店和产品类型的发货时间相同,但发货类型不同。
所以我需要帮助创建一个查询,在该查询中我通过初始子查询对其进行限制,但即使存在匹配的最小发货时间,它仍会按商店和产品类型将计数限制为一条记录。
当前查询是:
SELECT *
FROM table AS A INNER JOIN
(Select Min(Despatch_Time) as MinDue, store, product_type
FROM table
WHERE day_num = [Forms]![FRM_SomeForm]![combo_del_day] AND start_week =[Forms]![FRM_SomeForm]![txt_date1]
GROUP BY store, product_type) AS B
ON (A.product_type = B.product_type) AND (A.store = B.store) AND (A.Despatch_Time = B.MinDue);
我想你想要:
SELECT t.*
FROM table as t
WHERE t.Dispatch_Time = (SELECT MIN(t2.Dispatch_Time)
FROM table as t2
WHERE t2.Store = t.Store AND t2.Product_Type = t.Product_Type);
以上将 return 重复。为了避免重复,您需要一个键来提供唯一性。让我假设你有一个主键 pk
:
SELECT t.*
FROM table as t
WHERE t.pk = (SELECT TOP (1) t2.pk
FROM table as t2
WHERE t2.Store = t.Store AND t2.Product_Type = t.Product_Type
ORDER BY t2.Dispatch_Time, t2.pk
);
我是一名具有基本 SQL 经验的新手开发人员,这个问题在过去几天一直 'doing my head in'。我已经在这里问了几次问题,我想……还没有……继续尝试。
我有一个 table:
ID
Store
Product_Type
Delivery_Window
Despatch_Time
Despatch_Type
Pallets
Cartons
- 许多其他列(
start_week
和day_num
是其中两个)
我的目标是通过 product_type
获取商店列表,其中最小值 despatch_time
以及所有其他列信息。
我已经测试了基本查询。
SELECT Product_Type, Store, Min(Despatch_Time) as MinDes
FROM table
GROUP BY Store, Product_Type
效果很好,我按预期获得了 200 行。
现在我想要那 200 行有其他相关记录信息:Delivery_Window
、start_week
、等等
我尝试了以下方法。
SELECT * FROM Table WHERE EXISTS
(SELECT Product_Type, Store, Min(Despatch_Time) as MinDes
FROM table
GROUP BY Store, Product_Type)
我试过做内连接和右连接都返回了 200 多条记录,这是我原来的数量。
我检查了额外的记录,其中商店和产品类型的发货时间相同,但发货类型不同。
所以我需要帮助创建一个查询,在该查询中我通过初始子查询对其进行限制,但即使存在匹配的最小发货时间,它仍会按商店和产品类型将计数限制为一条记录。
当前查询是:
SELECT *
FROM table AS A INNER JOIN
(Select Min(Despatch_Time) as MinDue, store, product_type
FROM table
WHERE day_num = [Forms]![FRM_SomeForm]![combo_del_day] AND start_week =[Forms]![FRM_SomeForm]![txt_date1]
GROUP BY store, product_type) AS B
ON (A.product_type = B.product_type) AND (A.store = B.store) AND (A.Despatch_Time = B.MinDue);
我想你想要:
SELECT t.*
FROM table as t
WHERE t.Dispatch_Time = (SELECT MIN(t2.Dispatch_Time)
FROM table as t2
WHERE t2.Store = t.Store AND t2.Product_Type = t.Product_Type);
以上将 return 重复。为了避免重复,您需要一个键来提供唯一性。让我假设你有一个主键 pk
:
SELECT t.*
FROM table as t
WHERE t.pk = (SELECT TOP (1) t2.pk
FROM table as t2
WHERE t2.Store = t.Store AND t2.Product_Type = t.Product_Type
ORDER BY t2.Dispatch_Time, t2.pk
);