SQL Server 2008 を使用しています
私は2つのテーブルを持っています
PS
列: PS_ID(PK)、AID(FK)、PS_Vendor
システム
列: System_ID(PK)、AID(FK)、Sys_Prod
2 つのテーブルは次のように接続されています。PS.AID = System.AID
PS_Vendors
for eachのカウントとeachSYS_Product
の合計を見つける必要がありますSys_product
。
私の出力は次のようになります。
PS_Vendor Sys_product Count Total
ABC A 2 10
ABC B 5 20
ABC C 0 15
XYZ A 3 10
XYZ B 0 20
XYZ C 12 15
というビューを作成しましたPS_system_View
:
select * from system
left outer join ps
on ps.aid=system.aid
組み合わせに困っています
select B.ps_vendor, A.sys_product, B.Count,A.Total from
--To get the total:
select sys_product, sum(total) as Total from
(select sys_product, ps_vendor, sum(case when sys_product is not null then 1 else 0 end) as total
from PS_System_view
)A
group by sys_product
left join
(
--To get the count:
select sys_product, ps_vendor, sum(case when ps_vendor is not null then 1 else 0 end) as count from
PS_System_view
where ps_vendor is not null and sys_product is not null
)B on B.sys_product=A.Sys_product