数据库

Day1

  1. 数据库系统
    1. server <——> cilent
    2. 实例
      1. 数据库
        1. schema
            1. 字段

一个数据库可以有多个实例 MySQL、DB2 Instance ——> 数据库 缺省数据库
写库的前缀

  1. 1. 字符型 char / varchar
  2. 1. 数值 int / bigint / smnint 精度不同 占用空间不同
  3. 1. 逻辑
  4. 1. 日期 date / datesteamp time 精度不同

聚合函数
窗口函数https://zhuanlan.zhihu.com/p/92654574
https://www.cnblogs.com/airen123/p/11015200.html

https://www.cnblogs.com/miracle-luna/p/11832595.html

https://blog.csdn.net/qq_34412985/article/details/113326535?ops_request_misc=%7B%22request%5Fid%22%3A%22163521922916780271596312%22%2C%22scm%22%3A%2220140713.130102334.pc%5Fall.%22%7D&request_id=163521922916780271596312&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-1-113326535.pc_search_result_cache&utm_term=MySQL%E4%B8%AD%E5%B0%86%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E4%B8%BA%E6%95%B4%E6%95%B0&spm=1018.2226.3001.4187

mysql:

  1. # mysql 字符串转日期
  2. select date_format(now(),'%Y-%m-%d');
  3. # 时间转时间戳
  4. select unix_timestamp(now());
  5. # 字符串转时间
  6. select str_to_date('2020-02-20','%Y-%m-%d %H');
  7. # 字符串转时间戳
  8. select unix_timestamp('2020-02-20');
  9. # 时间戳转时间
  10. select from_unixtime('1582128000');
  11. # 时间戳转字符串
  12. select from_unixtime('1582128000','%Y-%m-%d');
  13. # 字符串和数字的互相转换
  14. # 1.直接在字符串后面+0
  15. select '123'+0+'111';
  16. # 2.CAST(),使用方式为CAST(value AS type);
  17. select cast('5' as signed);
  18. select cast('5.24' as decimal(9,2)); # 9和2分别代表精度和小数点后位数
  19. # 3.CONVERT(),使用CONVERT(value, type); SELECT CONVERT('67',SIGNED);
  20. select convert('67',signed);
  21. select convert('67.35',decimal(9,2));
  22. # 将数字转化为字符串
  23. # 1.通用“+''”
  24. select 111+'';
  25. # 2.convert()
  26. select convert(25,char);

数值类型:

字符串类型

类型 用途
char 定长字符串
vachar 变长字符串
tinyblob 不超过 255 个字符的二进制字符串
tinytext 短文本字符串
blob 二进制形式的长文本数据
text 长文本
mediumblob 二进制形式的中等长度文本数据
mediumtext 中等长度文本数据
longblob 二进制形式的极大文本数据
longtext 极大文本数据

数值

类型 大小(Bytes) 用途
tinyint 1 小整数型
smallint 2 大整数型
mediumint 3 大整数型
int/integer 4 大整数型
bigint 8 极大整数型
float 4 单精度浮点数值
double 8 双精度浮点数值
decimal 对于decimal(m,d),如果M>D,为M+2否则为D+2 小数值

日期和时间类型

类型 格式 用途
date YYYY-MM-DD 日期值
time HH:MM:SS 时间值或持续时间
year YYYY 年份值
datetime YYYY-MM-DD HH:MM:SS 混合日期和时间值
timestamp YYYYMMDD HHMMSS 混合日期和时间值,时间戳

https://www.runoob.com/mysql/mysql-data-types.html

多表连接

MySQL主要分三种

inner join

内连接或者等值连接

内连接:SELECT * from a INNER JOIN b on a.name=b.id;

数据库 - 图1

left join

左连接:SELECT * from a left JOIN b on a.name=b.id;

数据库 - 图2

rigth join

SELECT * from a right JOIN b on a.name=b.id;

数据库 - 图3

全连接:mysql不支持全连接(full join),可以使用 left join+ union+right join

(SELECT from a left JOIN b on a.name=b.id) UNION (SELECT from a RIGHT JOIN b on a.name=b.id );

多表连接MySQL多表关联查询

[数据库和实例的区别]

数据库是一个逻辑上的概念,简单的说就是相互关联的一块数据。而对应到实际的物理概念上,就是磁盘上的一个或者一堆文件,里边包含着数据。

实例就是指的操作系统中一系列的进程以及为这些进程所分配的内存块,而它服务的对象就是数据库。简单说就是实例是我们访问数据库的通道。读取数据时都是先读取到内存中,然后再提交给用户。

不同的数据库实例的服务模式会有略微不同。在Oracle中,非RAC的数据库:一个数据库只能有一个实例为它提供服务;而对于RAC数据库:一个数据库可以有多个实例访问。而所有实例每次只能连接一个数据库。

聚合函数

在一个行的集合(一组行)上进行操作,对每个组给一个结果。

名称 作用
avg 求平均值
count 统计行的数量
max 求最大值
min 求最小值
sum 求累加和

MySQL最常用分组聚合函数

  • ①每个组函数接收一个参数
  • ②默认情况下,组函数忽略列值为null的行,不参与计算
  • ③有时,会使用关键字distinct剔除字段值重复的条数

Day2

查询

查询

