SELECT pl.id AS product_line_id, pl.name AS product_line_name, ROUND(SUM(s.price), 2) AS daily_sales, ROUND(SUM(s.price) OVER (PARTITION BY pl.id, DATE_TRUNC('week', o.create_time)), 2) AS weekly_sales, ROUND(SUM(s.price) OVER (PARTITION BY pl.id, DATE_TRUNC('month', o.create_time)), 2) AS monthly_sales, ROUND(SUM(s.price) OVER (PARTITION BY pl.id, DATE_TRUNC('year', o.create_time)), 2) AS yearly_sales FROM ls_order_sku AS od JOIN ls_order AS o ON o.id = od.order_id JOIN ls_sku AS s ON s.id = od.sku_id JOIN ls_spu AS sp ON sp.id = s.spu_id JOIN ls_product_line AS pl ON pl.id = s.product_line_id GROUP BY pl.id, pl.name, DATE_TRUNC('day', o.create_time) ORDER BY pl.id, DATE_TRUNC('day', o.create_time) 这个sql 报 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(PARTITION BY pl.id, DATE_TRUNC('week', o.create_time)), 2) AS weekly_sales, ' at line 5 这个错 怎么解决

时间: 2024-02-05 16:03:22 浏览: 28
根据报错信息,提示语法错误在第5行,即 `SUM(s.price) OVER (PARTITION BY pl.id, DATE_TRUNC('week', o.create_time))` 这一行。这是因为在 MySQL 中,没有 `OVER` 关键字来支持窗口函数,而 `OVER` 是 PostgreSQL 中的语法。因此,需要将 `SUM(s.price) OVER` 改为 `SUM(s.price)`,并将 `GROUP BY` 语句中的分组条件改为 `pl.id, pl.name, DATE(o.create_time)`,即: ```sql SELECT pl.id AS product_line_id, pl.name AS product_line_name, ROUND(SUM(s.price), 2) AS daily_sales, ROUND(SUM(CASE WHEN DATE_TRUNC('week', o.create_time) = DATE_TRUNC('week', CURDATE()) THEN s.price ELSE 0 END), 2) AS weekly_sales, ROUND(SUM(CASE WHEN DATE_TRUNC('month', o.create_time) = DATE_TRUNC('month', CURDATE()) THEN s.price ELSE 0 END), 2) AS monthly_sales, ROUND(SUM(CASE WHEN DATE_TRUNC('year', o.create_time) = DATE_TRUNC('year', CURDATE()) THEN s.price ELSE 0 END), 2) AS yearly_sales FROM ls_order_sku AS od JOIN ls_order AS o ON o.id = od.order_id JOIN ls_sku AS s ON s.id = od.sku_id JOIN ls_spu AS sp ON sp.id = s.spu_id JOIN ls_product_line AS pl ON pl.id = s.product_line_id GROUP BY pl.id, pl.name, DATE(o.create_time) ORDER BY pl.id, DATE(o.create_time) ``` 另外,因为 MySQL 不支持 `DATE_TRUNC` 函数,所以需要使用 `DATE` 函数将日期截取到天、月、年。上面的 SQL 语句中,我使用了类似 `DATE_TRUNC('week', o.create_time) = DATE_TRUNC('week', CURDATE())` 这样的语法来判断当前记录的日期是否属于本周、本月、本年。

相关推荐

java.sql.SQLException: sql injection violation, syntax error: TODO : pos 872, line 15, column 43, token UNION : SELECT asewo.code_s as code,asewo.order_type_s as orderType,asewo.equipment_name_s as equipmentName,asewo.executor_s as executor, asewo.plan_start_time_T as planStartTime,asewo.plan_finish_time_T as planFinishTime,asewo.tpm_key_s as tpmKey,asewo.shop_s as shop, asewo.line_S as line,asewo.actual_start_time_t as startTime,asewo.actual_finish_time_t as finishTime,asewo.executor_account_s as executorAccounts, asewo.source_type_s as sourceType,asewo.SOURCE_ORDER_S as sourceOrder,asewo.CREATION_TIME as creationTime FROM AT_AS_EM_Work_Order asewo WHERE to_char(asewo.plan_start_time_T,'yyyy_mm-dd hh24:mi') <= to_char(plan_finish_time_T,'yyyy_mm_dd hh24:mi') AND asewo.ORDER_STATUS_S='创建' AND asewo.executor_s=? ORDER BY asewo.creation_time DESC Union All SELECT asewo.code_s as code,asewo.order_type_s as orderType,asewo.equipment_name_s as equipmentName,asewo.executor_s as executor, asewo.plan_start_time_T as planStartTime,asewo.plan_finish_time_T as planFinishTime,asewo.tpm_key_s as tpmKey,asewo.shop_s as shop, asewo.line_S as line,asewo.actual_start_time_t as startTime,asewo.actual_finish_time_t as finishTime,asewo.executor_account_s as executorAccounts, asewo.source_type_s as sourceType,asewo.SOURCE_ORDER_S as sourceOrder,asewo.CREATION_TIME as creationTime FROM AT_AS_EM_Work_Order asewo WHERE asewo.ORDER_STATUS_S='创建' AND asewo.executor_s=? ORDER BY asewo.creation_time DESC如何改

