MS Access - Sub Query with Running Total using DSUM with filter

后端 未结 1 1381
感动是毒
感动是毒 2021-01-22 07:20

In order to generate running total of Sales Qty in MS Access, I used below query, it is working as expected

SELECT ID, [Product Line], DSUM(\"[Qty]\",\"[SalesDat         


        
相关标签:
1条回答
  • 2021-01-22 08:10

    Rather than using domain aggregate functions (such as DSum) which are known to be notoriously slow, I would suggest using a correlated subquery, such as the following:

    select q.* from
    (
        select t.id, t.[product line], t.qty, 
            (
                select sum(u.qty) 
                from salesdata u 
                where u.[product line] = t.[product line] and u.id <= t.id
            ) as runningtotal
        from salesdata t
        where t.[product line] like "*Electronics*"
    ) q
    where q.runningtotal < 100
    

    EDIT:

    select t.*, q.runningtotal from salesdata t inner join
    (
        select t.id, 
            (
                select sum(u.qty) 
                from salesdata u 
                where u.[product line] like "*Electronics*" and u.id <= t.id
            ) as runningtotal
        from salesdata t
    ) q on t.id = q.id
    where q.runningtotal < 100 and t.[product line] like "*Electronics*"
    
    0 讨论(0)
提交回复
热议问题