子查询

  1. CREATE table if not EXISTS subquery.customers (
  2. `cust_id` int unsigned auto_increment,
  3. `cust_name` varchar(50) not null,
  4. `cust_address` varchar(100) not null,
  5. `cust_city` varchar(150) not null,
  6. `cust_state` varchar(20) not null,
  7. `cust_zip` int not null,
  8. `cust_country` varchar(25) not null,
  9. `cust_contact` varchar(40) not null,
  10. `cust_email` varchar(50) not null,
  11. primary key (`cust_id`)
  12. )
  13. ENGINE=InnoDB
  14. DEFAULT CHARSET=utf8mb4
  15. COLLATE=utf8mb4_general_ci
  16. COMMENT='存储顾客信息';
  17. CREATE table if not EXISTS subquery.orderitems (
  18. `order_num` int unsigned auto_increment,
  19. `order_item` int not null,
  20. `prod_id` varchar(20) not null,
  21. `quantity` int not null,
  22. `item_price` float not null,
  23. primary key (`order_num`)
  24. )
  25. ENGINE=InnoDB
  26. DEFAULT CHARSET=utf8mb4
  27. COLLATE=utf8mb4_general_ci
  28. COMMENT='只存储订单信息,无客户信息';
  29. # 增加一个主键
  30. CREATE table if not EXISTS subquery.orders (
  31. `order_num` int unsigned auto_increment,
  32. `order_date` datetime,
  33. `cust_id` int not null,
  34. primary key (`order_num`)
  35. )
  36. ENGINE=InnoDB
  37. DEFAULT CHARSET=utf8mb4
  38. COLLATE=utf8mb4_general_ci
  39. COMMENT='存储订单号和顾客id';
  40. # 添加数据
  41. insert into customers
  42. (`cust_id`,`cust_name`,`cust_address`,`cust_city`,`cust_state`,`cust_zip`,`cust_country`,`cust_contact`,`cust_email`)
  43. values
  44. (10001,'Coyote Inc','200 Maple Lans','Detroit','MI',44444,'USA','Y Lee','ylee@coyote.com'),
  45. (10002,'Mouse House','333 Fromage Lans','Columbus','OH',43333,'USA','Jerry Mouse',NULL),
  46. (10003,'Wascals','1 Sunny Place','Muncie','IN',42222,'USA','Jim Jones','rabbit@qq.com'),
  47. (10004,'Yosemite Place','829 Riverside Drive','Phoenix','AZ',88888,'USA','Y Sam','sam@yo.com'),
  48. (10005,'E Fudd','4545 53rd Street','Chicago','IL',54545,'USA','E Fudd',NULL);
  49. insert into orderitems
  50. (`order_num`,`order_item`,`prod_id`,`quantity`,`item_price`)
  51. values
  52. (20005,1,'ANV01',10,5.99),
  53. (20005,2,'ANV02',3,9.99),
  54. (20005,3,'TNT2',5,10.00),
  55. (20005,4,'FB',1,10.00),
  56. (20006,1,'JP2000',1,55.00),
  57. (20007,1,'TNT2',100,10.00),
  58. (20008,1,'FC',50,2.50),
  59. (20009,1,'FB',1,10.00),
  60. (20009,2,'OL1',1,8.99),
  61. (20009,3,'SLING',1,4.49),
  62. (20009,4,'ANV03',1,14.99);
  63. insert into orders
  64. (`order_num`,`order_date`,`cust_id`)
  65. values
  66. (20005,'2005-09-01 00:00:00',10001),
  67. (20006,'2005-09-12 00:00:00',10003),
  68. (20007,'2005-09-30 00:00:00',10004),
  69. (20008,'2005-10-03 00:00:00',10005),
  70. (20009,'2005-10-08 00:00:00',10001);

注意:一般在子查询中,程序先运行在嵌套在最内层的语句,再运行外层。因此在写子查询语句时,可以先测试下内层的子查询语句是否输出了想要的内容,再一层层往外测试,增加子查询正确率。否则多层的嵌套使语句可读性很低。

查询买了商品为’TNT2’的顾客信息

1. 在orderitems里找出TNT2的订单号

  1. select `order_id` from subquery.orderitems where prod_id = 'TNT2';

2. 在orders里找出第一步找出的订单号对应的客户id

  1. select `cust_id` from subquery.orders where `order_id` in (select `order_id` from subquery.orderitems where prod_id = 'TNT2');

3. 在customers中找出第二步对应客户的所有信息

  1. select * from subquery.customers where `cust_id` in (select `cust_id` from subquery.orders where `order_id` in (select `order_id` from subquery.orderitems where prod_id = 'TNT2'));

子查询一般与IN操作符结合使用,也可用=><等。

想要查询每个客户的订单数

  1. 查询某个客户的订单数
  1. select count(*) as order1 from subquery.orders where `cust_id` = 10001;
  1. 某个客户改为所有客户。
  1. select `cust_name`,`cust_state`,
  2. (select count(*) from orders where orders.cust_id = customers.cust_id)
  3. as order1
  4. from customers
  5. order by cust_name;

子查询

explain

profile

聚簇索引

variables

case when

https://www.jb51.net/article/126107.htm

explain

作用
1、表的读取顺序
2、数据读取操作的操作类型
3、哪些索引可以使用
4、哪些索引被实际使用
5、表之间的引用
6、每张表有多少行被优化器查询

  1. -- 实际SQL,查找用户名为Jefabc的员工
  2. select * from tt_repair_order;
  3. -- 查看SQL是否使用索引,前面加上explain即可
  4. explain select * from tt_repair_order;

数据库 - 图4

