selectクエリで、両方のテーブルに1つの列に存在するアイテムの数と、1つのテーブルにのみ存在するアイテムの数を取得しようとしています。これらの2つのクエリは、必要なデータを提供しますが、1つのクエリから取得したいと考えています。前もって感謝します。
select
d.description,
count(pd.item_id)
from productdetails pd
join inventory i on i.itemnum=pd.item_id
inner join departments d on d.dept_id=i.dept_id
where i.last_sold is not null and i.in_stock !=0 and len(i.itemnum) >4 and d.DEPT_ID NOT IN ('1010','1010c','1010cc','1010h','1010h','1011','1012','1013','1014','1015','1016','1017','1018','1015p','1015d','1015s','1015u','1251','1401','2000')
group by d.description
select d.description,
count(pd.item_id)
from productdetails pd
join inventory i on i.itemnum=pd.item_id
inner join departments d on d.dept_id=i.dept_id
group by d.description
望ましい結果:
SUMステートメントとIIFステートメントを組み合わせて、目標を達成できます。
select d.description,
count(pd.item_id),
SUM(IIF(i.last_sold is not null and i.in_stock !=0 and len(i.itemnum) >4 and d.DEPT_ID NOT IN ('1010','1010c','1010cc','1010h','1010h','1011','1012','1013','1014','1015','1016','1017','1018','1015p','1015d','1015s','1015u','1251','1401','2000'), 1, 0))
from productdetails pd
join inventory i on i.itemnum=pd.item_id
inner join departments d on d.dept_id=i.dept_id
group by d.description
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加