`

一些sql经验

sql 
阅读更多
安装了mysql5.7,用group by 查询时抛出如下异常:
https://www.cnblogs.com/jpfss/p/10401753.html

开发新客户和老客户激活的情况(下单客户中前90天没有下过单的客户)

select employees.name, companies.name_cn,
DATE_FORMAT(min(od.created_at),'%Y-%m-%d') as min_created_at,
count(case when od.created_at>= "#{@start_time}" then 1 else 0 end ) as count_order,
sum(case when od.created_at>= "#{@start_time}" then od.amount else 0 end ) as sum_amount
from order_details  as od
left join customer_orders as co
on co.id = od.customer_order_id
left join employees
on employees.id = co.trader_id
left join companies
on companies.id = od.company_id
where od.created_at >= date_sub("#{@start_time}",interval 3 month)  and detail_status != 7
#{@condition_end_time}
#{@condition_em_name}
group by od.company_id
having min_created_at >= "#{@start_time}"


业务线利润占比

select
            truncate( sum(source_data.c_amount),2) as sum_c_amount,
            truncate(
            sum(
                     (source_data.c_amount
            - source_data.v_amount
            - source_data.goods_service_fee
            - (case
                when (source_data.payment_type != 3 and source_data.order_type != 4) then 0
                when source_data.order_type = 4 then (source_data.c_amount * 0.05) / 1.05
                else source_data.customer_commision_fee end )
            - source_data.od_commision_fee
            ) * vm_rate * special_rate * reagent_rate)
            ,2) as real_gross
        from
        (select od.id,od.customer_order_no,co.payment_type,od.invoice_type,co.order_type,
          od.commision_fee as od_commision_fee ,od.customer_commision_fee,od.goods_service_fee,
         case when od.vendor_order_type in (1,4,12,13,14) then 0.5 else 1 end as vm_rate,
          case when co.special_type = 1 then 0.5 else 1 end as special_rate,
          case when companies.reagent = 1 then 0.9 else 1 end as reagent_rate,

          case  
               when  od.vendor_invoice_type = 2 && od.goods_ownership = 1 then od.vendor_amount * 1.16
               when  od.goods_ownership = 1 then od.vendor_amount
               when  od.vendor_invoice_type = 2 and od.previous_vendor_amount is not null and od.previous_vendor_amount != 0 and odd.previous_vendor_invoice_type = 2 then od.previous_vendor_amount * 1.16 + (od.vendor_invoice_amount - od.previous_vendor_amount)*1.16*0.3
               when  od.vendor_invoice_type = 2 and od.previous_vendor_amount is not null and od.previous_vendor_amount != 0 then od.previous_vendor_amount + (od.vendor_invoice_amount*1.16 - od.previous_vendor_amount)*0.3
               when  od.previous_vendor_amount is not null and od.previous_vendor_amount != 0 and odd.previous_vendor_invoice_type = 2 then od.previous_vendor_amount * 1.16 + (od.vendor_invoice_amount - od.previous_vendor_amount*1.16)*0.3
               when  od.previous_vendor_amount is not null and od.previous_vendor_amount != 0 then od.previous_vendor_amount + (od.vendor_invoice_amount - od.previous_vendor_amount)*0.3
               when  od.vendor_invoice_type = 2 then od.vendor_invoice_amount * 1.16
               else  od.vendor_invoice_amount
          end as v_amount,
        od.amount as c_amount,
        co.trader_id
        from order_details as od
        left join `customer_orders` as co on
        od.`customer_order_id` = co.id
        left join employees on
        employees.id = co.trader_id
        left join order_detail_deeps as odd
        on odd.order_detail_id = od.id
        left join companies
        on companies.id = co.company_id
        where
        od.`origin`!=6
        and companies.company_type = 3
        and od.received_at >= '#{start_time}'
        and od.received_at <= '#{end_time}'
        and od.received_amount >= od.amount
        and detail_status!=7 and od.vendor_amount !=0 and od.amount !=0 ) as source_data


一对多表关联sum1表uniq price

select
sum(count_order_items) as sum_order_items,
sum(sum_order_amount/count_order_items) as sum_amount,
sum(sum_fob_price),
sum(sum_vendor_price),
sum(sum_vendor_price),
sum(sum_profit),
sum(sum_profit) * 100 / sum(sum_order_amount) as profit_rate
from
(select count(order_items.id) as count_order_items,
sum(orders.amount) as sum_order_amount,
sum(order_items.fob_price) as sum_fob_price,
sum(order_items.vendor_price) as sum_vendor_price,
sum(order_items.profit) as sum_profit
from order_items
left join orders
on order_items.order_id = orders.id
where order_items.created_at >= "2018-10-01"
group by orders.id) as sount_data

-查找条件作为条件二次查询

select
week_data,
'Total' as bd_manager,
sum(count_order_items) as sum_order_items,
sum(sum_order_amount/count_order_items) as sum_amount,
sum(sum_fob_price) as sum_fob_price,
sum(sum_vendor_price) as sum_vendor_price,
sum(sum_profit) as sum_profit,
truncate( sum(sum_profit) * 100 / sum(sum_order_amount/count_order_items),2) as profit_rate,
(select sum(order_items.amount - order_items.received_amount) from order_items where UNIX_TIMESTAMP(order_items.created_at) <= UNIX_TIMESTAMP(last_date) and UNIX_TIMESTAMP(order_items.overdue_date) < UNIX_TIMESTAMP(last_date) and order_items.state != 1 ) as sum_overdue,
(select sum(order_items.amount - order_items.received_amount) from order_items where UNIX_TIMESTAMP(order_items.created_at) <= UNIX_TIMESTAMP(last_date) and (order_items.overdue_date is null or UNIX_TIMESTAMP(order_items.overdue_date) >= UNIX_TIMESTAMP(last_date) ) and order_items.state != 1 ) as sum_inoverdue,
(select sum(order_items.amount - order_items.received_amount) from order_items where UNIX_TIMESTAMP(order_items.created_at) <= UNIX_TIMESTAMP(last_date) and order_items.state != 1 ) as sum_received
from
(select
concat( date_sub(date_sub(date_format(DATE_FORMAT(order_items.created_at,'%Y-%m-%d'), '%y-%m-%d'),interval extract(day from DATE_FORMAT(order_items.created_at,'%Y-%m-%d')) - 1 day),interval 0 month), '  ' , last_day(order_items.created_at) ) as week_data,
last_day(order_items.created_at) as last_date,
count(order_items.id) as count_order_items,
sum(orders.amount) as sum_order_amount,
sum(order_items.fob_price) as sum_fob_price,
sum(order_items.vendor_price) as sum_vendor_price,
sum(order_items.profit) as sum_profit
from order_items
left join orders
on order_items.order_id = orders.id
where  order_items.state != 1
#{@condition_start_time}
#{@condition_end_time}
group by orders.id) as sount_data group by week_data


要的是每个月有几个定制产品(并且这类产品是以其没有成单过的)

select
customer_order_no,
amount,
chemicals.cas,
`package`,
package_unit ,
(select count(*) from order_details where chemical_id = od.chemical_id and order_details.id != od.id ) as count_uniq
from order_details as od
left join chemicals
on chemicals.id = od.`chemical_id`
where customization_type = 1 and od.detail_status != 7 and od.origin != 6
having count_uniq = 0
0
0
分享到:
评论

相关推荐

    SQL查询经验分享SQL查询经验分享

    SQL查询经验分享SQL查询经验分享SQL查询经验分享

    SQL经验,是一位达人的经验总结

    这位达人总结的SQL经验涵盖了多个方面,旨在帮助数据库应用开发者提升技能和优化工作流程。以下是对这些经验的详细解析: 1. **查询优化**:高效的SQL查询是数据库性能的关键。达人强调了避免全表扫描,合理使用...

    sql经验总结技巧—非常的实用

    以下是一些关于SQL Server的经验总结,涵盖了问题解决、日期处理、数据操作、查询优化等多个方面: 1. **处理挂起的安装操作**:当安装SQL Server或Service Pack时,如果系统提示有挂起的安装操作,可以尝试进入...

    sql优化经验总结

    通过阅读"sql优化经验总结V1.5.ppt"和"sql优化经验总结.ppt",可以更深入地学习到更多实用技巧和案例分析,进一步提升在Oracle SQL优化方面的能力。不断学习和实践,才能在这个领域取得更大的成就。

    oracle的SQL语句的一些经验总结

    以下是对"Oracle的SQL语句的一些经验总结"中可能涉及的关键知识点的详细阐述: 1. **基本查询**:SQL的基础是SELECT语句,用于从表中提取数据。例如,`SELECT column1, column2 FROM table_name;`用于获取指定列的...

    SQL优化经验大全

    ### SQL优化经验大全 在数据库管理和开发过程中,SQL查询优化是一项至关重要的技能。良好的SQL查询不仅可以提高系统的响应速度,还能显著降低系统资源消耗。本文将详细介绍30多条SQL优化的经验,帮助您更好地理解和...

    sql练习总结sql经验总结

    在本篇总结中,我们将深入探讨SQL的核心概念、常见操作以及实践经验,旨在帮助你提升SQL技能并积累宝贵的经验。 一、SQL基础 1. 数据库与表:SQL主要用于创建、查询、更新和管理数据库中的数据。数据库是一个组织...

    SQL语句经验总结

    本文将针对SQL中最常用的四条基本数据操作语句——INSERT、SELECT、UPDATE和DELETE——进行深入讲解,并分享一些实用的经验技巧。 #### 二、SQL基本数据操作语句 ##### 1. INSERT语句 **用途**:用于向数据库表中...

    SQL Server 精华集 心得与经验

    《SQL Server 精华集 心得与经验》是一份集合了众多SQL Server使用技巧、实战经验的宝贵资料,适合SQL Server初学者以及有一定基础的用户深入学习。该资源包含了许多实用的知识点,旨在帮助读者提升在数据库管理、...

    sql 教程和经典sql

    最后,"SQL经典.txt"可能是包含了一些经典的SQL问题和解决方案,可能涵盖: 1. **复杂查询示例**:如多表JOIN、子查询、自连接和窗口函数的应用。 2. **数据转换**:如何进行行列转换,如使用PIVOT和UNPIVOT操作。 3...

    SQL SERVER实用经验技巧集

    以下是一些关于SQL Server的实用技巧和经验,旨在帮助数据库管理员和开发人员更好地管理和利用数据。 1. **查询优化**: - 使用`EXPLAIN PLAN`或`SET STATISTICS IO ON`来分析查询执行计划,找出性能瓶颈。 - ...

    经典SQL语句经验集锦

    "经典SQL语句经验集锦"是针对SQL语句的学习资料,无论你是初学者还是有经验的开发者,都能从中获取宝贵的实战经验。 首先,SQL的基础知识包括以下几个部分: 1. **数据类型**:SQL中的数据类型有数值型(如INT、...

    sqlserver转PG经验总结及PG的一些特性

    ### SQL Server 转 PostgreSQL 经验总结及 PostgreSQL 特性 #### 一、SQL Server 到 PostgreSQL 的数据类型映射 在进行 SQL Server 数据库迁移至 PostgreSQL 的过程中,一个重要的步骤是确保数据类型的正确映射。...

    SQL_SERVER实用经验技巧集

    在SQL Server数据库管理中,掌握一些实用的经验技巧可以极大地提高工作效率和问题解决能力。下面将详细探讨标题"SQL_SERVER实用经验技巧集"中所涵盖的关键知识点,并基于描述内容进行深入解析。 1. **安装时提示有...

    数据库SQL语句调优的一些经验

    以下是一些关于SQL语句调优的经验分享。 首先,了解目标至关重要。你需要知道你的优化目标是什么,是提高查询速度、减少资源消耗还是改善用户体验。同时,清楚当前系统的性能状况也是基础,可以通过基准测试(Base ...

    一些SQL开发的经验,真实的工作经验

    在SQL开发领域,实际工作经验是积累专业知识和技能的关键。这里我们主要关注的是Hive SQL的书写技巧,这是大数据处理和分析中的重要工具。Hive是一个基于Hadoop的数据仓库工具,可以将结构化的数据文件映射为一张...

    新迷你SQL2000

    5. **示例数据库**:可能包含一些预设的示例数据库,帮助用户快速上手和学习如何使用SQL2000。 6. **文档和帮助文件**:提供有关如何使用SQL2000的详细信息和教程。 尽管迷你SQL2000简化了许多功能,但它依然保留了...

    SQL_PLUS.rar_PLUS SQL_SQL_Plus_plus_sql plus

    总的来说,SQL Plus作为一款强大且灵活的数据库管理工具,无论对于初学者还是经验丰富的数据库专业人员,都是一个不可或缺的工具。通过深入理解和熟练使用SQL Plus,可以大大提高数据库管理和开发的效率。

    SQL、T-SQL与PL-SQL的区别

    但无论选择哪一种语言,开发者都需要具备一定的数据库知识和编程经验,以便更好地使用这些语言。 SQL、T-SQL和PL-SQL是关系数据库中的三种常用的语言,每种语言都有其特点和优缺。了解它们之间的区别和特点,对于...

Global site tag (gtag.js) - Google Analytics