查询出来有十二列,分别是id、select_type、table、type、possible_keys、key、key_len、ref、rows、filtered、Extra

  1. id:选择表示符
  2. select_type:查询的类型
  3. table:输出结果集的表
  4. partition:匹配的分区
  5. type:表示表的连接类型
  6. possible_keys:表示查询时,可能使用的索引
  7. key:表示实际使用的索引
  8. key_len:索引字段的长度
  9. ref:列与索引的比较
  10. rows:扫描出的行数(估算的行数)
  11. filtered:按表条件过滤的行百分比
  12. Extra:执行情况的描述和说明

Explain

MySQL优化——看懂explain

id

SELECT识别符,是SELECT的查询序列号,SQL执行的顺序的标识,SQL从大到小的执行

  1. id相同时,执行顺序由上至下。
  2. 如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行。
  3. id如果相同,可以认为是一组,从上往下顺序执行;在所有组中,id值越大,优先级越高,越先执行。

select_type

查询中每个select子句的类型

  1. SIMPLE(简单select,不使用UNION或者子查询等)
  2. PRIMARY(子查询中最外层查询,查询中若包含任何复杂的子部分,最外层的select被标记为PRIMARY)
  3. UNION(UNION中第二个或后面的SELECT语句)
  4. DEPENDENT UNION(UNION中第二个或后面的SELECT语句,取决于外面的查询)
  5. UNION RESULT(UNION的结果,union语句中第二个select开始后面所有select)
  6. SUBQUERY(子查询中的第一个select,结果不依赖于外部查询)
  7. DEPENDENT SUBERY(子查询中的第一个SELECT,依赖于外部查询)
  8. DERIVED(派生表的SELECT,FROM子句的子查询)
  9. UNCACHEABLE SUBQUERY(一个子查询的结果不能被缓存,必须重新评估外链接的第一行)

table

显示这一步所访问数据库中表名称,输出的行所引用的表。

type

对表访问方式,表示MySQL在表中找到所需行的方式,又称“访问类型”。

常用的类型有:ALL、index、range、ref、eq_ref、const、system、NULL(从左往右,性能从差到好)

  • ALL:Full Table Scan,MySQL将遍历全表以找到匹配的行
  • index:Full Index Scan,index与ALL区别为index类型只遍历索引树
  • range:只检索给定范围的行,使用一个索引来选择行
  • ref:表示上述表的连接匹配条件,即哪些列或常量被用于查找索引列上的值
  • eq_ref:类似ref,区别就在使用的索引是唯一索引,对于每个索引键值,表中只有一条记录匹配,简单来说,就是多表连接中使用primary key或者 unique key作为关联条件
  • const、system:当MySQL对查询某部分进行优化,并转换为一个常量时,使用这些类型访问。如将主键置于where列表中,MySQL就能将该查询转换为一个常量,system是const类型的特例,当查询的表只有一行的情况下,使用system
  • NULL:MySQL在优化过程中分解语句,执行时甚至不用访问表或索引,例如从一个索引列里选取最小值可以通过单独索引查找完成。

possible_keys

指出MySQL能使用哪个索引在表中找到记录,查询涉及到字段上若存在索引,则该索引将被引出,但不一定被查询使用(该查询可以利用的索引,如果没有任何索引显示为null)

该列完全独立于EXPLAIN输出所示的表的次序。这意味着在possible_keys中存在某些键实际上不能按生成的表次序使用。

如果该列为null,则没有相关索引。在这种情况下,可以通过检查WHERE子句看是否它引用某些列或适合索引的列来提高你的查询性能。如果是这样,创造一个适当的索引并且再次用EXPLAIN检查查询

key

key列显示MySQL实际决定使用的键(索引),必然包含在possible_keys中

如果没有选择索引,键是NULL。要想强制MySQL使用或忽视possible_keys列中的索引,在查询中使用FORCE INDEX、USE INDEX或者IGNORE INDEX。

key_len

表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度(key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的)

不损失精确性的情况下,长度越短越好

ref

列与索引的比较,表示上述表的连接匹配条件,即哪些列或常量被用于查找索引列上的值

rows

估算出结果集行数,表示MySQL根据表统计信息及索引选用情况,估算的找到所需的记录所需要读取的行数

Extra

该列包含MySQL解决查询的详细信息,有以下几种情况:

  • Using where:不用读取表中所有信息,仅通过索引就可以获取所需数据,这发生在对表的全部的请求列都是同一个索引的部分的时候,表示mysql服务器将在存储引擎检索行后再进行过滤
  • Using temporary:表示MySQL需要使用临时表来存储结果集,常见于排序和分组查询,常见 group by ; order by
  • Using filesort:当Query中包含 order by 操作,而且无法利用索引完成的排序操作称为“文件排序”
  1. -- 测试Extrafilesort
  2. explain select * from emp order by name;
  • Using join buffer:该值强调了在获取连接条件时没有使用索引,并且需要连接缓冲区来存储中间结果。如果出现了这个值,那应该注意,根据查询的具体情况可能需要添加索引来改进能。
  • Impossible where:这个值强调了where语句会导致没有符合条件的行(通过收集统计信息不可能存在结果)。
  • Select tables optimized away:这个值意味着仅通过使用索引,优化器可能仅从聚合函数结果中返回一行
  • No tables used:Query语句中使用from dual 或不含任何from子句
  1. -- explain select now() from dual;

总结:
• EXPLAIN不会告诉关于触发器、存储过程的信息或用户自定义函数对查询的影响情况
• EXPLAIN不考虑各种Cache
• EXPLAIN不能显示MySQL在执行查询时所作的优化工作
• 部分统计信息是估算的,并非精确值
• EXPALIN只能解释SELECT操作,其他操作要重写为SELECT后查看执行计划。

