0

あなたの助けが必要です。レンタル中、在庫中、アウトレットにあるマシンをクエリするためのこのコードがありますが、これは itemID を入力した場合にのみ機能します。つまり、一度に 1 つのアイテムのみをクエリします。手持ちの在庫数と並行して、レンタルおよびアウトレットのマシンの数を照会する必要があります。

alter procedure GetItemsForQueries
@itemID varchar(15)
as begin 
select i.ItemName, m.MachineModel, i.SellingPrice, i.QuantityOnHand, 
(select COUNT(*) from ClientMachine where AcquisitionType = 'Rental' and ItemID = @itemID) as 'Quantity on Rentals',
(select COUNT(*) from OutletMachine where ItemID = @itemID) as 'Quantity on Outlets'
from Item i inner join Machine m on (m.ItemID = i.ItemID)
where i.ItemID = @itemID
end
4

3 に答える 3

0

このコードを確認してください:

select i.ItemName, 
       m.MachineModel, 
       i.SellingPrice, 
       i.QuantityOnHand, 
       (select COUNT(*) from ClientMachine where AcquisitionType = 'Rental' and ItemID =            i.ItemID) as 'Quantity on Rentals',
       (select COUNT(*) from OutletMachine where ItemID = i.ItemID) as 'Quantity on Outlets'
from Item i inner join Machine m on (m.ItemID = i.ItemID)
于 2013-05-10T17:26:19.817 に答える
0

私の頭のてっぺん(いくつかの構文エラーがあるかもしれませんが、ロジックはそこにあります)

select i.ItemId, i.ItemName, m.MachineModel, i.SellingPrice, i.QuantityOnHand, rental.rentalCount, outlet.outletCount
from Item i
left join (select ItemId, count(*) as 'rentalCount' from ClientMachine Where AcquisitionType = 'Rental' group by ItemId) rental on rental.ItemId = i.ItemId
left join (select ItemId, count(*) as 'outletCount' from OutletMachine group by ItemId) outlet on outlet.ItemId = i.ItemId
inner join Machine m on (m.ItemID = i.ItemID)
于 2013-05-10T17:24:00.727 に答える
0

2 つのサブクエリで実行する必要があります - このようなもの...

簡単に言えば、すべてのマシンを検索し、レンタルのカウント ID を検索するサブクエリに対して結合し、アウトレット マシンごとのカウントを検出する別のサブクエリに対して再度結合します...

select m.itemid, 
       ifnull(ccount,0) as rental_count,
       ifnull(ocount,0) as outlet_count
from Machine m
left join (select itemid,
             count(*) as ccount
             from ClientMachine 
             where AcquisitionType = 'Rental' group by ItemID) a1 on (a1.itemid=m.itemid)
left join (select itemid,
             count(*) as ocount
             from OutletMachine group by ItemID) a2 on (a2.itemid=m.itemid)
于 2013-05-10T17:20:56.467 に答える