0

現在の日付のブランチの値を取得したいのですが、現在の日付の値がない場合は、最新の読み取り値を取得します。

たとえば、選択した日付は 2013 年 9 月 29 日です。

私は3つの枝を持っています。

これらの支店のうち 2 つは、2013 年 9 月 29 日の売上値を持っています。

1 つのブランチにはエンコードされた値がありませんが、このブランチの最新の日付は 2013 年 8 月 30 日です。

言い換えると、

Branch 1 - Sep 29 - value is 150
Branch 2 - Sep 29 - value is 150
Branch 3 - Sep 29 - value is 0

150 + 150 + 0 = 300 はできません

私がしなければならないことは次のとおりです。

Branch 1 - Sep 29 - value is 150
Branch 2 - Sep 29 - value is 150
Branch 3 - Sep 29 - value is 0, so find the latest reading, system finds August 30 with value 250.

これで、150 + 150 + 250 = 550 を実行できます。

現在、次のSQLクエリがあります。

SELECT 
    user_id, product_code, uom, inventory_date, account_id, branch_id, beginning_inventory 

FROM 
    inventory_mgmt_uploads 

WHERE 
    user_id = '137'
    AND product_code = 'GRO_AL'
    AND uom = 'box'
    AND account_id = '3'
    AND inventory_date <= '2013-09-29'

ORDER BY
    inventory_date

上記のクエリの結果は次のとおりです。

ここに画像の説明を入力

今私が達成したいのは、この結果です:

ここに画像の説明を入力

私が試したのは、次のクエリです。

SELECT 
    user_id, product_code, uom, inventory_date, account_id, branch_id, beginning_inventory 

FROM 
    inventory_mgmt_uploads 

WHERE 
    user_id = '137'
    AND product_code = 'GRO_AL'
    AND uom = 'box'
    AND account_id = '3'
    AND inventory_date <= '2013-09-29'

GROUP BY
    branch_id

ORDER BY
    inventory_date

しかし、それは私に与えます:

ここに画像の説明を入力

branch_id desc または inventory_date desc で注文しようとしても、目的の出力が得られません。正しいクエリは何ですか?ティア!

4

2 に答える 2

0

これを試して::

Select * from inventory_mgmt_uploads outerimu 
INNER JOIN 
  (  SELECT 
        user_id, MIN(inventory_date) as minInvent, branch_id as Bid, MIN(beginning_inventory) as Binvent

    FROM 
        inventory_mgmt_uploads 

    WHERE 
        user_id = '137'
        AND product_code = 'GRO_AL'
        AND uom = 'box'
        AND account_id = '3'
        AND inventory_date <= '2013-09-29'

    GROUP BY
        branch_id
) as tempTab
    on (tempTab.user_id = outerimu.user_id and tempTab.minInvent=outerimu.inventory_date AND tempTab.Binvent =outerimu.beginning_inventory and tempTab.Bid= outerimu.branch_id)
    ORDER BY
    inventory_date
于 2013-10-11T07:10:44.213 に答える
0

これも試すことができます:

SELECT a.USER_ID, a.PRODUCT_CODE, a.UOM, MAX(a.INVENTORY_DATE), a.ACCOUNT_ID, a.BRANCH_ID, (
  SELECT BEGINNING_INVENTORY FROM test 
  WHERE user_id = a.user_id
  AND product_code = a.product_code
  AND uom = a.uom
  AND inventory_date = MAX(a.inventory_date)
  AND account_id = a.account_id
  AND branch_id = a.branch_id
) as BEGINNING_INVENTORY
FROM test as a
WHERE a.INVENTORY_DATE <= '2013-09-29'
GROUP BY a.USER_ID, a.product_code, a.uom, a.account_id, a.branch_id

Sashi Kant が言及したクエリは、連続したデータがあるため正しく機能しました (beginning_inventory は日付とともに減少します)。データがスクランブルされている場合、上記のアプローチでは正しいデータが得られません。

于 2013-10-11T07:31:24.110 に答える