通过收集统计信息不可能存在结果

Show

https://www.cnblogs.com/saneri/p/6963583.html

profile

https://www.cnblogs.com/flzs/p/9974822.html

https://www.cnblogs.com/developer_chan/p/9231761.html

case when

行转列 case when 列转行 union

https://www.jianshu.com/p/1bfddf9fd91e

https://blog.csdn.net/u013514928/article/details/80969949

https://blog.csdn.net/rocling/article/details/82083332

https://www.cnblogs.com/chenduzizhong/p/9590741.html

https://www.cnblogs.com/xiaoxi/p/7151433.html

Day3

行转列 case when 列转行 union

cdk ——> spark

数据结构、数据类型、数据组合

行转列,列转行

  1. select
  2. sum( if(xb='男','1','0') ) as '男',
  3. sum( if(xb='女','1','0') ) as '女',
  4. count(1) as total
  5. from testuser t
  6. select
  7. sum(case xb when '男' then 1 else 0 end ) as '男',
  8. sum(case xb when '女' then 1 else 0 end ) as '女',
  9. count(1) as total
  10. from testuser t
  11. # 也可以通过where
  12. SELECT
  13. COUNT(if(xb LIKE "男",xb,NULL)) as '男',
  14. COUNT(if(xb LIKE "女",xb,NULL)) as '女',
  15. COUNT(xb) as '总计'
  16. FROM person;
  17. SELECT
  18. COUNT(CASE xb when '男' THEN '男' ELSE NULL END) as '男',
  19. COUNT(CASE xb when '女' THEN '女' ELSE NULL END) as '女',
  20. COUNT(xb) as '总计'
  21. FROM person;
  22. SELECT id,area as '地区',sales1 as '销量' FROM sales
  23. UNION ALL
  24. SELECT id,area as '地区',sales2 as '销量' FROM sales
  25. select id,area as '地区' ,
  26. group_concat(sales1,"+",sales2,"=",sales1+sales2) as '销量(销量1,销量2,总销量)'
  27. from sales group by id;

https://blog.csdn.net/LOOCOO225/article/details/110389424


Day4

数据库 - 图5

  1. #6
  2. select
  3. if (left(PPARTNO,1) in ('B','M','E','C','N'),substring(PPARTNO,2,2),(
  4. if (PPARTNO like 'Z9908%LGL',99,left(PPARTNO,2))))
  5. as MAIN_GROUP
  6. from m_42439_tm_part_stock_card_5;

数据库 - 图6

  1. #7
  2. SELECT CASE
  3. WHEN GROUP1 LIKE 'B%' or 'M%' or 'E%' or 'C%' THEN SUBSTRING(PPARTNO,4,2)
  4. WHEN PPARTNO LIKE 'Z9908%LGL' THEN SUBSTRING(PPARTNO,4,2)
  5. ELSE SUBSTRING(PPARTNO,3,2)
  6. END AS SUB_GROUP
  7. FROM m_42439_tm_part_stock_card_5;

数据库 - 图7

  1. #8
  2. select case
  3. when GROUP1 like 'B%' or '%M' or 'E%' or 'C%' then substring(PPARTNO,6,7)
  4. when PPARTNO like 'Z9908%LGL' then substring(PPARTNO,6,7)
  5. else substring(PPARTNO,5,7)
  6. end as PART_NUM
  7. from m_42439_tm_part_stock_card_5;

数据库 - 图8

  1. #9
  2. SELECT CASE
  3. WHEN GROUP1 LIKE 'B%' or 'M%' or 'E%' or 'C%' THEN '50001000' + SUBSTRING(GROUP1,2,2)
  4. WHEN PPARTNO LIKE 'Z9908%LGL' THEN '50001012'
  5. ELSE '50001016'
  6. END AS PART_TYPE
  7. FROM m_42439_tm_part_stock_card_5;

数据库 - 图9

  1. #11
  2. SELECT
  3. LEFT(PPARTNO,1) as BRAND_CODE
  4. FROM m_42439_tm_part_stock_card_5;

数据库 - 图10

  1. #12
  2. SELECT PART_NO,sum((AVGCOST*TOTALQTY))/(case when sum(TOTALQTY)=0 then 1 else sum(TOTALQTY) end) AS AVERAGE_COST
  3. FROM m_42439_tm_part_stock_card_5 GROUP BY PART_NO;

数据库 - 图11

  1. #13
  2. SELECT LASTCOST AS LATEST_COST
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图12

  1. #14
  2. SELECT PRICE1 AS RECOMMEND_PRICE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图13

  1. #15
  2. SELECT CASE
  3. WHEN GROUP1 LIKE 'B%' or 'M%' or 'E%' or 'C%' THEN price2
  4. WHEN PPARTNO LIKE 'Z9908%LGL' THEN price2
  5. ELSE price2/(case when trim(pfactor)=0 then 1 else pfactor end)
  6. END AS BBA_COST
  7. FROM m_42439_tm_part_stock_card_5;

数据库 - 图14

  1. #16
  2. SELECT ROUND(PRICE1*1.13,4) AS TAX_PRICE
  3. FROM m_42439_tm_part_stock_card_5;

数据库 - 图15

  1. #17
  2. SELECT NLPRICE AS LAST_RECOMMEND_PRICE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT max(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图16

  1. #18
  2. SELECT NLPRICE AS LAST_RCMD_DATE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT max(LASTBUY) FROM m_42439_tm_part_stock_card_5 group by `PART_NO`);

