如何在三个表中汇总总和?

时间:2013-05-25 09:56:32

标签: sql database sql-server-2008

我有四张这样的表

表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

我如何展示展示PatientIDPatientNameTotalMedicineBought(从购买的药品数量中获得),其中MedicineID购买的药品是' ME001' PatientName由2个或更多单词组成。

示例:

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

4 个答案:

答案 0 :(得分:0)

你在寻找这样的东西 - :

select a.PatientID,a.PatientName,sum(Quantity) Total_Medicine_Bought
from MsPatient a
join TransactionHeader b
on a.PatientID= b.PatientID
join TransactionDetail c
on b.TransactionID =c.TransactionID
where MedicineID='MEOO1' and (Length(a.PatientName) - length(REPLACE(a.PatientName, ' ', '')) + 1)=2
group by a.PatientID,a.PatientName

Sql Fiddle:http://sqlfiddle.com/#!2/4c2cc/3

答案 1 :(得分:0)

试试这个

select PatientID ,PatientName,sum(td.Quantity)asTotal Medicine Bought from MsPatient mp
inner join TransactionHeader t on  mp.PatientID = t.PatientID 
inner join TransactionDetail td on t.TransactionID =td.TransactionID 
Group by mp.PatientID,mp.PatientName

答案 2 :(得分:0)

原始查询的问题是混合不同的连接方式。您应该在from子句中避免使用逗号。如果您确实需要cross join,请使用cross join语句。

您的问题是过滤正确名称​​和加入表格的问题:

select p.PatientID, p.PatientName,
       sum(case when MedicineId = 'ME001' then Quantity else 0
           end) as Total_Medicine_Bought
from MsPatient p join
     TransactionHeader th
     on p.PatientID= th.PatientID join
     TransactionDetail td
     on td.TransactionID =th.TransactionID
where PatientName like '% %'
group by p.PatientID, p.PatientName;

此查询还显示了对别名的良好使用。为了便于阅读,这些应该是表名的缩写,而不是任意字母。它还显示了条件聚合的使用。例如,您应该能够轻松地查看如何修改查询以显示ME002。患者姓名中至少有两个部分似乎相当于至少有一个空格。 like是查找单个空格的简单机制。

答案 3 :(得分:0)

我相信这就是你要找的东西。我使用您样本中的数据创建了表格,另外还有2组用户和交易:Bobby Lee Smith和Fred。根据您的要求,查询仅返回名称中至少有一个空格的患者,我添加了PATINDEX子句。这是查询:

 select p.PatientID, p.PatientName, sum(d.Quantity) as 'Total Medicine Bought'
 from   MsPatient p
 join   TransactionHeader h on p.PatientId = h.PatientId
 join   TransactionDetail d on h.TransactionId = d.TransactionId
 where  PATINDEX('% %', p.PatientName) > 0
 group by p.PatientID, p.PatientName

结果如下:

 PatientID      | PatientName      | Total Medicine Bought  
 PA001          | Danny andrean    | 5  
 PA002          | John Travolta    | 6  
 PA003          | Danny Lee        | 5  
 PA004          | Bobby Lee Smith  | 3  

删除PATINDEX:

 PatientID      | PatientName      | Total Medicine Bought  
 PA001          | Danny andrean    | 5  
 PA002          | John Travolta    | 6  
 PA003          | Danny Lee        | 5  
 PA004          | Bobby Lee Smith  | 3  
 PA005          | Fred             | 5