我使用SQL Anywhere作为我的数据库。
我有两个表SALES DETAIL (POSDETAIL)
和库存调整表(AdjustInventory
),并创建和查询以显示销售和浪费/调整后的查询。
输出应该是这样的
Product SalesQty Value WastageQty Value
------------------------------------------------
销售数量将来自POSDETAIL
表,WastageQty
来自AdjustInventory
POSDETAIL
共有435625条记录,AdjustInventory
共有183528条记录。
我设计了下面的查询它根据需要给我完美,但问题是查询非常非常慢,在任何日期范围内显示任何特定数据需要大约10到15美元。
此查询是必需的优化,此处调整是查询。
select
p.OrderDate, p.ProductId, p.SalesQty,
f.WastedQty / p.SalesQty as WastedQty,
p.NetCost, p.EachCost
from
(select
POSDETAIL.PRODNUM as ProductId,
DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd') as OrderDate,
POSDETAIL.NETCOSTEACH as NetCost,POSDETAIL.COSTEACH as EachCost,
SUM(POSDETAIL.QUAN) as SalesQty
from
DBA.POSDETAIL
group by
POSDETAIL.PRODNUM, POSDETAIL.NETCOSTEACH, POSDETAIL.COSTEACH,
DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')
) as p (ProductId, OrderDate, NetCost, EachCost, SalesQty)
left outer join
(select
DBA.AdjustInventory.INVENNUM as ProductId,
DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd') as OrderDate,
SUM(AdjustInventory.ADJUSTUNITS) as WastedQty
from
DBA.POSDETAIL
join
DBA.AdjustInventory on DBA.POSDETAIL.PRODNUM = DBA.AdjustInventory.INVENNUM
and DBA.AdjustInventory.AdjustType = 9
and DATEFORMAT(DBA.AdjustInventory.AdjustTime,'yyyy/mm/dd') = DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')
group by
DBA.AdjustInventory.INVENNUM, DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')
) as f(ProductId, OrderDate, WastedQty) on p.ProductId = f.ProductId and p.OrderDate = f.OrderDate;
请帮忙
答案 0 :(得分:1)
好的,这可能是你的问题。 永远不要在嵌套选择中分组。 1.拉出选择,使其成为临时表 2.索引临时表 3.将临时表加入外部查询。
你应该能够在这里做两次,这应该会有所帮助。
答案 1 :(得分:0)
not 100% sure of syntax for SQL
select
POSDETAIL.PRODNUM as ProductId,
DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd') as OrderDate,
POSDETAIL.NETCOSTEACH as NetCost,POSDETAIL.COSTEACH as EachCost,
SUM(POSDETAIL.QUAN) as SalesQty
from
DBA.POSDETAIL
into #P
group by
POSDETAIL.PRODNUM, POSDETAIL.NETCOSTEACH, POSDETAIL.COSTEACH,
DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')
select
DBA.AdjustInventory.INVENNUM as ProductId,
DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd') as OrderDate,
SUM(AdjustInventory.ADJUSTUNITS) as WastedQty
from
DBA.POSDETAIL
Into #F
join
DBA.AdjustInventory on DBA.POSDETAIL.PRODNUM = DBA.AdjustInventory.INVENNUM
and DBA.AdjustInventory.AdjustType = 9
and DATEFORMAT(DBA.AdjustInventory.AdjustTime,'yyyy/mm/dd') = DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')
group by
DBA.AdjustInventory.INVENNUM, DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')
select
p.OrderDate, p.ProductId, p.SalesQty,
f.WastedQty / p.SalesQty as WastedQty,
p.NetCost, p.EachCost
from #p (ProductId, OrderDate, NetCost, EachCost, SalesQty)
Left outer Join #f
on p.ProductId = f.ProductId and p.OrderDate = f.OrderDate;
自从我使用SQL Anywhere以来已经过了大约16年,很抱歉这是psudeo代码