1

我有以下两个表:

1. BList

  • 预订ID
  • 成人没有
  • 孩子没有
  • 预定日期

2. B手柄

  • 预订ID
  • 票务状态
  • 最终售价
  • 终网
  • 职员

我想要做的是获得distinct Staffwith Sum of (SellingPrice), Sum of (NettPrice), Profit (Sum of sellingPrice)- Sum of (NettPrice)), No of Pax 这是(AdultNo + ChildNo)并且也算作BookingIDNo of Bookings

WHERE BookingDate >= fromDate AND BookingDate <= toDate 
    AND TicketingStatus='CP'

看起来像这样的东西(底部的总数无关紧要,因为我会将它们写入 csv 格式,我将在那里处理总数)但我需要先弄清楚如何获取查询。

我要制作的清单

这是我可以从第二个表BHandle获得的查询

SELECT Staff, SUM(FinalSellingPrice) AS gross, SUM(FinalNett) AS cost
FROM BHandle
WHERE ticketingstatus ='CP'
GROUP BY Staff

这是我对第一个表BList的查询

SELECT (adultno+childno) AS pax 
fFROM om BList
WHERE bookingdate >='01-mar-2013 00:00'
AND bookingdate <= '15-may-2013 23:59'

如何将这两个查询组合在一起?

4

2 回答 2

5

像这样的东西(假设所有列都不为空):

select Staff,
    sum(FinalSellingPrice) as gross,
    sum(FinalNett) as cost,
    sum(FinalSellingPrice - FinalNett) as profit,
    sum(AdultNo+ChildNo) as pax,
    count(1) as bookings
from Blist b
inner join BHandle bh on b.BookingID = bh.BookingID
where b.BookingDate >= fromDate
    and b.BookingDate <= toDate
    and bh.TicketingStatus = 'CP'
group by staff;
于 2013-05-16T02:25:01.317 回答
1

一种方法是使用union all聚合:

select staff, sum(gross) as gross, sum(cost) as cost, sum(pax) as pax,
       sum(numbookings) as numbookings
from ((SELECT Staff, SUM(FinalSellingPrice) AS gross, SUM(FinalNett) AS cost,
              null as pax, null as numbookings
       FROM BHandle
       WHERE ticketingstatus ='CP'
       GROUP BY Staff
      ) union all
      (select staff, null as gross, null as cost, (adultno+childno) AS pax ,
              count(*) as numbookings
       from blist join
            bhandle
            on blist.bookingid = bhandle.bookingid
       group by staff
      )
     ) t
group by staff
于 2013-05-16T02:04:09.510 回答