bash
表:profit_loss_sum_m_snapshot
计算字段:成本cost_whole求和,收入income_whole求和,收入求和-成本求和,成本目标cost_target求和,收入求和-成本目标求和
条件:日期statis_date在2023-11-01,资源类型resource_type为公路,区域cost_structure_org为苏沪皖
分组:区域 cost_structure_org
分组后条件:收入求和-成本求和 小于 收入求和-成本目标求和
排序:按 收入求和-成本求和 倒序
sql
SELECT cost_structure_org,
SUM(cost_whole) AS total_cost,
SUM(income_whole) AS total_income,
SUM(income_whole) - SUM(cost_whole) AS income_minus_cost,
SUM(cost_target) AS total_cost_target,
SUM(income_whole) - SUM(cost_target) AS income_minus_cost_target
FROM profit_loss_sum_m_snapshot
WHERE statis_date = '2023-11-01'
AND resource_type = '公路'
AND cost_structure_org = '苏沪皖'
GROUP BY cost_structure_org
HAVING (SUM(income_whole) - SUM(cost_whole)) < (SUM(income_whole) - SUM(cost_target))
ORDER BY income_minus_cost DESC;
增加子查询
bash
表:carrier_label_detail
计算字段:承运商carrier_code去重数量
条件:
日期statis_date在2023-11-01,
确认状态verify_status为审核通过,
工作状态work_status为活跃,
承运商carrier_code:
内表:
表:bid_book_detail
计算字段:承运商carrier_code去重
条件:招标日期quotation_end_time在2023-01-01和2023-08-03之间
分组:区域 develop_org_name
分组后条件:无
排序:无
sql
SELECT develop_org_name,
COUNT(DISTINCT carrier_code) AS unique_carrier_count
FROM carrier_label_detail
WHERE statis_date = '2023-11-01'
AND verify_status = '审核通过'
AND work_status = '活跃'
AND carrier_code IN (
SELECT DISTINCT carrier_code
FROM bid_book_detail
WHERE quotation_end_time BETWEEN '2023-01-01' AND '2023-08-03'
)
GROUP BY develop_org_name;
嵌套查询
bash
外表:
内表:
表名:profit_loss_sum_m_snapshot
计算字段:成本cost_whole求和,收入income_whole求和,收入求和-成本求和,成本目标cost_target求和,收入求和-成本目标求和
条件:日期statis_date在2023-11-01,资源类型resource_type为公路,区域cost_structure_org为苏沪皖
分组:区域 cost_structure_org
分组后条件:收入求和-成本求和 小于 收入求和-成本目标求和
计算字段:成本求和的求和,收入求和的求和,收入求和-成本求和的求和
条件:成本求和的求和 大于100000
sql
SELECT
SUM(subquery.成本求和) AS 成本求和的求和,
SUM(subquery.收入求和) AS 收入求和的求和,
SUM(subquery.收入减成本) AS 收入减成本求和
FROM (
SELECT
SUM(cost_whole) AS 成本求和,
SUM(income_whole) AS 收入求和,
SUM(income_whole) - SUM(cost_whole) AS 收入减成本,
SUM(cost_target) AS 成本目标求和,
SUM(income_whole) - SUM(cost_target) AS 收入减成本目标
FROM
profit_loss_sum_m_snapshot
WHERE
statis_date = '2023-11-01'
AND resource_type = '公路'
AND cost_structure_org = '苏沪皖'
GROUP BY
cost_structure_org
HAVING
SUM(income_whole) - SUM(cost_whole) < SUM(income_whole) - SUM(cost_target)
) AS subquery
HAVING
SUM(subquery.成本求和) > 100000;
多表关联
bash
表:carrier_label_detail
计算字段:承运商carrier_code去重数量,bid_book_detail表中的vehicle车辆数去重
关联字段:承运商carrier_code
关联表:
表名:bid_book_detail,
条件:招标日期quotation_end_time在2023-01-01和2023-08-03之间
条件:日期statis_date在2023-11-01,确认状态verify_status为审核通过,工作状态work_status为活跃
分组:区域 develop_org_name
分组后条件:无
排序:无
sql
SELECT develop_org_name,
COUNT(DISTINCT carrier_label_detail.carrier_code) AS unique_carrier_count,
COUNT(DISTINCT bid_book_detail.vehicle) AS unique_vehicle_count
FROM carrier_label_detail
LEFT JOIN bid_book_detail ON carrier_label_detail.carrier_code = bid_book_detail.carrier_code
WHERE carrier_label_detail.statis_date = '2023-11-01'
AND carrier_label_detail.verify_status = '审核通过'
AND carrier_label_detail.work_status = '活跃'
AND bid_book_detail.quotation_end_time BETWEEN '2023-01-01' AND '2023-08-03'
GROUP BY develop_org_name;