数据库 - 图17

  1. #21
  2. SELECT NLDATE AS LAST_RCMD_DATE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT max(LASTBUY) FROM m_42439_tm_part_stock_card_5 group by `PART_NO`);

数据库 - 图18

  1. #22
  2. SELECT TOTALQTY AS TOTAL_STOCK_QTY
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT max(LASTBUY) FROM m_42439_tm_part_stock_card_5 group by `PART_NO`);

数据库 - 图19

  1. #23
  2. SELECT MAXIMUM AS MAX_QTY
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 group by `PART_NO`);
  6. #24
  7. SELECT MINIMUM AS MIN_QTY
  8. FROM m_42439_tm_part_stock_card_5
  9. WHERE LASTBUY IN
  10. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 group by `PART_NO`);

数据库 - 图20

  1. #26
  2. SELECT SUM(TOTALQTY) AS TOTAL_AVAILABLE_QTY
  3. FROM m_42439_tm_part_stock_card_5 GROUP BY PART_NO;

数据库 - 图21

  1. #29
  2. SELECT SUM(QUANTITY7) AS SCRAP_STORAGE_QTY
  3. FROM m_42439_tm_part_stock_card_5 GROUP BY PART_NO;

数据库 - 图22

  1. #30
  2. SELECT SUM(QUANTITY8) AS CLAIM_STORAGE_QTY
  3. FROM m_42439_tm_part_stock_card_5 GROUP BY PART_NO;

