SQL从多个表中选择分组依据

时间:2016-09-08 17:54:00

标签: sql sql-server group-by concatenation

我正在从SQL数据库构建数据提取。我有以下SQL查询,它从多个表中选择特定列。有问题的关系是这样的: 我有一个ConveyancingCondition表,它有一个ConveyancingDetail表的外键,该表具有OTPSale表的外键。 OTPSale中的记录可能有一个带有多个ConveyancingCondition记录的ConveyancingDetail记录。

我需要单独选择ConveyancingCondition字段,并将它们组合成一个字符串。

当我执行select时,我会收到同一OTPSale的多条记录。我知道我需要通过conveyancingDetail进行分组,但我得到错误的组“列在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。”。我选择了太多的字段来将它们全部添加到组中(至少50个不同的列)。

这样做的正确方法是什么?

select distinct
    --other fields from other tables
     (select FORMAT(conveyancingCondition.DateRequired, 'dd/MM/yyyy')
     where conveyancingCondition.Name = 'Deposit') 'Deposit Date Required',      
     (select FORMAT(conveyancingCondition.DateRequired, 'dd/MM/yyyy')       
     (select FORMAT(conveyancingCondition.DateRequired, 'dd/MM/yyyy')
     where conveyancingCondition.Name = 'Subject') 'Subject To Sale Date Required',     
     (select FORMAT(conveyancingCondition.DateRequired, 'dd/MM/yyyy')
     where conveyancingCondition.Name = 'Guarant') 'Guarantee Date Required',
    ISNULL(stuff((SELECT ( ',' + conveyancingCondition.DateRequired) FROM otp.ConveyancingCondition cd where (cd.ConveyancingDetailId = conveyancingDetail.Id AND cd.IsApplicable=1) FOR XML PATH( '' ) ), 1, 1, ''), NULL) as 'ConditionDates'

from otp.OTPSale    
left join otp.ConveyancingDetail conveyancingDetail 
on conveyancingDetail.Id = otp.OTPSale.ConveyancingDetailId
left join otp.ConveyancingCondition conveyancingCondition 
on conveyancingCondition.ConveyancingDetailId = conveyancingDetail.Id
WHERE otp.OTPSale.IsTransferRequired= 1 
--group by conveyancingDetail.Id

数据如下所示:

OTPSale

Id ConveyancingDetail IsTransferRequired
1  1                  1
2  2                  1

ConveyancingDetail

Id
1
2
3

ConveyancingCondition

Id  ConveyancingDetailId    Name    IsApplicable    DateRequired
1   1                       Deposit 1               2016-09-12      
2   1                       Bond    1               2016-09-26      
3   1                       Subject 1               2016-09-26        
4   1                       Guarant 1               2016-09-30      
5   1                       Other   0               NULL    
6   2                       Deposit 1               2016-09-15      
7   2                       Bond    1               2016-09-16      
8   2                       Subject 1               2016-09-17        
9   2                       Guarant 1               2016-09-18      
10  2                       Other   0               NULL          

这就是我的期望

OTPSaleId   Deposit Date Required   Bond Date Required  Subject Date Required   Guarantee Date Required     Other   ConditionDates

1           2016-09-12              2016-09-26          2016-09-26              2016-09-30              NULL    2016/09/22, 2016/09/26, 2016/09/26, 2016/09/30
2           2016-09-15              2016-09-16          2016-09-17              2016-09-18              NULL    2016/09/16, 2016/09/16, 2016/09/17, 2016/09/18  

1 个答案:

答案 0 :(得分:0)

尝试条件聚合

select conveyancingDetail.Id
    --other fields from other tables
     max(case conveyancingCondition.Name when 'Deposit' then FORMAT(conveyancingCondition.DateRequired, 'dd/MM/yyyy') end) [Deposit Date Required],      
     max(case conveyancingCondition.Name when 'Subject' then FORMAT(conveyancingCondition.DateRequired, 'dd/MM/yyyy') end) [Subject To Sale Date Required],     
     --...

from otp.OTPSale    
left join otp.ConveyancingDetail conveyancingDetail 
on conveyancingDetail.Id = otp.OTPSale.ConveyancingDetailId
left join otp.ConveyancingCondition conveyancingCondition 
on conveyancingCondition.ConveyancingDetailId = conveyancingDetail.Id
WHERE otp.OTPSale.IsTransferRequired= 1 
group by conveyancingDetail.Id