4

特定の日付までの在庫履歴の利用可能なコンポーネントの数量とコストのデータを表示しています。以下のクエリを使用して正常に動作します。

SELECT s.part_id,
       sum(s.updated_quantity),
       p.item_code,
       sum(
             (SELECT (s.updated_quantity * cost)
              FROM inventory
              WHERE inventory.id=s.inv_id)) AS tcost
FROM status_history AS s,
     inventory AS i,
     part_master AS p
WHERE s.action='add'
  AND DATE(s.date_created)<='2013-04-09'
  AND i.currency_id=1
  AND s.inv_id=i.id
  AND s.part_id=p.id
GROUP BY s.part_id

また、コンマで区切られたコンポーネントの場所名を単一のフィールドに表示したいと考えています。私の欲求の結果を得るために、クエリの下で試しましたが、複数の場所名のカンマ区切りの文字列ではなく、1 つの場所名のみを返します。

SELECT s.part_id,
       sum(s.updated_quantity),
       p.item_code,
       sum(
             (SELECT (s.updated_quantity * cost)
              FROM inventory
              WHERE inventory.id=s.inv_id)) AS tcost,
       CONCAT_WS(',',
                   (SELECT name
                    FROM location_master
                    WHERE id=i.location_id)) AS LOCATION
FROM status_history AS s,
     inventory AS i,
     part_master AS p
WHERE s.action='add'
  AND DATE(s.date_created)<='2013-04-09'
  AND i.currency_id=1
  AND s.inv_id=i.id
  AND s.part_id=p.id
GROUP BY s.part_id
4

2 に答える 2

3

クエリの下に重複データを表示します:

select 
s.part_id,
sum(s.updated_quantity),
p.item_code,
sum(
     (select (s.updated_quantity * cost) from inventory where inventory.id=s.inv_id)) 
      as tcost,
GROUP_CONCAT(name) as location 
from status_history as s,
inventory as i,
part_master as p,
location_master as l 
where s.action='add' and 
DATE(s.date_created)<='2013-04-09' and 
i.currency_id=1 and 
s.inv_id=i.id and 
s.part_id=p.id and 
l.id=i.location_id 
group by s.part_id

クエリの下に個別のデータを表示します。

select 
s.part_id, 
sum(s.updated_quantity),
p.item_code,
sum(
     (select(s.updated_quantity * cost) from inventory where inventory.id=s.inv_id))
       as tcost,
GROUP_CONCAT(distinct(name)) as location 
from status_history as s,
inventory as i,
part_master as p,
location_master as l 
where s.action='add' and 
DATE(s.date_created)<='2013-04-09' and 
i.currency_id=1 and 
s.inv_id=i.id and 
s.part_id=p.id and 
l.id=i.location_id 
group by s.part_id
于 2013-04-12T11:28:17.913 に答える