SELECT DISTINCT c.ID AS id, c.NAME AS contName, c.CONTRACT_NO AS contractNo, c.INSTANCE_ID AS instanceId, c.UNDERTAKE_DEPT_ID AS remindDeptId, c.UNDERTAKE_DEPT_NAME AS sendDeptName, c.CREATE_USER_ID, c.CREATE_USER_NAME AS contractOpteraterName, c.PLAN_STATE AS planState, c.PLAN_STATE_NAME AS planStateName, aw.INSTANCE_ID AS inId, aw.CREATE_TIME AS sendTime FROM ( SELECT c.* FROM ( SELECT c.* FROM ( SELECT c.ORIGINAL_CONTRACT_ID, MAX(CREATE_TIME) CREATE_TIME FROM CONTRACT_DRAFT.C_CONTRACT_INFO c WHERE c.ORIGINAL_CONTRACT_ID IS NOT NULL AND c.ORIGINAL_CONTRACT_ID != '' GROUP BY c.ORIGINAL_CONTRACT_ID ) t LEFT JOIN CONTRACT_DRAFT.C_CONTRACT_INFO c ON t.ORIGINAL_CONTRACT_ID = c.ORIGINAL_CONTRACT_ID AND t.CREATE_TIME = c.CREATE_TIME UNION ALL SELECT c.* FROM CONTRACT_DRAFT.C_CONTRACT_INFO c WHERE ( c.ORIGINAL_CONTRACT_ID IS NULL OR c.ORIGINAL_CONTRACT_ID = '' ) AND c.ID NOT IN ( SELECT c.ORIGINAL_CONTRACT_ID FROM CONTRACT_DRAFT.C_CONTRACT_INFO c WHERE c.ORIGINAL_CONTRACT_ID IS NOT NULL AND c.ORIGINAL_CONTRACT_ID != '')) c WHERE c.deleted_flag = 0 AND c.BELONG = 1 AND sysdate > c.end_date AND c.plan_state IN (4100, 4110, 4120, 4200, 4210, 4220, 5100, 5110, 5120) ) c INNER JOIN (SELECT INSTANCE_ID,create_time,state FROM CONTRACT_DRAFT.C_ACTIVITY_WORKITEMS WHERE state = 'Waiting') aw ON c.INSTANCE_ID = aw.INSTANCE_ID LEFT JOIN (SELECT deleted_flag,CONT_ID FROM CONTRACT_DRAFT.C_GET_PAY_PLAN WHERE deleted_flag = 0 ) g ON c.ID = g.CONT_ID LEFT JOIN CONTRACT_DRAFT.C_OUR_ENTITY_INFO oe ON c.OUR_ENTITY_ID = oe.ID AND oe.DELETED_FLAG = 0 ORDER BY aw.CREATE_TIME DESC 优化

WITH -- 定义一个子查询,获取销售额排名前10的产品 top_products AS ( SELECT product_id, SUM(sales) AS total_sales FROM orders WHERE order_date BETWEEN '2021-01-01' AND '2021-06-30' GROUP BY product_id ORDER BY total_sales DESC LIMIT 10 ), -- 定义一个子查询,获取销售额排名前10的客户 top_customers AS ( SELECT customer_id, SUM(sales) AS total_sales FROM orders WHERE order_date BETWEEN '2021-01-01' AND '2021-06-30' GROUP BY customer_id ORDER BY total_sales DESC LIMIT 10 ), -- 定义一个窗口函数,计算每个客户的销售额排名 customer_sales_rank AS ( SELECT customer_id, SUM(sales) AS total_sales, ROW_NUMBER() OVER (ORDER BY SUM(sales) DESC) AS sales_rank FROM orders WHERE order_date BETWEEN '2021-01-01' AND '2021-06-30' GROUP BY customer_id ) -- 最终查询,获取纽约市销售额排名前10的客户,以及他们购买的销售额排名前10的产品 SELECT customers.id AS customer_id, customers.name AS customer_name, products.id AS product_id, products.name AS product_name, SUM(orders.sales) AS total_sales FROM orders -- 连接顾客信息 INNER JOIN customers ON orders.customer_id = customers.id -- 连接产品信息 INNER JOIN products ON orders.product_id = products.id -- 仅查询纽约市的客户 WHERE customers.city = 'New York' -- 仅查询销售额排名前10的客户 AND customers.id IN (SELECT customer_id FROM top_customers) -- 仅查询销售额排名前10的产品 AND products.id IN (SELECT product_id FROM top_products) -- 仅查询客户销售额排名前10的订单 AND customers.id IN (SELECT customer_id FROM customer_sales_rank WHERE sales_rank <= 10) GROUP BY customers.id, customers.name, products.id, products.name ORDER BY customers.id, total_sales DESC, products.id;请优化下这条sql

