2017-03-01 87 views
-1

編輯:我的措辭我的問題第一次走錯了路......道歉,我希望這個人會更清楚:比0選擇更大的價值,如果它存在,離開0,如果它是唯一的選擇

我在他可能有數量的所有可能的位置查詢某個物品(在我的情況下item_id = 321)的數量的數據庫。

我得到以下結果:

item_location_id item_def_loc_name qty_per_location item_id 
8     A08     0.00    321      
962     POL_113    30.00    321   
5     A05     60.00    321 

從這三個給出的結果(可能在某些情況下更多或更少),我們有一個包含零數量的結果和幾個人有更大數量的一條線 - 我我的目標是第一個「qty_per_location」大於零但小於下一個數量(在本例中爲30)。

但對於只有一個位置分配給他們的其他項目,查詢將返回唯一一個qty_per_location = 0的結果行 - 我需要它來顯示,因爲沒有更大的量。


所以,如果我有三個數量的庫存當前項目,如下面的例子,與qty = 30的那個將是所需的一個。但是,如果我只有一個位置是0的話 - 那麼我應該得到零。

enter image description here


你好再次 - 我結束了做一些擺弄和最終使用這個作爲一個可能的解決方案:

DECLARE @QTY as decimal(5,2); 

SELECT @QTY = (SELECT SUM(qty_per_location) 
FROM [asm].[dbo].[mdta_item-item_def_loc] 
where item_id = 321 
group by item_id) 

SELECT CASE (@QTY) WHEN 0 THEN 

(Select top 1 IsNull(qty_per_location, 0) as qty 
from [asm].[dbo].[mdta_item-item_def_loc] -- change this to your table name 
Where qty_per_location = 0 and item_id = 321 
Order by qty_per_location) 

ELSE 

(Select top 1 IsNull(qty_per_location, 0) as qty 
from [asm].[dbo].[mdta_item-item_def_loc] -- change this to your table name 
Where qty_per_location > 0 and item_id = 321 
Order by qty_per_location) 

END 

但是,這個腳本麻煩的是我無法將[qty_per_location]顯示爲qty結果上方的標題/列名 - 我得到「(無列名)」。此外,我無法在[qty_per_location]旁邊顯示[item_location_id]列以及...

請分享您的想法。 謝謝大家!

+1

什麼決定了結果集的順序? 'SELECT TOP 1 * FROM YourTable where qty_per_location> 0'需要'ORDER BY'來確定性。有沒有其他列你沒有列出?你也說過,如果你不關心有多少行會被返回,如果值大於0,但是接着說你只想返回第二行....爲什麼? – scsimon

+0

請將代碼/樣本數據/結果作爲文本發佈,而不是作爲其他網站上的圖像。 「i.stack.imgur.com目前無法處理此請求。」 – HoneyBadger

回答

1

這個查詢將返回表或0第一量值如果沒有與表正值任何行:如果排序的qty_per_location查詢

Select IsNull(firstQty.qty_per_location, 0) as qty_per_location, 
     tbl.item_location_id 
from mdta_item-item_def_loc tbl 
    left outer join (
     select top 1 tbl2.item_id, tbl2.qty_per_location, tbl2.item_location_id 
     from mdta_item-item_def_loc tbl2 
     Where tbl2.qty_per_location > 0 and tbl2.item_id = 321 
     Order by tbl2.qty_per_location 
    ) firstQty on tbl.item_id = firstQty.item_id and tbl.item_location_id = firstQty.item_location_id 
Where tbl.item_id = 321 

,你可以獲得最低或最高的價值。

+0

什麼決定了頂級1?由於您沒有通過...指定訂單,並且OP要求提供訂單的「第一可用肯定」,所以此值可能隨時間而改變。 – scsimon

+0

我在我的回答中解釋說,通過添加訂單,可以管理前1行。我沒有在顯示的模式中看到日期或時間戳字段,這表明系統正在跟蹤每個位置每個項目的最新數量。前1可以確定最低可用數量或最高,取決於排序順序 – Sparrow

+0

在qty_per_location上進行排序可能會得到期望的結果,如果OP構思的問題與我懷疑的一樣糟糕,但它不等於「第一個」 – scsimon

0

我要感謝所有善良的人,幫助我通過花費時間點我對我的方式:)

這是我所採用的解決我的這個SQL謎題,最終使用,滿足我的要求:

DECLARE @QTY as decimal(5,2); 
SET @QTY = 0; 

IF @QTY < (SELECT SUM(qty_per_location) 
FROM [asm].[dbo].[mdta_item-item_def_loc] 
where item_id = 321 
group by item_id) 

Select top 1 qty_per_location, [item_def_loc_name] 
from [dbo].[mdta_item-item_def_loc] 

inner join [dbo].[mdta_item_def_loc] on 
[dbo].[mdta_item_def_loc].[item_def_loc_id] = [dbo].[mdta_item-item_def_loc].[item_location_id] 

where item_id = 321 
order by qty_per_location desc 

ELSE 

Select top 1 qty_per_location, [item_def_loc_name] 
from [dbo].[mdta_item-item_def_loc] 

inner join [dbo].[mdta_item_def_loc] on 
[dbo].[mdta_item_def_loc].[item_def_loc_id] = [dbo].[mdta_item-item_def_loc].[item_location_id] 

where item_id = 321 

祝大家好!

相關問題