将两个查询合二为一

时间:2017-05-26 10:00:49

标签: mysql

我应该从这些数据做出报告:

|--------|---------------|------------|----------|--------------|
| BIL_Id | BIL_BookingId | BIL_Date   | BIL_Rate | BIL_Quantity |
|--------|---------------|------------|----------|--------------|
| 1      | 90            | 2017-04-01 | 85.00    | 1            |
| 2      | 90            | 2017-04-02 | 85.00    | 1            |
| 3      | 90            | 2017-04-03 | 85.00    | 1            |
| 4      | 92            | 2017-04-07 | 95.00    | 1            |
| 5      | 93            | 2017-04-10 | 85.00    | 1            |
| 6      | 93            | 2017-04-11 | 85.00    | 1            |
| 7      | 93            | 2017-04-12 | 85.00    | 1            |
| 8      | 94            | 2017-04-07 | 95.00    | 1            |
| 9      | 95            | 2017-04-12 | 95.00    | 1            |
| 10     | 95            | 2017-04-13 | 95.00    | 1            |
|--------|---------------|------------|----------|--------------|

我需要:

  • 每日预订的夜数。 例如,从数据表中,2应该2017-04-12,其他日期应该1

该工作查询:

SELECT BIL_Date,
COUNT(*) AS total_nights 
FROM ___BillableDatas 
WHERE BIL_Date BETWEEN "2017-04-01" AND "2017-04-30" 
GROUP BY BIL_Date ASC
  • 获取同一时期的每个日期的收入。

该工作查询:

SELECT BIL_Date,
SUM(BIL_Rate * BIL_Quantity) AS sumRevAccomodation
FROM `___BillableDatas` 
WHERE BIL_Date BETWEEN "2017-04-01" AND "2017-04-30" 
GROUP BY BIL_Date ASC

我的问题: 我如何将这两个查询合并为一个?

2 个答案:

答案 0 :(得分:0)

SELECT COUNT(*) AS total_nights, SUM(BIL_Rate * BIL_Quantity) AS sumRevAccomodation, BIL_Date FROM ___BillableDatas WHERE BIL_Date BETWEEN "2017-04-01" AND "2017-04-30" GROUP BY BIL_Date ASC

答案 1 :(得分:0)

您只需要将两个select子句混合为

SELECT COUNT(*) AS total_nights, BIL_Date,SUM(BIL_Rate * BIL_Quantity)  AS sumRevAccomodation 
FROM ___BillableDatas 
WHERE BIL_Date BETWEEN "2017-04-01" AND "2017-04-30" GROUP BY BIL_Date ASC