难以获取以前的查询记录

时间:2016-09-08 09:13:18

标签: sql ms-access ms-access-2013

我有4个不同级别的商店。 (商店A,商店B,商店C,商店D)。商店保留物品。 (Item1,Item2,Item3,Item4和Item5)。

我有3张桌子(物品表,商店表, Tracking Table)和1个查询( Store_rec query for Item2 in Store B)。

跟踪表包含列(ID,T_Date,Item_ID,Item_Name,Store_From,Store_To,Amount,备注)。

Store_rec查询表用于检查商店中商品的更改(在我的情况下 - 商店B中的商品2)。它有5列(SrNo,T_Date,Location_To / From(从B到其他/从其他到B),Received(B余额增加)和Dispatched(B余额减少)。

我的问题是:

  1. 我不能按升序排序SrNo而不影响T_Date的行号。

  2. 我想在Store_rec查询中添加一个余额列,如图所示。 (余额=以前的余额+已收到 - 已调度)。

  3. 我在谷歌搜索,阅读论坛,教程和示例,但仍无法解决我的问题。

    请帮忙。

    SELECT (
            SELECT COUNT(aa.[ID]) + 1
            FROM Tracking AS aa
            WHERE (
                    t.[Store_From] = "Store B"
                    OR t.[Store_To] = "Store B"
                    )
                AND aa.[ID] < t.[ID]
            ) AS SrNo
        ,t.T_Date
        ,IIf(t.[Store_To] = "Store B", t.[Store_From], t.[Store_To]) AS [Location_To/From]
        ,IIf(t.[Store_To] = "Store B", t.[Amount], 0) AS Received
        ,IIf(t.[Store_From] = "Store B", t.[Amount], 0) AS Dispatched
    FROM Tracking AS t
    WHERE (
            t.[Store_From] = "Store B"
            OR t.[Store_To] = "Store B"
            )
        AND t.[Item_Name] = "Item 2"
    ORDER BY t.[T_Date];
    

3 个答案:

答案 0 :(得分:0)

SELECT  id ,
        CASE WHEN Store_From = 'Store B' THEN Store_To
             ELSE Store_From
        END AS Location_To ,
        CASE WHEN Store_To = 'Store B' THEN 0
             ELSE Amount
        END AS Dispatched ,
        CASE WHEN Store_From = 'Store B' THEN 0
             ELSE Amount


        END AS Received
INTO    #Tmp
FROM    dbo.Tracking
WHERE   Item_Id = 'Item_2'
        AND ( Store_From = 'Store B'
              OR Store_To = 'Store B'
            )

SELECT  T1.Location_To ,       
        T1.Received ,T1.Dispatched ,
        SUM(T.Dispatched) AS RemainingBal
FROM    #Tmp AS T1
        LEFT JOIN ( SELECT  ISNULL(( -CAST(Dispatched AS INT) + CAST(Received AS INT) ),
                                   0) AS Dispatched ,
                            id
                    FROM    #Tmp
                  ) AS T ON T.id <= T1.id
GROUP BY T1.Location_To ,
        T1.Dispatched ,
        T1.Received

答案 1 :(得分:0)

select  T1.id ,IIF(Store_From = 'Store B',Store_To, Store_From) ,IIF(Store_To = 'Store B',0, Amount) AS Dispatched, IIF(Store_From = 'Store B',0, Amount) AS Received,SUM(T.Received) AS RemainingBal
  From track  as T1

left join (

select  id,(-IIF(Store_To = 'Store B',0, Amount) + IIF(Store_From = 'Store B',0, Amount) ) AS Received
  From track 

) as  T ON T.id <= T1.id

WHERE   Item_Id = 'Item_2'
        AND ( Store_From = 'Store B'
              OR Store_To = 'Store B'
            )

GROUP BY 
        T1.Store_From ,
        T1.Store_To,T1.id,Amount
        ORDER BY T1.id

答案 2 :(得分:0)

感谢您的帮助。我在上面提供的代码中编辑了一些要点。代码如下

SELECT T1.TDate,IIF(T1.Store_From = 'Store B',T1.Store_To, T1.Store_From) AS Location, IIF(T1.Store_From = 'Store B',0, T1.Amount) AS Received, IIF(T1.Store_To = 'Store B',0, T1.Amount) AS Dispatched, SUM(T.Received) AS Balance FROM Tracking AS T1 LEFT JOIN (SELECT aa.id, aa.TDate, (-IIF(aa.Store_To = 'Store B',0, aa.Amount) + IIF(aa.Store_From = 'Store B',0, aa.Amount) ) AS Received FROM Tracking AS aa WHERE aa.Item_Name = 'Item 2')  AS T ON T.id <= T1.id WHERE Item_Name = 'Item 2' AND (Store_From = 'Store B' OR Store_To = 'Store B') GROUP BY T1.TDate,T1.Store_From, T1.Store_To, T1.id, Amount ORDER BY T1.TDate;