改如何修正: <select id="getCurrentTask" resultType="com.sottop.sokonmobile.sokonmobile.qingdao.entity.AsEmWorkOrderEntity"> SELECT asewo.code_s as code,asewo.order_type_s as orderType,asewo.equipment_name_s as equipmentName,asewo.executor_s as executor, asewo.plan_start_time_T as planStartTime,asewo.plan_finish_time_T as planFinishTime,asewo.tpm_key_s as tpmKey,asewo.shop_s as shop, asewo.line_S as line,asewo.actual_start_time_t as startTime,asewo.actual_finish_time_t as finishTime,asewo.executor_account_s as executorAccounts, asewo.source_type_s as sourceType,asewo.SOURCE_ORDER_S as sourceOrder,asewo.CREATION_TIME as creationTime FROM AT_AS_EM_Work_Order asewo WHERE to_char(asewo.plan_start_time_T,'yyyy_mm-dd hh24:mi') <= to_char(plan_finish_time_T,'yyyy_mm_dd hh24:mi') AND asewo.ORDER_STATUS_S='创建' AND asewo.executor_s=#{executor} <if test="orderType!=null and orderType!=''"> AND asewo.order_type_s=#{orderType} </if> ORDER BY asewo.creation_time DESC Union All SELECT asewo.code_s as code,asewo.order_type_s as orderType,asewo.equipment_name_s as equipmentName,asewo.executor_s as executor, asewo.plan_start_time_T as planStartTime,asewo.plan_finish_time_T as planFinishTime,asewo.tpm_key_s as tpmKey,asewo.shop_s as shop, asewo.line_S as line,asewo.actual_start_time_t as startTime,asewo.actual_finish_time_t as finishTime,asewo.executor_account_s as executorAccounts, asewo.source_type_s as sourceType,asewo.SOURCE_ORDER_S as sourceOrder,asewo.CREATION_TIME as creationTime FROM AT_AS_EM_Work_Order asewo WHERE asewo.ORDER_STATUS_S='创建' AND asewo.executor_s=#{executor} <if test="orderType!=null and orderType!=''"> AND asewo.order_type_s=#{orderType} </if> ORDER BY asewo.creation_time DESC </select>

优化sql:SELECT we.organization_id ,we.wip_entity_id ,case when wl.line_id is null then we.wip_entity_name else '' end wip_entity_name ,we.primary_item_id ,mtt.transaction_type_name ,mmt.transaction_date ,bd.department_code ,mmt.inventory_item_id ,mmt.subinventory_code ,mta.reference_account ,br.resource_code ,lu2.meaning as line_type_name ,mta.base_transaction_value ,mta.cost_element_id ,flv.meaning as cost_element ,wdj.class_code job_type_code ,ml.meaning job_type_name FROM (select * from gerp.mtl_material_transactions where substr(transaction_date,1,7) >= '2023-06' and transaction_source_type_id = 5) mmt inner join gerp.wip_entities we on mmt.organization_id = we.organization_id inner join gerp.mtl_transaction_accounts mta on mta.transaction_source_id = we.wip_entity_id and mta.transaction_id = mmt.transaction_id and mta.transaction_source_type_id = 5 inner join gerp.mtl_transaction_types mtt on mtt.transaction_type_id = mmt.transaction_type_id inner join mfg_lookups lu2 on lu2.lookup_code = mta.accounting_line_type and lu2.lookup_type = 'CST_ACCOUNTING_LINE_TYPE' inner join gerp.mtl_system_items_b msi on msi.inventory_item_id = mmt.inventory_item_id and msi.organization_id = mta.organization_id left join gerp.bom_departments bd on bd.department_id = mmt.department_id left join gerp.bom_resources br on br.resource_id = mta.resource_id left join gerp.wip_lines wl on wl.line_id = mmt.repetitive_line_id left join gerp.wip_discrete_jobs wdj on wdj.wip_entity_id = mta.transaction_source_id left join gerp.fnd_lookup_values_vl flv on cast(mta.cost_element_id as string) = flv.lookup_code and flv.lookup_type = 'CST_COST_CODE_TYPE' left join mfg_lookups ml on ml.lookup_code = wdj.job_type and ml.lookup_type = 'WIP_DISCRETE_JOB' 。其中mmt,we,mta,msi,wdj数据量很大

