0

我有一张叫做发票的表。为了帮助简化这个问题,我们将说发票中有 4 列。

id                    (PK auto int)
booking_id            (int)
is_business_invoice   (1 or 0 or NULL)
amount                (decimal)

一个预订可以有多个发票。

我试着写一个查询,什么将确定对于给定的预订 ID,它们都是商业发票还是个人发票。如果有多个不同is_business_invoice类型的发票,它将默认为空。如果有 3 个预订并且所有 3 个预订,is_business_invoice那么它将返回 1。

4

1 回答 1

1
select case when count(is_business_invoice) = sum(is_business_invoice) 
            then 1 
            when sum(is_business_invoice) = 0
            then 0
            else null 
      end as IsBusinessInvoices
from invoices
group by booking_id
于 2012-06-06T15:31:59.687 回答