数据库 - 图23

  1. #31
  2. SELECT CASE NOUPDATE
  3. WHEN 'Y' THEN '10041001'
  4. WHEN 'N' THEN '10041002'
  5. END AS PROHIBIT_PRICE_UPDATE
  6. FROM m_42439_tm_part_stock_card_5
  7. WHERE LASTBUY IN
  8. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图24

  1. #32
  2. SELECT CASE
  3. WHEN KIT ='Y' AND BALANCE = 'Y' THEN '50011001'
  4. WHEN KIT ='Y' THEN '50011002'
  5. ELSE '0'
  6. END AS ASSEMBLY_KIT
  7. FROM m_42439_tm_part_stock_card_5
  8. WHERE LASTBUY IN
  9. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图25

  1. #39
  2. SELECT CASE DISCOUNT
  3. WHEN 'A' THEN '18'
  4. WHEN 'B' THEN '20'
  5. WHEN 'C' THEN '24'
  6. WHEN 'D' THEN '27'
  7. WHEN 'E' THEN '31'
  8. WHEN 'F' THEN '36'
  9. WHEN 'G' THEN '40'
  10. WHEN 'H' THEN '45'
  11. WHEN 'I' THEN '49'
  12. WHEN 'K' THEN '52'
  13. WHEN 'P' THEN '5'
  14. WHEN 'N' THEN '63'
  15. WHEN 'L' THEN '56'
  16. WHEN 'M' THEN '58'
  17. END AS DISCOUNT_CODE
  18. FROM m_42439_tm_part_stock_card_5
  19. WHERE LASTBUY IN
  20. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图26

  1. #40
  2. SELECT SALECODE AS FAMILY_CODE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图27

  1. #41
  2. SELECT REORDER AS ALOIS_CATEGORY
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECTMAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图28

  1. #42
  2. SELECT SUBCAT AS ALOIS_SUBCATEGORY
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT max(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图29

  1. #43
  2. SELECT LEFT(MLI1,1) AS REPLENISHMENT_CODE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图30

  1. #44
  2. SELECT CASE
  3. WHEN LENGTH(MLI1) = 3 THEN SUBSTRING(MLI1,3,1)
  4. WHEN LENGTH(MLI1) < 3 THEN NULL
  5. END AS REPLENISHMENT_CODE_OLD
  6. FROM m_42439_tm_part_stock_card_5
  7. WHERE LASTBUY IN
  8. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

https://blog.csdn.net/wangxi06/article/details/115090047

数据库 - 图31

  1. #45
  2. SELECT CASE
  3. WHEN BALANCE = 'Y' THEN '50021001'
  4. WHEN BALANCE <> 'Y' THEN '50021002'
  5. END AS IS_INVENTORY
  6. FROM m_42439_tm_part_stock_card_5
  7. WHERE LASTBUY IN
  8. (SELECT max(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图32

  1. #50
  2. SELECT FIRSTMV AS FIRST_PURCHASE_DATE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);
  1. #51
  2. SELECT FIRSTMV AS FIRST_SALES_DATE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图33

  1. #52
  2. SELECT LASTSOLD AS LAST_SALES_DATE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图34

  1. #53
  2. SELECT LASTBUY AS LAST_PURCHASE_DATE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图35

  1. #54
  2. SELECT MOVED AS LAST_FLOW_DATE
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图36

  1. #55
  2. SELECT
  3. IF(SUPER = PPARTNO,null,SUPER) AS NEW_REPLACE_CODE
  4. FROM m_42439_tm_part_stock_card_5;
  5. #56
  6. SELECT
  7. IF(SUPFROM = PPARTNO,null,SUPFROM) AS OLD_REPLACE_CODE
  8. FROM m_42439_tm_part_stock_card_5;

数据库 - 图37

  1. #60
  2. SELECT LOSTQTY AS SALES_LOSS_QTY
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY
  5. IN (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图38

  1. #66
  2. SELECT IF(DECIMAL1 = 'Y' ,10041001,10041002) AS IS_DECIMAL_OUT
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图39

  1. #67
  2. SELECT DISTINCT ECONBUY AS MIN_PURCHASE_QTY
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图40

  1. #78
  2. SELECT DISTINCT RAISED AS CREATED_AT
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY IN
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

数据库 - 图41

  1. #81
  2. SELECT DISTINCT CONCAT(`EDITDATE` , `EDITTIME`) AS UPDATED_AT
  3. FROM m_42439_tm_part_stock_card_5
  4. WHERE LASTBUY in
  5. (SELECT MAX(LASTBUY) FROM m_42439_tm_part_stock_card_5 GROUP BY `PART_NO`);

Day5

  1. SELECT *
  2. FROM (SELECT PART_NO,MAX(LASTBUY) AS LASTBUY FROM m_42439_tm_part_stock_card_5 GROUP BY PART_NO) AS M1,
  3. m_42439_tm_part_stock_card_5 AS M2
  4. WHERE M1.PART_NO = M2.PART_NO AND M1.LASTBUY = M2.LASTBUY GROUP BY M2.PART_NO;
  1. SELECT M1.PART_NO,
  2. if (left(PPARTNO,1) in ('B','M','E','C','N'),substring(PPARTNO,2,2),(
  3. if (PPARTNO like 'Z9908%LGL',99,left(PPARTNO,2))))
  4. as MAIN_GROUP -- 6
  5. ,CASE
  6. WHEN GROUP1 LIKE 'B%' or 'M%' or 'E%' or 'C%' THEN SUBSTRING(PPARTNO,4,2)
  7. WHEN PPARTNO LIKE 'Z9908%LGL' THEN SUBSTRING(PPARTNO,4,2)
  8. ELSE SUBSTRING(PPARTNO,3,2)
  9. END AS SUB_GROUP -- 7
  10. ,case
  11. when GROUP1 like 'B%' or '%M' or 'E%' or 'C%' then substring(PPARTNO,6,7)
  12. when PPARTNO like 'Z9908%LGL' then substring(PPARTNO,6,7)
  13. else substring(PPARTNO,5,7)
  14. end as PART_NUM -- 8
  15. ,CASE
  16. WHEN GROUP1 LIKE 'B%' or 'M%' or 'E%' or 'C%' THEN '50001000' + SUBSTRING(GROUP1,2,2)
  17. WHEN PPARTNO LIKE 'Z9908%LGL' THEN '50001012'
  18. ELSE '50001016'
  19. END AS PART_TYPE -- 9
  20. ,LEFT(PPARTNO,1) as BRAND_CODE -- 11
  21. ,SUM((AVGCOST*TOTALQTY))/(case when sum(TOTALQTY)=0 then 1 else sum(TOTALQTY) end) AS AVERAGE_COST -- 12
  22. ,LASTCOST AS LATEST_COST -- 13
  23. ,PRICE1 AS RECOMMEND_PRICE -- 14
  24. ,CASE
  25. WHEN GROUP1 LIKE 'B%' or 'M%' or 'E%' or 'C%' THEN price2
  26. WHEN PPARTNO LIKE 'Z9908%LGL' THEN price2
  27. ELSE price2/(case when trim(pfactor)=0 then 1 else pfactor end)
  28. END AS BBA_COST -- 15
  29. ,ROUND(PRICE1*1.13,4) AS TAX_PRICE -- 16
  30. ,NLPRICE AS LAST_RECOMMEND_PRICE -- 17
  31. ,NLPRICE AS LAST_RCMD_DATE -- 18
  32. ,NLDATE AS LAST_RCMD_DATE -- 21
  33. ,TOTALQTY AS TOTAL_STOCK_QTY -- 22
  34. ,MAXIMUM AS MAX_QTY -- 23
  35. ,MINIMUM AS MIN_QTY -- 24
  36. ,SUM(TOTALQTY) AS TOTAL_AVAILABLE_QTY -- 26
  37. ,SUM(QUANTITY7) AS SCRAP_STORAGE_QTY -- 29
  38. ,SUM(QUANTITY7) AS CLAIM_STORAGE_QTY -- 30
  39. ,CASE NOUPDATE
  40. WHEN 'Y' THEN '10041001'
  41. WHEN 'N' THEN '10041002'
  42. END AS PROHIBIT_PRICE_UPDATE -- 31
  43. ,CASE
  44. WHEN KIT ='Y' AND BALANCE = 'Y' THEN '50011001'
  45. WHEN KIT ='Y' THEN '50011002'
  46. ELSE '0'
  47. END AS ASSEMBLY_KIT -- 32
  48. ,CASE DISCOUNT
  49. WHEN 'A' THEN '18' WHEN 'B' THEN '20' WHEN 'C' THEN '24' WHEN 'D' THEN '27'
  50. WHEN 'E' THEN '31' WHEN 'F' THEN '36' WHEN 'G' THEN '40' WHEN 'H' THEN '45'
  51. WHEN 'I' THEN '49' WHEN 'K' THEN '52' WHEN 'P' THEN '5' WHEN 'N' THEN '63'
  52. WHEN 'L' THEN '56' WHEN 'M' THEN '58'
  53. END AS DISCOUNT_CODE -- 39
  54. ,SALECODE AS FAMILY_CODE -- 40
  55. ,REORDER AS ALOIS_CATEGORY -- 41
  56. ,SUBCAT AS ALOIS_SUBCATEGORY -- 42
  57. ,LEFT(MLI1,1) AS REPLENISHMENT_CODE -- 43
  58. ,CASE
  59. WHEN LENGTH(MLI1) = 3 THEN SUBSTRING(MLI1,3,1)
  60. WHEN LENGTH(MLI1) < 3 THEN NULL
  61. END AS REPLENISHMENT_CODE_OLD -- 44
  62. ,CASE
  63. WHEN BALANCE = 'Y' THEN '50021001'
  64. WHEN BALANCE <> 'Y' THEN '50021002'
  65. END AS IS_INVENTORY -- 45
  66. ,FIRSTMV AS FIRST_PURCHASE_DATE -- 50
  67. ,FIRSTMV AS FIRST_SALES_DATE -- 51
  68. ,LASTSOLD AS LAST_SALES_DATE -- 52
  69. ,M2.LASTBUY AS LAST_PURCHASE_DATE -- 53
  70. ,MOVED AS LAST_FLOW_DATE -- 54
  71. ,IF(SUPER = PPARTNO,null,SUPER) AS NEW_REPLACE_CODE -- 55
  72. ,IF(SUPFROM = PPARTNO,null,SUPFROM) AS OLD_REPLACE_CODE -- 56
  73. ,LOSTQTY AS SALES_LOSS_QTY -- 60
  74. ,IF(DECIMAL1 = 'Y' ,10041001,10041002) AS IS_DECIMAL_OUT -- 66
  75. ,ECONBUY AS MIN_PURCHASE_QTY -- 67
  76. ,RAISED AS CREATED_AT -- 78
  77. ,CONCAT(`EDITDATE` , `EDITTIME`) AS UPDATED_AT -- 81
  78. FROM (SELECT PART_NO,MAX(LASTBUY) AS LASTBUY FROM m_42439_tm_part_stock_card_5 GROUP BY PART_NO) AS M1,
  79. m_42439_tm_part_stock_card_5 AS M2
  80. WHERE M1.PART_NO = M2.PART_NO AND M1.LASTBUY = M2.LASTBUY GROUP BY M2.PART_NO;

  1. SELECT M1.PART_NO,
  2. if (left(PPARTNO,1) in ('B','M','E','C','N'),substring(PPARTNO,2,2),(
  3. if (PPARTNO like 'Z9908%LGL',99,left(PPARTNO,2))))
  4. as MAIN_GROUP -- 6
  5. ,CASE
  6. WHEN GROUP1 LIKE 'B%' or 'M%' or 'E%' or 'C%' THEN SUBSTRING(PPARTNO,4,2)
  7. WHEN PPARTNO LIKE 'Z9908%LGL' THEN SUBSTRING(PPARTNO,4,2)
  8. ELSE SUBSTRING(PPARTNO,3,2)
  9. END AS SUB_GROUP -- 7
  10. ,case
  11. when GROUP1 like 'B%' or '%M' or 'E%' or 'C%' then substring(PPARTNO,6,7)
  12. when PPARTNO like 'Z9908%LGL' then substring(PPARTNO,6,7)
  13. else substring(PPARTNO,5,7)
  14. end as PART_NUM -- 8
  15. ,CASE
  16. WHEN GROUP1 LIKE 'B%' or 'M%' or 'E%' or 'C%' THEN '50001000' + SUBSTRING(GROUP1,2,2)
  17. WHEN PPARTNO LIKE 'Z9908%LGL' THEN '50001012'
  18. ELSE '50001016'
  19. END AS PART_TYPE -- 9
  20. ,LEFT(PPARTNO,1) as BRAND_CODE -- 11
  21. ,LASTCOST AS LATEST_COST -- 13
  22. ,PRICE1 AS RECOMMEND_PRICE -- 14
  23. ,CASE
  24. WHEN GROUP1 LIKE 'B%' or 'M%' or 'E%' or 'C%' THEN price2
  25. WHEN PPARTNO LIKE 'Z9908%LGL' THEN price2
  26. ELSE price2/(case when trim(pfactor)=0 then 1 else pfactor end)
  27. END AS BBA_COST -- 15
  28. ,ROUND(PRICE1*1.13,4) AS TAX_PRICE -- 16
  29. ,NLPRICE AS LAST_RECOMMEND_PRICE -- 17
  30. ,NLPRICE AS LAST_RCMD_DATE -- 18
  31. ,NLDATE AS LAST_RCMD_DATE -- 21
  32. ,TOTALQTY AS TOTAL_STOCK_QTY -- 22
  33. ,MAXIMUM AS MAX_QTY -- 23
  34. ,MINIMUM AS MIN_QTY -- 24
  35. ,CASE NOUPDATE
  36. WHEN 'Y' THEN '10041001'
  37. WHEN 'N' THEN '10041002'
  38. END AS PROHIBIT_PRICE_UPDATE -- 31
  39. ,CASE
  40. WHEN KIT ='Y' AND BALANCE = 'Y' THEN '50011001'
  41. WHEN KIT ='Y' THEN '50011002'
  42. ELSE '0'
  43. END AS ASSEMBLY_KIT -- 32
  44. ,CASE DISCOUNT
  45. WHEN 'A' THEN '18' WHEN 'B' THEN '20' WHEN 'C' THEN '24' WHEN 'D' THEN '27'
  46. WHEN 'E' THEN '31' WHEN 'F' THEN '36' WHEN 'G' THEN '40' WHEN 'H' THEN '45'
  47. WHEN 'I' THEN '49' WHEN 'K' THEN '52' WHEN 'P' THEN '5' WHEN 'N' THEN '63'
  48. WHEN 'L' THEN '56' WHEN 'M' THEN '58'
  49. END AS DISCOUNT_CODE -- 39
  50. ,SALECODE AS FAMILY_CODE -- 40
  51. ,REORDER AS ALOIS_CATEGORY -- 41
  52. ,SUBCAT AS ALOIS_SUBCATEGORY -- 42
  53. ,LEFT(MLI1,1) AS REPLENISHMENT_CODE -- 43
  54. ,CASE
  55. WHEN LENGTH(MLI1) = 3 THEN SUBSTRING(MLI1,3,1)
  56. WHEN LENGTH(MLI1) < 3 THEN NULL
  57. END AS REPLENISHMENT_CODE_OLD -- 44
  58. ,CASE
  59. WHEN BALANCE = 'Y' THEN '50021001'
  60. WHEN BALANCE <> 'Y' THEN '50021002'
  61. END AS IS_INVENTORY -- 45
  62. ,FIRSTMV AS FIRST_PURCHASE_DATE -- 50
  63. ,FIRSTMV AS FIRST_SALES_DATE -- 51
  64. ,LASTSOLD AS LAST_SALES_DATE -- 52
  65. ,M2.LASTBUY AS LAST_PURCHASE_DATE -- 53
  66. ,MOVED AS LAST_FLOW_DATE -- 54
  67. ,IF(SUPER = PPARTNO,null,SUPER) AS NEW_REPLACE_CODE -- 55
  68. ,IF(SUPFROM = PPARTNO,null,SUPFROM) AS OLD_REPLACE_CODE -- 56
  69. ,LOSTQTY AS SALES_LOSS_QTY -- 60
  70. ,IF(DECIMAL1 = 'Y' ,10041001,10041002) AS IS_DECIMAL_OUT -- 66
  71. ,ECONBUY AS MIN_PURCHASE_QTY -- 67
  72. ,RAISED AS CREATED_AT -- 78
  73. ,CONCAT(`EDITDATE` , `EDITTIME`) AS UPDATED_AT -- 81
  74. ,M1.AVERAGE_COST
  75. ,M1.TOTAL_AVAILABLE_QTY
  76. ,M1.SCRAP_STORAGE_QTY
  77. ,M1.CLAIM_STORAGE_QTY
  78. FROM (SELECT PART_NO
  79. ,MAX(LASTBUY) AS LASTBUY
  80. ,SUM((AVGCOST*TOTALQTY))/(case when sum(TOTALQTY)=0
  81. then 1 else sum(TOTALQTY) end) AS AVERAGE_COST -- 12
  82. ,SUM(TOTALQTY) AS TOTAL_AVAILABLE_QTY -- 26
  83. ,SUM(QUANTITY7) AS SCRAP_STORAGE_QTY -- 29
  84. ,SUM(QUANTITY7) AS CLAIM_STORAGE_QTY -- 30
  85. FROM m_42439_tm_part_stock_card_5 GROUP BY PART_NO) AS M1,
  86. m_42439_tm_part_stock_card_5 AS M2
  87. WHERE M1.PART_NO = M2.PART_NO
  88. AND M1.LASTBUY = M2.LASTBUY
  89. GROUP BY M2.PART_NO;

Day6

  1. select
  2. sum(CKINQTY) as IN_QTY, -- 10
  3. sum(CKINVAL) as IN_AMOUNT, -- 11
  4. sum(CKOUTQTY) as OUT_QTY, -- 12
  5. sum(CKOUTVAL) as OUT_AMOUNT, -- 13
  6. sum(TOTALQTY) as END_QTY, -- 14
  7. (case when sum(TOTALQTY)!=0.0
  8. then sum(TOTALQTY*AVGCOST)/sum(TOTALQTY) else 0 end) as END_PRICE, -- 15
  9. sum(TOTALQTY*AVGCOST) as END_AMOUNT, -- 16
  10. sum(HISTORY1) as DEMAND_QTY, -- 17
  11. sum(REQUESTS1) as DEMAND_FREQUENCY, -- 18
  12. sum(QUANTITY1) as ONE_HOUSE, -- 20
  13. sum(QUANTITY2) as TOW_HOUSE, -- 21
  14. sum(QUANTITY3) as THREE_HOUSE, -- 22
  15. sum(QUANTITY4) as FOUR_HOUSE, -- 23
  16. sum(QUANTITY5) as FIVE_HOUSE, -- 24
  17. sum(QUANTITY6) as SIX_HOUSE, -- 25
  18. sum(QUANTITY7) as SCRAP_HOUSE, -- 26
  19. sum(QUANTITY8) as CLAIM_HOUSE, -- 27
  20. sum(QUANTITY9) as NINE_HOUSE, -- 28
  21. sum(INQTY) as PURCHASE_IN_QTY, -- 29
  22. sum(INVAL) as PURCHASE_IN_AMOUNT, -- 30
  23. sum(OUTQTY) as SALES_OUT_QTY, -- 31
  24. sum(OUTVAL) as SALES_OUT_AMOUNT, -- 32
  25. sum(CKINQTY) as INVENTORY_IN_QTY, -- 33
  26. sum(CKINVAL) as INVENTORY_IN_AMOUNT, -- 34
  27. sum(CKOUTQTY) as INVENTORY_OUT_QTY, -- 35
  28. sum(CKOUTVAL) as INVENTORY_OUT_AMOUNT -- 36
  29. from bmw_mgr.m_42439_tt_part_month_report_5
  30. where part_no<>'Total'
  31. group by YEAR1,MONTH,PART_NO