• 2118. Build the Equation


    SQL架构

    Table: Terms

    +-------------+------+
    | Column Name | Type |
    +-------------+------+
    | power       | int  |
    | factor      | int  |
    +-------------+------+
    power is the primary key column for this table.
    Each row of this table contains information about one term of the equation.
    power is an integer in the range [0, 100].
    factor is an integer in the range [-100, 100] and cannot be zero.
    

    You have a very powerful program that can solve any equation of one variable in the world. The equation passed to the program must be formatted as follows:

    • The left-hand side (LHS) should contain all the terms.
    • The right-hand side (RHS) should be zero.
    • Each term of the LHS should follow the format "<sign><fact>X^<pow>" where:
      • <sign> is either "+" or "-".
      • <fact> is the absolute value of the factor.
      • <pow> is the value of the power.
    • If the power is 1, do not add "^<pow>".
      • For example, if power = 1 and factor = 3, the term will be "+3X".
    • If the power is 0, add neither "X" nor "^<pow>".
      • For example, if power = 0 and factor = -3, the term will be "-3".
    • The powers in the LHS should be sorted in descending order.

    Write an SQL query to build the equation.

    The query result format is in the following example.

    Example 1:

    Input: 
    Terms table:
    +-------+--------+
    | power | factor |
    +-------+--------+
    | 2     | 1      |
    | 1     | -4     |
    | 0     | 2      |
    +-------+--------+
    Output: 
    +--------------+
    | equation     |
    +--------------+
    | +1X^2-4X+2=0 |
    +--------------+
    

    Example 2:

    Input: 
    Terms table:
    +-------+--------+
    | power | factor |
    +-------+--------+
    | 4     | -4     |
    | 2     | 1      |
    | 1     | -1     |
    +-------+--------+
    Output: 
    +-----------------+
    | equation        |
    +-----------------+
    | -4X^4+1X^2-1X=0 |
    +-----------------+
    

    Follow up: What will be changed in your solution if the power is not a primary key but each power should be unique in the answer?

    1. with t1 as (select
    2. `power`,factor,row_number() over(order by power desc) rn #按 power 降序 标号 便于 后期 group_concat an标号排序拼接
    3. from
    4. Terms
    5. ) ,t2 as (
    6. select
    7. group_concat(equation order by rn separator '') equation
    8. from(
    9. select
    10. if(factor>0,
    11. concat('+',convert(factor,char),
    12. case
    13. when `power` = 0 then ''
    14. when `power` = 1 then 'X'
    15. else concat('X','^',convert(`power`,char))
    16. end),
    17. concat(convert(factor,char),case
    18. when `power` = 0 then ''
    19. when `power` = 1 then 'X'
    20. else concat('X','^',convert(`power`,char))
    21. end)) equation ,rn
    22. from
    23. t1
    24. -- union all
    25. -- select
    26. -- "=0"
    27. ) s2
    28. )
    29. select
    30. concat(
    31. (select
    32. equation
    33. from
    34. t2)
    35. ,"=0") equation

    思路一样 稍微简洁了一些:

    1. with t as (
    2. select power
    3. ,case
    4. when power = 0 then if(factor>0,concat('+',factor),factor)
    5. when power = 1 then if(factor>0,concat('+',factor,'X'),concat(factor,'X'))
    6. else if(factor>0,concat('+',factor,'X','^',power),concat(factor,'X','^',power)) end term
    7. from Terms
    8. )
    9. select concat(group_concat(term order by power desc separator ''),'=0') equation
    10. from t

    笔记:

    group_concat(equation order by rn   separator  '')

    group_concat([Distinct] 要连接的字段 [Order by asc/desc 排序字段] [separator'分隔符'])

    其中 要链接的字段 和 排序字段可以不同

    CONVERT ( key USING utf8mb4 )
    一、转换数据类型
    1、语法
    input_value –用于指定输入值。
    data_type –它用于指定要转换的所需数据类型。

    data_type
     1. 字符型,可带参数 : CHAR()  
     2. 日期 : DATE  
     3. 时间: TIME  
     4. 日期时间型 : DATETIME  
     5. 浮点数 : DECIMAL  
     6. 整数 : SIGNED  
     7. 无符号整数 : UNSIGNED

    CONVERT( input_value, data_type )

    2、实例
    SELECT CONVERT(198, CHAR); 
    SELECT CONVERT('2019-11-19', DATETIME); 
    SELECT CONVERT(2-5, UNSIGNED); 
    SELECT CONVERT('geeksforgeeks' USING utf8); 

    二、转换字符集的语法
    1、语法
    input_value –用于指定输入值。
    character_set –它用于指定要转换为的所需字符集。

    CONVERT( input_value USING character_set )

    2、实例
    SELECT CONVERT('geeksforgeeks' USING utf8); 
    ————————————————
    版权声明:本文为CSDN博主「姚鑫国」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_44325655/article/details/124489287

  • 相关阅读:
    Day796.什么是线程数据的强、弱一致性 -Java 性能调优实战
    Flask之路由(app.route)详解
    顺序存储二叉树 [数据结构与算法][Java]
    Java之基本类、包装类、自动装箱、自动拆箱、集合和数组对比、三种for循环、遍历删除存在的安全问题
    【Java】反射是什么?
    可编程控制器有几种编程语言以及它们的特点
    【Linux学习】02Linux基础命令
    2022 年 8 月 GameFi 报告
    【AI】用iOS的ML(机器学习)创建自己的AI App
    excel公式怎么完全复制到另一列?
  • 原文地址:https://blog.csdn.net/m0_69157845/article/details/125632580