提问人:Alvaro 提问时间:9/15/2020 最后编辑:Alvaro 更新时间:9/15/2020 访问量:403
在另一个表中添加行存在标志,并使用 group by
Add flag for row existence in another table with group by
问:
对于下面的查询,我想有一个标志,其计算结果为 0
如果表中不存在 from 视图和
否则为 1isHold
billNo
viewBills
onHold
select max(t.id) TrackingID , max(vb.billNo) billNo, cb.id ,
max(case when vb.[count] > 1 then 1 else 0 end) isMultiple ,
max(case when t.TrackingID = 31 then 1 else 0 end) IsCancelled,
max(case when exists (select 1 from OnHold oh
where oh.billNo = billNo) then 1 else 0 end) IsHold
from viewBills vb
join tracking t on vb.billNo = t.billNo
join customerBills cb on vb.billNo = cb.billNo
join customerPieces cp on cb.id = cp.customerBillId
where cb.statusid <> 3
group by cb.id
我在执行时收到此错误
Cannot perform an aggregate function on an expression
containing an aggregate or a subquery.
这是合理的,但如何实现呢?
答:
1赞
Gordon Linoff
9/15/2020
#1
您可以使用 or a 将逻辑移动到子句中:outer apply
left join
FROM
select max(t.id) as TrackingID , max(vb.billNo) as billNo, cb.id ,
max(case when vb.[count] > 1 then 1 else 0 end) as isMultiple,
max(case when t.TrackingID = 31 then 1 else 0 end) as IsCancelled,
max(case when oh.billNo is not null then 1 else 0 end) as IsHold
from viewBills vb join
tracking t
on vb.billNo = t.billNo join
customerBills cb
on vb.billNo = cb.billNo join
customerPieces cp
on cb.id = cp.customerBillId outer apply
(select top (1) oh.*
from OnHold oh
where oh.billNo = cb.billNo
) oh
where cw.statusid <> 3
group by cb.id;
评论
0赞
Alvaro
9/15/2020
如何使用左连接来做到这一点?
0赞
Gordon Linoff
9/15/2020
@Alvaro . . .我更喜欢,这就是我使用这种方法的原因。outer apply
left join
0赞
Alvaro
9/15/2020
我可以知道为什么吗?效率更高吗?
0赞
Gordon Linoff
9/15/2020
@Alvaro . . .它只选择一行,因此如果有多个可能的匹配项,聚合不必做太多的工作。
1赞
Venkataraman R
9/15/2020
#2
您可以转到 LEFT OUTER JOIN 并按如下方式进行聚合:
select max(t.id) TrackingID , max(vb.billNo) billNo, cb.id ,
max(case when vb.[count] > 1 then 1 else 0 end) isMultiple ,
max(case when t.TrackingID = 31 then 1 else 0 end) IsCancelled,
max(case when oh.billNo IS NOT NULL then 1 else 0 end) IsHold
from viewBills vb
join tracking t on vb.billNo = t.billNo
join customerBills cb on vb.billNo = cb.billNo
join customerPieces cp on cb.id = cp.customerBillId
LEFT OUTER JOIN OnHold oh ON oh.billNo = vb.billNo
where cb.statusid <> 3
group by cb.id
评论
cb.id
select
cp.id
group by
outer apply