将下面这条sql语句简短一些:SELECT * FROM ( SELECT fpd.fpd_id AS fpdId, fpd.fpd_budget_project AS fpdBudgetProject, fpd.fpd_budget_project_id AS fpdBudgetProjectId, fpd.fpd_pf_name AS fpdPfName, fpd.fpd_pf_id AS fpdPfId, fpd.fpd_expend_subject AS fpdExpendSubject, fpd.fpd_expend_subject_id AS fpdExpendSubjectId, fpd.fpd_payee_name AS fpdPayeeName, fpd.fpd_payee_account AS fpdPayeeAccount, fpd.fpd_payee_opening_bank AS fpdPayeeOpeningBank, fpd.fpd_payee_opening_bank_num AS fpdPayeeOpeningBankNum, fpd.fpd_dept_pay_economics_classify_num AS fpdDeptPayEconomicsClassifyNum, fpd.fpd_dept_pay_economics_classify_name AS fpdDeptPayEconomicsClassifyName, fpd.fpd_applied_amount AS fpdAppliedAmount, fpd.fpd_capital_use AS fpdCapitalUse, fpd.fpd_postscript AS fpdPostscript, fpd.fpd_budget_item_name AS fpdBudgetItemName, fpd.fpd_budget_item_number AS fpdBudgetItemNumber, fpd.fpd_dr_name AS fpdDrName, dbec.dbec_parent_num AS dbecParentNum FROM financial_payment_details fpd LEFT JOIN dept_budget_economy_class dbec ON fpd.fpd_dept_pay_economics_classify_num = dbec.dbec_num WHERE fpd.fpd_is_tax = "0" UNION ALL SELECT fpd.fpd_id AS fpdId, fpd.fpd_budget_project AS fpdBudgetProject, fpd.fpd_budget_project_id AS fpdBudgetProjectId, fpd.fpd_pf_name AS fpdPfName, fpd.fpd_pf_id AS fpdPfId, fpd.fpd_expend_subject AS fpdExpendSubject, fpd.fpd_expend_subject_id AS fpdExpendSubjectId, fpd.fpd_payee_name AS fpdPayeeName, fpd.fpd_payee_account AS fpdPayeeAccount, fpd.fpd_payee_opening_bank AS fpdPayeeOpeningBank, fpd.fpd_payee_opening_bank_num AS fpdPayeeOpeningBankNum, fpd.fpd_dept_pay_economics_classify_num AS fpdDeptPayEconomicsClassifyNum, fpd.fpd_dept_pay_economics_classify_name AS fpdDeptPayEconomicsClassifyName, sum( fpd.fpd_applied_amount ) AS fpdAppliedAmount, fpd.fpd_capital_use AS fpdCapitalUse, fpd.fpd_postscript AS fpdPostscript, fpd.fpd_budget_item_name AS fpdBudgetItemName, fpd.fpd_budget_item_number AS fpdBudgetItemNumber, fpd.fpd_dr_name AS fpdDrName, dbec.dbec_parent_num AS dbecParentNum FROM financial_payment_details fpd LEFT JOIN dept_budget_economy_class dbec ON fpd.fpd_dept_pay_economics_classify_num = dbec.dbec_num WHERE fpd.fpd_is_tax = "1" GROUP BY fpd_pay_num ) t1

最新推荐

recommend-type

[Oracle] dbms_metadata.get_ddl 的使用方法总结

以下是对Oracle中dbms_metadata.get_ddl的用法进行了详细的分析介绍,需要的朋友参考下
recommend-type

智能制造的数字化工厂规划qytp.pptx

智能制造的数字化工厂规划qytp.pptx
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

帮我实现在Androidstudio调用chapgpt并提供源码

首先,你需要运行一个ChitGPT的服务器,然后通过Android应用程序与该服务器进行通信。以下是一个简单的Android应用程序示例,可以与ChitGPT进行通信: 1. 首先,在Android Studio中创建一个新的项目,并添加以下依赖项: ``` implementation 'com.squareup.okhttp3:okhttp:4.9.0' implementation 'com.google.code.gson:gson:2.8.6' ``` 2. 创建一个新的Java类,用于与ChitGPT服务器通信。以下是一个简单的实现: ```java import com.