[英]SQL query to find values based on selected date, but will find the latest value if there's no value on the selected date
I want to get the branch values of the current date, but if there's no value for the current date, get the value of its latest reading. 我想获取当前日期的分支值,但是如果当前日期没有任何值,请获取其最新读数的值。
For example, the selected date is September 29, 2013. 例如,所选日期为2013年9月29日。
I have three branches. 我有三个分支。
Two of those branches has sales value for September 29, 2013. 这些分支机构中有两个的销售价值为2013年9月29日。
One branch does not have encoded values, but this branch has a latest value date August 30, 2013. 一个分支没有编码值,但是此分支的最新日期为2013年8月30日。
In other words, 换一种说法,
Branch 1 - Sep 29 - value is 150
Branch 2 - Sep 29 - value is 150
Branch 3 - Sep 29 - value is 0
I cannot just do 150 + 150 + 0 = 300 我不能只做150 + 150 + 0 = 300
What I have to do is: 我要做的是:
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.
So now I can do 150 + 150 + 250 = 550 所以现在我可以做150 + 150 + 250 = 550
Currently, I have the following SQL query: 当前,我有以下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
The result of the query above is: 上面查询的结果是:
Now what I want to achieve is this result: 现在,我要实现的结果是:
What I've tried is this query: 我试过的是此查询:
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
But it gives me: 但这给了我:
Even if I tried to do an order by branch_id desc, or inventory_date desc, I still can't get my desired output. 即使我尝试按branch_id desc或ventory_date desc进行订单,我仍然无法获得所需的输出。 Any ideas what will be the correct query?
任何想法什么是正确的查询? TIA!
TIA!
Try this:: 尝试这个::
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
You can try this also: 您也可以尝试以下操作:
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
The query mentioned by Sashi Kant worked correctly because you have sequential data (beginning_inventory decreases with date). Sashi Kant提到的查询可以正常工作,因为您具有顺序数据(beginning_inventory随着日期减少)。 If the data was scrambled, the above approach won't give correct data.
如果数据被加密,上述方法将无法提供正确的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.