【问题标题】:SQL Select from multiple tables Group BySQL 从多个表中选择分组依据
【发布时间】:2016-09-08 17:53:59
【问题描述】:

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

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

当我执行选择时,我收到同一个 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

转让条件

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  

【问题讨论】:

  • 这适用于哪个 RDBMS?请添加一个标签以指定您使用的是mysqlpostgresqlsql-serveroracle 还是db2 - 或者完全是其他东西。
  • 哎呀,忘了,sql server - 添加在标签中

标签: sql sql-server group-by concatenation


【解决方案1】:

尝试条件聚合

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2021-02-06
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    相关资源
    最近更新 更多