我有四张这样的桌子
表格1:MsPatient
PatientID PatientName
PA001 | Danny andrean
PA002 | John Travolta
PA003 | Danny Lee
表 2:TransactionHeader
TransactionID PatientID TransactionDate
TR001 | PA001 | 2012/12/6
TR002 | PA002 | 2013/11/4
TR003 | PA003 | 2010/4/12
表3:TransactionDetail
TransactionID MedicineID Quantity
TR001 | ME001 | 5
TR002 | ME001 | 6
TR003 | ME002 | 5
表 4:MsMedicine
MedicineID MedicineName MedicineStock
ME001 |HIVGOD |100
ME002 |CancerCure |50
我怎样才能显示购买的药物是“ME001”并且由2个或更多单词组成的显示PatientID
、、PatientName
和TotalMedicineBought
(从购买的药物数量中获得) 。MedicineID
PatientName
例子:
PatientID | PatientName | Total Medicine Bought
PA001 | Danny Andrean | 5
PA002 | John Travolta | 6
我试过这个查询:
select
mp.PatientID,mp.PatientName,SUM(td.Quantity) as TotalMedicineBought
from
MsPatient mp, TransactionDetail td
inner join
TransactionHeader th on th.TransactionID = td.TransactionID
Group by
td.TransactionID, mp.PatientID, mp.PatientName
我不知道如何创建一个包含两个单词的条件
我使用 SQL Server 2008