GPT写SQL的模版

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;
相关推荐
!!!52539 分钟前
maven的生命周期
java·数据库·maven
Hello Dam1 小时前
基于 FastExcel 与消息队列高效生成及导入机构用户数据
java·数据库·spring boot·excel·easyexcel·fastexcel
许仙在19971 小时前
【无标题】四类sql语句通用
数据库·sql·mysql·sqlserver
云浩舟2 小时前
Golang并发读取json文件数据并写入oracle数据库的项目实践
开发语言·数据库·golang
学会沉淀。3 小时前
Redis
数据库·redis·缓存
deadknight95 小时前
Oracle重启后业务连接大量library cache lock
数据库·oracle
万事可爱^5 小时前
【SQL】进阶知识 -- 删除表的几种方法(包含表内单个字段的删除方法)
数据库·hive·sql·oracle
非凡的世界5 小时前
关于 ThinkPHP 与 PostgreSQL 结合使用的一些要点
数据库·postgresql
fox08155 小时前
wsl2上mysql出现ip端口冲突问题
网络·数据库·tcp/ip·mysql·wsl
m0_672449605 小时前
MySQL高级(事务隔离界别)
数据库·mysql