前言

本文隶属于专栏《1000个问题搞定大数据技术体系》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!

本专栏目录结构和参考文献请见1000个问题搞定大数据技术体系

目录

Spark SQL 内置函数(一)Array Functions(基于 Spark 3.2.0)

Spark SQL 内置函数(二)Map Functions(基于 Spark 3.2.0)

Spark SQL 内置函数(三)Date and Timestamp Functions(基于 Spark 3.2.0)

Spark SQL 内置函数(四)JSON Functions(基于 Spark 3.2.0)

Spark SQL 内置函数(五)Aggregate Functions(基于 Spark 3.2.0)

Spark SQL 内置函数(六)Window Functions(基于 Spark 3.2.0)

正文

cume_dist()

描述

计算值相对于分区中所有值的位置。

dense_rank()

描述

  • 计算一组值中某个值的排名。
  • 结果是 1 加上先前分配的排名值。
  • 与函数 rank 不同,dense_rank 不会在排名序列中产生间隙。

比如总共 4 个人,2 个第二名,最后一名,按照 rank 的排法是第四名, dense_rank 的排法就是第三名。

lag(input[, offset[, default]])

描述

  • 返回窗口中当前行之前的偏移量offset行处的input值。
  • offset的默认值为 1,default的默认值为 null。
  • 如果偏移量offset行处的input值为 null,则返回 null。
  • 如果没有这样的偏移行(例如,当偏移量为 1 时,窗口的第一行没有任何前一行),则返回default

lead(input[, offset[, default]])

描述

  • 返回窗口中当前行之后的偏移量offset行处的input值。
  • offset的默认值为 1,default的默认值为 null。
  • 如果偏移量offset行处的input值为 null,则返回 null。
  • 如果没有这样的偏移行(例如,当偏移量为 1 时,窗口的最后一行没有任何后一行),则返回default

nth_value(input[, offset])

描述

  • 返回从窗口开头的偏移量offset所在的行处的"input"值。
  • 偏移量offset从 1 开始。
  • 如果 ignoreNulls=true,我们将在找到偏移量offset时跳过 null。
  • 否则,每一行都算作偏移量offset
  • 如果没有这样的偏移量offset(例如,当偏移量offset为 10 时,窗口的大小小于 10),则返回 null。

ntile(n)

描述

  • 将每个窗口分区的行划分为"n"个存储桶
  • 范围从 1 到"n"。

percent_rank()

描述

计算一组值中某个值的百分比排名。

rank()

描述

  • 计算值组内某个值的排名。
  • 结果是 1 加上分区顺序中当前行之前或等于当前行的行数。
  • 这些值将在序列中产生间隙。

row_number()

描述

根据窗口分区中行的顺序,为每行分配一个唯一的连续编号,从 1 开始。

实践

本实践内容全部基于 Spark 3.2.0 版本的 spark-sql 命令行来操作的。

1. cume_dist() 和 row_number()

使用 Hive 的方式创建一个表:

CREATE TABLE scores (name STRING NOT NULL,score INT NOT NULL
) STORED AS ORC;

插入一些测试数据:

INSERT INTOscores(name, score)
VALUES('Smith',81),('Jones',55),('Williams',55),('Taylor',62),('Brown',62),('Davies',84),('Evans',87),('Wilson',72),('Thomas',72),('Johnson',100);

以下语句查找结果集中分数的累积分布:

SELECTname,score,ROW_NUMBER() OVER (ORDER BY score) row_num,CUME_DIST() OVER (ORDER BY score) cume_dist_val
FROMscores;

这是输出:

Jones    55      1       0.2
Williams 55      2       0.2
Taylor   62      3       0.4
Brown    62      4       0.4
Wilson   72      5       0.6
Thomas   72      6       0.6
Smith    81      7       0.7
Davies   84      8       0.8
Evans    87      9       0.9
Johnson  100     10      1.0

在此示例中,分数按从55到100的升序排序。请注意,ROW_NUMBER() 根据窗口分区中行的顺序,为每行分配一个唯一的连续编号,从 1 开始。

那么 CUME_DIST() 函数如何执行计算?

对于第一行,函数查找结果集中的行数,其值小于或等于55 的行数有 2 行。

然后,CUME_DIST()函数将 2 除以总行数 10:

2/10

结果是0.2。

2. dense_rank() 和 rank()

使用 Hive 的方式创建一个表:

CREATE TABLE rankDemo (val INT
) STORED AS ORC;

插入一些数据

INSERT INTO rankDemo(val)
VALUES(1),(2),(2),(3),(4),(4),(5);

看看插入后的查询结果

SELECT *
FROMrankDemo;
1
2
2
3
4
4
5

以下语句使用 DENSE_RANK() 函数为每行分配排名:

SELECTval,DENSE_RANK() OVER (ORDER BY val) my_rank
FROMrankDemo;
1       1
2       2
2       2
3       3
4       4
4       4
5       5

以下语句使用 RANK() 函数为每行分配排名:

SELECTval,RANK() OVER (ORDER BY val) my_rank
FROMrankDemo;
1       1
2       2
2       2
3       4
4       5
4       5
5       7

3. lag() 和 lead()

表和数据准备

使用 Hive 的方式创建2个表:

CREATE TABLE `orders` (`orderNumber` int NOT NULL,`orderDate` string NOT NULL,`requiredDate` string NOT NULL,`shippedDate` string,`status` varchar(15) NOT NULL,`comments` string,`customerNumber` int NOT NULL
) STORED AS ORC;CREATE TABLE `customers` (`customerNumber` int NOT NULL,`customerName` varchar(50) NOT NULL,`contactLastName` varchar(50) NOT NULL,`contactFirstName` varchar(50) NOT NULL,`phone` varchar(50) NOT NULL,`addressLine1` varchar(50) NOT NULL,`addressLine2` varchar(50),`city` varchar(50) NOT NULL,`state` varchar(50),`postalCode` varchar(15),`country` varchar(50) NOT NULL,`salesRepEmployeeNumber` int,`creditLimit` decimal(10,2)
) STORED AS ORC;

customers 表插入一些测试数据:

INSERT INTO `customers` VALUES (103,'Atelier graphique','Schmitt','Carine ','40.32.2555','54, rue Royale',NULL,'Nantes',NULL,'44000','France',1370,21000.00),(112,'Signal Gift Stores','King','Jean','7025551838','8489 Strong St.',NULL,'Las Vegas','NV','83030','USA',1166,71800.00),(114,'Australian Collectors, Co.','Ferguson','Peter','03 9520 4555','636 St Kilda Road','Level 3','Melbourne','Victoria','3004','Australia',1611,117300.00),(119,'La Rochelle Gifts','Labrune','Janine ','40.67.8555','67, rue des Cinquante Otages',NULL,'Nantes',NULL,'44000','France',1370,118200.00),(121,'Baane Mini Imports','Bergulfsen','Jonas ','07-98 9555','Erling Skakkes gate 78',NULL,'Stavern',NULL,'4110','Norway',1504,81700.00),(124,'Mini Gifts Distributors Ltd.','Nelson','Susan','4155551450','5677 Strong St.',NULL,'San Rafael','CA','97562','USA',1165,210500.00),(125,'Havel & Zbyszek Co','Piestrzeniewicz','Zbyszek ','(26) 642-7555','ul. Filtrowa 68',NULL,'Warszawa',NULL,'01-012','Poland',NULL,0.00),(128,'Blauer See Auto, Co.','Keitel','Roland','+49 69 66 90 2555','Lyonerstr. 34',NULL,'Frankfurt',NULL,'60528','Germany',1504,59700.00),(129,'Mini Wheels Co.','Murphy','Julie','6505555787','5557 North Pendale Street',NULL,'San Francisco','CA','94217','USA',1165,64600.00),(131,'Land of Toys Inc.','Lee','Kwai','2125557818','897 Long Airport Avenue',NULL,'NYC','NY','10022','USA',1323,114900.00),(141,'Euro+ Shopping Channel','Freyre','Diego ','(91) 555 94 44','C/ Moralzarzal, 86',NULL,'Madrid',NULL,'28034','Spain',1370,227600.00),(144,'Volvo Model Replicas, Co','Berglund','Christina ','0921-12 3555','Berguvsv gen  8',NULL,'Lule',NULL,'S-958 22','Sweden',1504,53100.00),(145,'Danish Wholesale Imports','Petersen','Jytte ','31 12 3555','Vinb ltet 34',NULL,'Kobenhavn',NULL,'1734','Denmark',1401,83400.00),(146,'Saveley & Henriot, Co.','Saveley','Mary ','78.32.5555','2, rue du Commerce',NULL,'Lyon',NULL,'69004','France',1337,123900.00),(148,'Dragon Souveniers, Ltd.','Natividad','Eric','+65 221 7555','Bronz Sok.','Bronz Apt. 3/6 Tesvikiye','Singapore',NULL,'079903','Singapore',1621,103800.00),(151,'Muscle Machine Inc','Young','Jeff','2125557413','4092 Furth Circle','Suite 400','NYC','NY','10022','USA',1286,138500.00),(157,'Diecast Classics Inc.','Leong','Kelvin','2155551555','7586 Pompton St.',NULL,'Allentown','PA','70267','USA',1216,100600.00),(161,'Technics Stores Inc.','Hashimoto','Juri','6505556809','9408 Furth Circle',NULL,'Burlingame','CA','94217','USA',1165,84600.00),(166,'Handji Gifts& Co','Victorino','Wendy','+65 224 1555','106 Linden Road Sandown','2nd Floor','Singapore',NULL,'069045','Singapore',1612,97900.00),(167,'Herkku Gifts','Oeztan','Veysel','+47 2267 3215','Brehmen St. 121','PR 334 Sentrum','Bergen',NULL,'N 5804','Norway  ',1504,96800.00),(168,'American Souvenirs Inc','Franco','Keith','2035557845','149 Spinnaker Dr.','Suite 101','New Haven','CT','97823','USA',1286,0.00),(169,'Porto Imports Co.','de Castro','Isabel ','(1) 356-5555','Estrada da sade n. 58',NULL,'Lisboa',NULL,'1756','Portugal',NULL,0.00),(171,'Daedalus Designs Imports','Ranc','Martine ','20.16.1555','184, chausse de Tournai',NULL,'Lille',NULL,'59000','France',1370,82900.00),(172,'La Corne D\'abondance, Co.','Bertrand','Marie','(1) 42.34.2555','265, boulevard Charonne',NULL,'Paris',NULL,'75012','France',1337,84300.00),(173,'Cambridge Collectables Co.','Tseng','Jerry','6175555555','4658 Baden Av.',NULL,'Cambridge','MA','51247','USA',1188,43400.00),(175,'Gift Depot Inc.','King','Julie','2035552570','25593 South Bay Ln.',NULL,'Bridgewater','CT','97562','USA',1323,84300.00),(177,'Osaka Souveniers Co.','Kentary','Mory','+81 06 6342 5555','1-6-20 Dojima',NULL,'Kita-ku','Osaka',' 530-0003','Japan',1621,81200.00),(181,'Vitachrome Inc.','Frick','Michael','2125551500','2678 Kingston Rd.','Suite 101','NYC','NY','10022','USA',1286,76400.00),(186,'Toys of Finland, Co.','Karttunen','Matti','90-224 8555','Keskuskatu 45',NULL,'Helsinki',NULL,'21240','Finland',1501,96500.00),(187,'AV Stores, Co.','Ashworth','Rachel','(171) 555-1555','Fauntleroy Circus',NULL,'Manchester',NULL,'EC2 5NT','UK',1501,136800.00),(189,'Clover Collections, Co.','Cassidy','Dean','+353 1862 1555','25 Maiden Lane','Floor No. 4','Dublin',NULL,'2','Ireland',1504,69400.00),(198,'Auto-Moto Classics Inc.','Taylor','Leslie','6175558428','16780 Pompton St.',NULL,'Brickhaven','MA','58339','USA',1216,23000.00),(201,'UK Collectables, Ltd.','Devon','Elizabeth','(171) 555-2282','12, Berkeley Gardens Blvd',NULL,'Liverpool',NULL,'WX1 6LT','UK',1501,92700.00),(202,'Canadian Gift Exchange Network','Tamuri','Yoshi ','(604) 555-3392','1900 Oak St.',NULL,'Vancouver','BC','V3F 2K1','Canada',1323,90300.00),(204,'Online Mini Collectables','Barajas','Miguel','6175557555','7635 Spinnaker Dr.',NULL,'Brickhaven','MA','58339','USA',1188,68700.00),(205,'Toys4GrownUps.com','Young','Julie','6265557265','78934 Hillside Dr.',NULL,'Pasadena','CA','90003','USA',1166,90700.00),(206,'Asian Shopping Network, Co','Walker','Brydey','+612 9411 1555','Suntec Tower Three','8 Temasek','Singapore',NULL,'038988','Singapore',NULL,0.00),(209,'Mini Caravy','Citeaux','Frdrique ','88.60.1555','24, place Klber',NULL,'Strasbourg',NULL,'67000','France',1370,53800.00),(211,'King Kong Collectables, Co.','Gao','Mike','+852 2251 1555','Bank of China Tower','1 Garden Road','Central Hong Kong',NULL,NULL,'Hong Kong',1621,58600.00),(216,'Enaco Distributors','Saavedra','Eduardo ','(93) 203 4555','Rambla de Catalua, 23',NULL,'Barcelona',NULL,'08022','Spain',1702,60300.00),(219,'Boards & Toys Co.','Young','Mary','3105552373','4097 Douglas Av.',NULL,'Glendale','CA','92561','USA',1166,11000.00),(223,'Natrlich Autos','Kloss','Horst ','0372-555188','Taucherstrae 10',NULL,'Cunewalde',NULL,'01307','Germany',NULL,0.00),(227,'Heintze Collectables','Ibsen','Palle','86 21 3555','Smagsloget 45',NULL,'Brhus',NULL,'8200','Denmark',1401,120800.00),(233,'Qubec Home Shopping Network','Fresnire','Jean ','(514) 555-8054','43 rue St. Laurent',NULL,'Montral','Qubec','H1J 1C3','Canada',1286,48700.00),(237,'ANG Resellers','Camino','Alejandra ','(91) 745 6555','Gran Va, 1',NULL,'Madrid',NULL,'28001','Spain',NULL,0.00),(239,'Collectable Mini Designs Co.','Thompson','Valarie','7605558146','361 Furth Circle',NULL,'San Diego','CA','91217','USA',1166,105000.00),(240,'giftsbymail.co.uk','Bennett','Helen ','(198) 555-8888','Garden House','Crowther Way 23','Cowes','Isle of Wight','PO31 7PJ','UK',1501,93900.00),(242,'Alpha Cognac','Roulet','Annette ','61.77.6555','1 rue Alsace-Lorraine',NULL,'Toulouse',NULL,'31000','France',1370,61100.00),(247,'Messner Shopping Network','Messner','Renate ','069-0555984','Magazinweg 7',NULL,'Frankfurt',NULL,'60528','Germany',NULL,0.00),(249,'Amica Models & Co.','Accorti','Paolo ','011-4988555','Via Monte Bianco 34',NULL,'Torino',NULL,'10100','Italy',1401,113000.00),(250,'Lyon Souveniers','Da Silva','Daniel','+33 1 46 62 7555','27 rue du Colonel Pierre Avia',NULL,'Paris',NULL,'75508','France',1337,68100.00),(256,'Auto Associs & Cie.','Tonini','Daniel ','30.59.8555','67, avenue de l\'Europe',NULL,'Versailles',NULL,'78000','France',1370,77900.00),(259,'Toms Spezialitten, Ltd','Pfalzheim','Henriette ','0221-5554327','Mehrheimerstr. 369',NULL,'Kln',NULL,'50739','Germany',1504,120400.00),(260,'Royal Canadian Collectables, Ltd.','Lincoln','Elizabeth ','(604) 555-4555','23 Tsawassen Blvd.',NULL,'Tsawassen','BC','T2F 8M4','Canada',1323,89600.00),(273,'Franken Gifts, Co','Franken','Peter ','089-0877555','Berliner Platz 43',NULL,'Mnchen',NULL,'80805','Germany',NULL,0.00),(276,'Anna\'s Decorations, Ltd','O\'Hara','Anna','02 9936 8555','201 Miller Street','Level 15','North Sydney','NSW','2060','Australia',1611,107800.00),(278,'Rovelli Gifts','Rovelli','Giovanni ','035-640555','Via Ludovico il Moro 22',NULL,'Bergamo',NULL,'24100','Italy',1401,119600.00),(282,'Souveniers And Things Co.','Huxley','Adrian','+61 2 9495 8555','Monitor Money Building','815 Pacific Hwy','Chatswood','NSW','2067','Australia',1611,93300.00),(286,'Marta\'s Replicas Co.','Hernandez','Marta','6175558555','39323 Spinnaker Dr.',NULL,'Cambridge','MA','51247','USA',1216,123700.00),(293,'BG&E Collectables','Harrison','Ed','+41 26 425 50 01','Rte des Arsenaux 41 ',NULL,'Fribourg',NULL,'1700','Switzerland',NULL,0.00),(298,'Vida Sport, Ltd','Holz','Mihael','0897-034555','Grenzacherweg 237',NULL,'Genve',NULL,'1203','Switzerland',1702,141300.00),(299,'Norway Gifts By Mail, Co.','Klaeboe','Jan','+47 2212 1555','Drammensveien 126A','PB 211 Sentrum','Oslo',NULL,'N 0106','Norway  ',1504,95100.00),(303,'Schuyler Imports','Schuyler','Bradley','+31 20 491 9555','Kingsfordweg 151',NULL,'Amsterdam',NULL,'1043 GR','Netherlands',NULL,0.00),(307,'Der Hund Imports','Andersen','Mel','030-0074555','Obere Str. 57',NULL,'Berlin',NULL,'12209','Germany',NULL,0.00),(311,'Oulu Toy Supplies, Inc.','Koskitalo','Pirkko','981-443655','Torikatu 38',NULL,'Oulu',NULL,'90110','Finland',1501,90500.00),(314,'Petit Auto','Dewey','Catherine ','(02) 5554 67','Rue Joseph-Bens 532',NULL,'Bruxelles',NULL,'B-1180','Belgium',1401,79900.00),(319,'Mini Classics','Frick','Steve','9145554562','3758 North Pendale Street',NULL,'White Plains','NY','24067','USA',1323,102700.00),(320,'Mini Creations Ltd.','Huang','Wing','5085559555','4575 Hillside Dr.',NULL,'New Bedford','MA','50553','USA',1188,94500.00),(321,'Corporate Gift Ideas Co.','Brown','Julie','6505551386','7734 Strong St.',NULL,'San Francisco','CA','94217','USA',1165,105000.00),(323,'Down Under Souveniers, Inc','Graham','Mike','+64 9 312 5555','162-164 Grafton Road','Level 2','Auckland  ',NULL,NULL,'New Zealand',1612,88000.00),(324,'Stylish Desk Decors, Co.','Brown','Ann ','(171) 555-0297','35 King George',NULL,'London',NULL,'WX3 6FW','UK',1501,77000.00),(328,'Tekni Collectables Inc.','Brown','William','2015559350','7476 Moss Rd.',NULL,'Newark','NJ','94019','USA',1323,43000.00),(333,'Australian Gift Network, Co','Calaghan','Ben','61-7-3844-6555','31 Duncan St. West End',NULL,'South Brisbane','Queensland','4101','Australia',1611,51600.00),(334,'Suominen Souveniers','Suominen','Kalle','+358 9 8045 555','Software Engineering Center','SEC Oy','Espoo',NULL,'FIN-02271','Finland',1501,98800.00),(335,'Cramer Spezialitten, Ltd','Cramer','Philip ','0555-09555','Maubelstr. 90',NULL,'Brandenburg',NULL,'14776','Germany',NULL,0.00),(339,'Classic Gift Ideas, Inc','Cervantes','Francisca','2155554695','782 First Street',NULL,'Philadelphia','PA','71270','USA',1188,81100.00),(344,'CAF Imports','Fernandez','Jesus','+34 913 728 555','Merchants House','27-30 Merchant\'s Quay','Madrid',NULL,'28023','Spain',1702,59600.00),(347,'Men \'R\' US Retailers, Ltd.','Chandler','Brian','2155554369','6047 Douglas Av.',NULL,'Los Angeles','CA','91003','USA',1166,57700.00),(348,'Asian Treasures, Inc.','McKenna','Patricia ','2967 555','8 Johnstown Road',NULL,'Cork','Co. Cork',NULL,'Ireland',NULL,0.00),(350,'Marseille Mini Autos','Lebihan','Laurence ','91.24.4555','12, rue des Bouchers',NULL,'Marseille',NULL,'13008','France',1337,65000.00),(353,'Reims Collectables','Henriot','Paul ','26.47.1555','59 rue de l\'Abbaye',NULL,'Reims',NULL,'51100','France',1337,81100.00),(356,'SAR Distributors, Co','Kuger','Armand','+27 21 550 3555','1250 Pretorius Street',NULL,'Hatfield','Pretoria','0028','South Africa',NULL,0.00),(357,'GiftsForHim.com','MacKinlay','Wales','64-9-3763555','199 Great North Road',NULL,'Auckland',NULL,NULL,'New Zealand',1612,77700.00),(361,'Kommission Auto','Josephs','Karin','0251-555259','Luisenstr. 48',NULL,'Minster',NULL,'44087','Germany',NULL,0.00),(362,'Gifts4AllAges.com','Yoshido','Juri','6175559555','8616 Spinnaker Dr.',NULL,'Boston','MA','51003','USA',1216,41900.00),(363,'Online Diecast Creations Co.','Young','Dorothy','6035558647','2304 Long Airport Avenue',NULL,'Nashua','NH','62005','USA',1216,114200.00),(369,'Lisboa Souveniers, Inc','Rodriguez','Lino ','(1) 354-2555','Jardim das rosas n. 32',NULL,'Lisboa',NULL,'1675','Portugal',NULL,0.00),(376,'Precious Collectables','Urs','Braun','0452-076555','Hauptstr. 29',NULL,'Bern',NULL,'3012','Switzerland',1702,0.00),(379,'Collectables For Less Inc.','Nelson','Allen','6175558555','7825 Douglas Av.',NULL,'Brickhaven','MA','58339','USA',1188,70700.00),(381,'Royale Belge','Cartrain','Pascale ','(071) 23 67 2555','Boulevard Tirou, 255',NULL,'Charleroi',NULL,'B-6000','Belgium',1401,23500.00),(382,'Salzburg Collectables','Pipps','Georg ','6562-9555','Geislweg 14',NULL,'Salzburg',NULL,'5020','Austria',1401,71700.00),(385,'Cruz & Sons Co.','Cruz','Arnold','+63 2 555 3587','15 McCallum Street','NatWest Center #13-03','Makati City',NULL,'1227 MM','Philippines',1621,81500.00),(386,'L\'ordine Souveniers','Moroni','Maurizio ','0522-556555','Strada Provinciale 124',NULL,'Reggio Emilia',NULL,'42100','Italy',1401,121400.00),(398,'Tokyo Collectables, Ltd','Shimamura','Akiko','+81 3 3584 0555','2-2-8 Roppongi',NULL,'Minato-ku','Tokyo','106-0032','Japan',1621,94400.00),(406,'Auto Canal+ Petit','Perrier','Dominique','(1) 47.55.6555','25, rue Lauriston',NULL,'Paris',NULL,'75016','France',1337,95000.00),(409,'Stuttgart Collectable Exchange','Mller','Rita ','0711-555361','Adenauerallee 900',NULL,'Stuttgart',NULL,'70563','Germany',NULL,0.00),(412,'Extreme Desk Decorations, Ltd','McRoy','Sarah','04 499 9555','101 Lambton Quay','Level 11','Wellington',NULL,NULL,'New Zealand',1612,86800.00),(415,'Bavarian Collectables Imports, Co.','Donnermeyer','Michael',' +49 89 61 08 9555','Hansastr. 15',NULL,'Munich',NULL,'80686','Germany',1504,77000.00),(424,'Classic Legends Inc.','Hernandez','Maria','2125558493','5905 Pompton St.','Suite 750','NYC','NY','10022','USA',1286,67500.00),(443,'Feuer Online Stores, Inc','Feuer','Alexander ','0342-555176','Heerstr. 22',NULL,'Leipzig',NULL,'04179','Germany',NULL,0.00),(447,'Gift Ideas Corp.','Lewis','Dan','2035554407','2440 Pompton St.',NULL,'Glendale','CT','97561','USA',1323,49700.00),(448,'Scandinavian Gift Ideas','Larsson','Martha','0695-34 6555','kergatan 24',NULL,'Brcke',NULL,'S-844 67','Sweden',1504,116400.00),(450,'The Sharp Gifts Warehouse','Frick','Sue','4085553659','3086 Ingle Ln.',NULL,'San Jose','CA','94217','USA',1165,77600.00),(452,'Mini Auto Werke','Mendel','Roland ','7675-3555','Kirchgasse 6',NULL,'Graz',NULL,'8010','Austria',1401,45300.00),(455,'Super Scale Inc.','Murphy','Leslie','2035559545','567 North Pendale Street',NULL,'New Haven','CT','97823','USA',1286,95400.00),(456,'Microscale Inc.','Choi','Yu','2125551957','5290 North Pendale Street','Suite 200','NYC','NY','10022','USA',1286,39800.00),(458,'Corrida Auto Replicas, Ltd','Sommer','Martn ','(91) 555 22 82','C/ Araquil, 67',NULL,'Madrid',NULL,'28023','Spain',1702,104600.00),(459,'Warburg Exchange','Ottlieb','Sven ','0241-039123','Walserweg 21',NULL,'Aachen',NULL,'52066','Germany',NULL,0.00),(462,'FunGiftIdeas.com','Benitez','Violeta','5085552555','1785 First Street',NULL,'New Bedford','MA','50553','USA',1216,85800.00),(465,'Anton Designs, Ltd.','Anton','Carmen','+34 913 728555','c/ Gobelas, 19-1 Urb. La Florida',NULL,'Madrid',NULL,'28023','Spain',NULL,0.00),(471,'Australian Collectables, Ltd','Clenahan','Sean','61-9-3844-6555','7 Allen Street',NULL,'Glen Waverly','Victoria','3150','Australia',1611,60300.00),(473,'Frau da Collezione','Ricotti','Franco','+39 022515555','20093 Cologno Monzese','Alessandro Volta 16','Milan',NULL,NULL,'Italy',1401,34800.00),(475,'West Coast Collectables Co.','Thompson','Steve','3105553722','3675 Furth Circle',NULL,'Burbank','CA','94019','USA',1166,55400.00),(477,'Mit Vergngen & Co.','Moos','Hanna ','0621-08555','Forsterstr. 57',NULL,'Mannheim',NULL,'68306','Germany',NULL,0.00),(480,'Kremlin Collectables, Co.','Semenov','Alexander ','+7 812 293 0521','2 Pobedy Square',NULL,'Saint Petersburg',NULL,'196143','Russia',NULL,0.00),(481,'Raanan Stores, Inc','Altagar,G M','Raanan','+ 972 9 959 8555','3 Hagalim Blv.',NULL,'Herzlia',NULL,'47625','Israel',NULL,0.00),(484,'Iberia Gift Imports, Corp.','Roel','JosPedro ','(95) 555 82 82','C/ Romero, 33',NULL,'Sevilla',NULL,'41101','Spain',1702,65700.00),(486,'Motor Mint Distributors Inc.','Salazar','Rosa','2155559857','11328 Douglas Av.',NULL,'Philadelphia','PA','71270','USA',1323,72600.00),(487,'Signal Collectibles Ltd.','Taylor','Sue','4155554312','2793 Furth Circle',NULL,'Brisbane','CA','94217','USA',1165,60300.00),(489,'Double Decker Gift Stores, Ltd','Smith','Thomas ','(171) 555-7555','120 Hanover Sq.',NULL,'London',NULL,'WA1 1DP','UK',1501,43300.00),(495,'Diecast Collectables','Franco','Valarie','6175552555','6251 Ingle Ln.',NULL,'Boston','MA','51003','USA',1188,85100.00),(496,'Kelly\'s Gift Shop','Snowden','Tony','+64 9 5555500','Arenales 1938 3\'A\'',NULL,'Auckland  ',NULL,NULL,'New Zealand',1612,110000.00);

orders 表插入一些测试数据:

INSERT INTO `orders` VALUES (10100,'2013-01-06','2013-01-13','2013-01-10','Shipped',NULL,363),(10101,'2013-01-09','2013-01-18','2013-01-11','Shipped','Check on availability.',128),(10102,'2013-01-10','2013-01-18','2013-01-14','Shipped',NULL,181),(10103,'2013-01-29','2013-02-07','2013-02-02','Shipped',NULL,121),(10104,'2013-01-31','2013-02-09','2013-02-01','Shipped',NULL,141),(10105,'2013-02-11','2013-02-21','2013-02-12','Shipped',NULL,145),(10106,'2013-02-17','2013-02-24','2013-02-21','Shipped',NULL,278),(10107,'2013-02-24','2013-03-03','2013-02-26','Shipped','Difficult to negotiate with customer. We need more marketing materials',131),(10108,'2013-03-03','2013-03-12','2013-03-08','Shipped',NULL,385),(10109,'2013-03-10','2013-03-19','2013-03-11','Shipped','Customer requested that FedEx Ground is used for this shipping',486),(10110,'2013-03-18','2013-03-24','2013-03-20','Shipped',NULL,187),(10111,'2013-03-25','2013-03-31','2013-03-30','Shipped',NULL,129),(10112,'2013-03-24','2013-04-03','2013-03-29','Shipped','Customer requested that ad materials (such as posters, pamphlets) be included in the shippment',144),(10113,'2013-03-26','2013-04-02','2013-03-27','Shipped',NULL,124),(10114,'2013-04-01','2013-04-07','2013-04-02','Shipped',NULL,172),(10115,'2013-04-04','2013-04-12','2013-04-07','Shipped',NULL,424),(10116,'2013-04-11','2013-04-19','2013-04-13','Shipped',NULL,381),(10117,'2013-04-16','2013-04-24','2013-04-17','Shipped',NULL,148),(10118,'2013-04-21','2013-04-29','2013-04-26','Shipped','Customer has worked with some of our vendors in the past and is aware of their MSRP',216),(10119,'2013-04-28','2013-05-05','2013-05-02','Shipped',NULL,382),(10120,'2013-04-29','2013-05-08','2013-05-01','Shipped',NULL,114),(10121,'2013-05-07','2013-05-13','2013-05-13','Shipped',NULL,353),(10122,'2013-05-08','2013-05-16','2013-05-13','Shipped',NULL,350),(10123,'2013-05-20','2013-05-29','2013-05-22','Shipped',NULL,103),(10124,'2013-05-21','2013-05-29','2013-05-25','Shipped','Customer very concerned about the exact color of the models. There is high risk that he may dispute the order because there is a slight color mismatch',112),(10125,'2013-05-21','2013-05-27','2013-05-24','Shipped',NULL,114),(10126,'2013-05-28','2013-06-07','2013-06-02','Shipped',NULL,458),(10127,'2013-06-03','2013-06-09','2013-06-06','Shipped','Customer requested special shippment. The instructions were passed along to the warehouse',151),(10128,'2013-06-06','2013-06-12','2013-06-11','Shipped',NULL,141),(10129,'2013-06-12','2013-06-18','2013-06-14','Shipped',NULL,324),(10130,'2013-06-16','2013-06-24','2013-06-21','Shipped',NULL,198),(10131,'2013-06-16','2013-06-25','2013-06-21','Shipped',NULL,447),(10132,'2013-06-25','2013-07-01','2013-06-28','Shipped',NULL,323),(10133,'2013-06-27','2013-07-04','2013-07-03','Shipped',NULL,141),(10134,'2013-07-01','2013-07-10','2013-07-05','Shipped',NULL,250),(10135,'2013-07-02','2013-07-12','2013-07-03','Shipped',NULL,124),(10136,'2013-07-04','2013-07-14','2013-07-06','Shipped','Customer is interested in buying more Ferrari models',242),(10137,'2013-07-10','2013-07-20','2013-07-14','Shipped',NULL,353),(10138,'2013-07-07','2013-07-16','2013-07-13','Shipped',NULL,496),(10139,'2013-07-16','2013-07-23','2013-07-21','Shipped',NULL,282),(10140,'2013-07-24','2013-08-02','2013-07-30','Shipped',NULL,161),(10141,'2013-08-01','2013-08-09','2013-08-04','Shipped',NULL,334),(10142,'2013-08-08','2013-08-16','2013-08-13','Shipped',NULL,124),(10143,'2013-08-10','2013-08-18','2013-08-12','Shipped','Can we deliver the new Ford Mustang models by end-of-quarter?',320),(10144,'2013-08-13','2013-08-21','2013-08-14','Shipped',NULL,381),(10145,'2013-08-25','2013-09-02','2013-08-31','Shipped',NULL,205),(10146,'2013-09-03','2013-09-13','2013-09-06','Shipped',NULL,447),(10147,'2013-09-05','2013-09-12','2013-09-09','Shipped',NULL,379),(10148,'2013-09-11','2013-09-21','2013-09-15','Shipped','They want to reevaluate their terms agreement with Finance.',276),(10149,'2013-09-12','2013-09-18','2013-09-17','Shipped',NULL,487),(10150,'2013-09-19','2013-09-27','2013-09-21','Shipped','They want to reevaluate their terms agreement with Finance.',148),(10151,'2013-09-21','2013-09-30','2013-09-24','Shipped',NULL,311),(10152,'2013-09-25','2013-10-03','2013-10-01','Shipped',NULL,333),(10153,'2013-09-28','2013-10-05','2013-10-03','Shipped',NULL,141),(10154,'2013-10-02','2013-10-12','2013-10-08','Shipped',NULL,219),(10155,'2013-10-06','2013-10-13','2013-10-07','Shipped',NULL,186),(10156,'2013-10-08','2013-10-17','2013-10-11','Shipped',NULL,141),(10157,'2013-10-09','2013-10-15','2013-10-14','Shipped',NULL,473),(10158,'2013-10-10','2013-10-18','2013-10-15','Shipped',NULL,121),(10159,'2013-10-10','2013-10-19','2013-10-16','Shipped',NULL,321),(10160,'2013-10-11','2013-10-17','2013-10-17','Shipped',NULL,347),(10161,'2013-10-17','2013-10-25','2013-10-20','Shipped',NULL,227),(10162,'2013-10-18','2013-10-26','2013-10-19','Shipped',NULL,321),(10163,'2013-10-20','2013-10-27','2013-10-24','Shipped',NULL,424),(10164,'2013-10-21','2013-10-30','2013-10-23','Resolved','This order was disputed, but resolved on 11/1/2003; Customer doesn\'t like the colors and precision of the models.',452),(10165,'2013-10-22','2013-10-31','2013-12-26','Shipped','This order was on hold because customers\'s credit limit had been exceeded. Order will ship when payment is received',148),(10166,'2013-10-21','2013-10-30','2013-10-27','Shipped',NULL,462),(10167,'2013-10-23','2013-10-30',NULL,'Cancelled','Customer called to cancel. The warehouse was notified in time and the order didn\'t ship. They have a new VP of Sales and are shifting their sales model. Our VP of Sales should contact them.',448),(10168,'2013-10-28','2013-11-03','2013-11-01','Shipped',NULL,161),(10169,'2013-11-04','2013-11-14','2013-11-09','Shipped',NULL,276),(10170,'2013-11-04','2013-11-12','2013-11-07','Shipped',NULL,452),(10171,'2013-11-05','2013-11-13','2013-11-07','Shipped',NULL,233),(10172,'2013-11-05','2013-11-14','2013-11-11','Shipped',NULL,175),(10173,'2013-11-05','2013-11-15','2013-11-09','Shipped','Cautious optimism. We have happy customers here, if we can keep them well stocked.  I need all the information I can get on the planned shippments of Porches',278),(10174,'2013-11-06','2013-11-15','2013-11-10','Shipped',NULL,333),(10175,'2013-11-06','2013-11-14','2013-11-09','Shipped',NULL,324),(10176,'2013-11-06','2013-11-15','2013-11-12','Shipped',NULL,386),(10177,'2013-11-07','2013-11-17','2013-11-12','Shipped',NULL,344),(10178,'2013-11-08','2013-11-16','2013-11-10','Shipped','Custom shipping instructions sent to warehouse',242),(10179,'2013-11-11','2013-11-17','2013-11-13','Cancelled','Customer cancelled due to urgent budgeting issues. Must be cautious when dealing with them in the future. Since order shipped already we must discuss who would cover the shipping charges.',496),(10180,'2013-11-11','2013-11-19','2013-11-14','Shipped',NULL,171),(10181,'2013-11-12','2013-11-19','2013-11-15','Shipped',NULL,167),(10182,'2013-11-12','2013-11-21','2013-11-18','Shipped',NULL,124),(10183,'2013-11-13','2013-11-22','2013-11-15','Shipped','We need to keep in close contact with their Marketing VP. He is the decision maker for all their purchases.',339),(10184,'2013-11-14','2013-11-22','2013-11-20','Shipped',NULL,484),(10185,'2013-11-14','2013-11-21','2013-11-20','Shipped',NULL,320),(10186,'2013-11-14','2013-11-20','2013-11-18','Shipped','They want to reevaluate their terms agreement with the VP of Sales',489),(10187,'2013-11-15','2013-11-24','2013-11-16','Shipped',NULL,211),(10188,'2013-11-18','2013-11-26','2013-11-24','Shipped',NULL,167),(10189,'2013-11-18','2013-11-25','2013-11-24','Shipped','They want to reevaluate their terms agreement with Finance.',205),(10190,'2013-11-19','2013-11-29','2013-11-20','Shipped',NULL,141),(10191,'2013-11-20','2013-11-30','2013-11-24','Shipped','We must be cautions with this customer. Their VP of Sales resigned. Company may be heading down.',259),(10192,'2013-11-20','2013-11-29','2013-11-25','Shipped',NULL,363),(10193,'2013-11-21','2013-11-28','2013-11-27','Shipped',NULL,471),(10194,'2013-11-25','2013-12-02','2013-11-26','Shipped',NULL,146),(10195,'2013-11-25','2013-12-01','2013-11-28','Shipped',NULL,319),(10196,'2013-11-26','2013-12-03','2013-12-01','Shipped',NULL,455),(10197,'2013-11-26','2013-12-02','2013-12-01','Shipped','Customer inquired about remote controlled models and gold models.',216),(10198,'2013-11-27','2013-12-06','2013-12-03','Shipped',NULL,385),(10199,'2013-12-01','2013-12-10','2013-12-06','Shipped',NULL,475),(10200,'2013-12-01','2013-12-09','2013-12-06','Shipped',NULL,211),(10201,'2013-12-01','2013-12-11','2013-12-02','Shipped',NULL,129),(10202,'2013-12-02','2013-12-09','2013-12-06','Shipped',NULL,357),(10203,'2013-12-02','2013-12-11','2013-12-07','Shipped',NULL,141),(10204,'2013-12-02','2013-12-10','2013-12-04','Shipped',NULL,151),(10205,'2013-12-03','2013-12-09','2013-12-07','Shipped',' I need all the information I can get on our competitors.',141),(10206,'2013-12-05','2013-12-13','2013-12-08','Shipped','Can we renegotiate this one?',202),(10207,'2013-12-09','2013-12-17','2013-12-11','Shipped','Check on availability.',495),(10208,'2014-01-02','2014-01-11','2014-01-04','Shipped',NULL,146),(10209,'2014-01-09','2014-01-15','2014-01-12','Shipped',NULL,347),(10210,'2014-01-12','2014-01-22','2014-01-20','Shipped',NULL,177),(10211,'2014-01-15','2014-01-25','2014-01-18','Shipped',NULL,406),(10212,'2014-01-16','2014-01-24','2014-01-18','Shipped',NULL,141),(10213,'2014-01-22','2014-01-28','2014-01-27','Shipped','Difficult to negotiate with customer. We need more marketing materials',489),(10214,'2014-01-26','2014-02-04','2014-01-29','Shipped',NULL,458),(10215,'2014-01-29','2014-02-08','2014-02-01','Shipped','Customer requested that FedEx Ground is used for this shipping',475),(10216,'2014-02-02','2014-02-10','2014-02-04','Shipped',NULL,256),(10217,'2014-02-04','2014-02-14','2014-02-06','Shipped',NULL,166),(10218,'2014-02-09','2014-02-16','2014-02-11','Shipped','Customer requested that ad materials (such as posters, pamphlets) be included in the shippment',473),(10219,'2014-02-10','2014-02-17','2014-02-12','Shipped',NULL,487),(10220,'2014-02-12','2014-02-19','2014-02-16','Shipped',NULL,189),(10221,'2014-02-18','2014-02-26','2014-02-19','Shipped',NULL,314),(10222,'2014-02-19','2014-02-27','2014-02-20','Shipped',NULL,239),(10223,'2014-02-20','2014-02-28','2014-02-24','Shipped',NULL,114),(10224,'2014-02-21','2014-03-02','2014-02-26','Shipped','Customer has worked with some of our vendors in the past and is aware of their MSRP',171),(10225,'2014-02-22','2014-03-01','2014-02-24','Shipped',NULL,298),(10226,'2014-02-26','2014-03-06','2014-03-02','Shipped',NULL,239),(10227,'2014-03-02','2014-03-12','2014-03-08','Shipped',NULL,146),(10228,'2014-03-10','2014-03-18','2014-03-13','Shipped',NULL,173),(10229,'2014-03-11','2014-03-20','2014-03-12','Shipped',NULL,124),(10230,'2014-03-15','2014-03-24','2014-03-20','Shipped','Customer very concerned about the exact color of the models. There is high risk that he may dispute the order because there is a slight color mismatch',128),(10231,'2014-03-19','2014-03-26','2014-03-25','Shipped',NULL,344),(10232,'2014-03-20','2014-03-30','2014-03-25','Shipped',NULL,240),(10233,'2014-03-29','2014-04-04','2014-04-02','Shipped','Customer requested special shippment. The instructions were passed along to the warehouse',328),(10234,'2014-03-30','2014-04-05','2014-04-02','Shipped',NULL,412),(10235,'2014-04-02','2014-04-12','2014-04-06','Shipped',NULL,260),(10236,'2014-04-03','2014-04-11','2014-04-08','Shipped',NULL,486),(10237,'2014-04-05','2014-04-12','2014-04-10','Shipped',NULL,181),(10238,'2014-04-09','2014-04-16','2014-04-10','Shipped',NULL,145),(10239,'2014-04-12','2014-04-21','2014-04-17','Shipped',NULL,311),(10240,'2014-04-13','2014-04-20','2014-04-20','Shipped',NULL,177),(10241,'2014-04-13','2014-04-20','2014-04-19','Shipped',NULL,209),(10242,'2014-04-20','2014-04-28','2014-04-25','Shipped','Customer is interested in buying more Ferrari models',456),(10243,'2014-04-26','2014-05-03','2014-04-28','Shipped',NULL,495),(10244,'2014-04-29','2014-05-09','2014-05-04','Shipped',NULL,141),(10245,'2014-05-04','2014-05-12','2014-05-09','Shipped',NULL,455),(10246,'2014-05-05','2014-05-13','2014-05-06','Shipped',NULL,141),(10247,'2014-05-05','2014-05-11','2014-05-08','Shipped',NULL,334),(10248,'2014-05-07','2014-05-14',NULL,'Cancelled','Order was mistakenly placed. The warehouse noticed the lack of documentation.',131),(10249,'2014-05-08','2014-05-17','2014-05-11','Shipped','Can we deliver the new Ford Mustang models by end-of-quarter?',173),(10250,'2014-05-11','2014-05-19','2014-05-15','Shipped',NULL,450),(10251,'2014-05-18','2014-05-24','2014-05-24','Shipped',NULL,328),(10252,'2014-05-26','2014-06-04','2014-05-29','Shipped',NULL,406),(10253,'2014-06-01','2014-06-09','2014-06-02','Cancelled','Customer disputed the order and we agreed to cancel it. We must be more cautions with this customer going forward, since they are very hard to please. We must cover the shipping fees.',201),(10254,'2014-06-03','2014-06-13','2014-06-04','Shipped','Customer requested that DHL is used for this shipping',323),(10255,'2014-06-04','2014-06-12','2014-06-09','Shipped',NULL,209),(10256,'2014-06-08','2014-06-16','2014-06-10','Shipped',NULL,145),(10257,'2014-06-14','2014-06-24','2014-06-15','Shipped',NULL,450),(10258,'2014-06-15','2014-06-25','2014-06-23','Shipped',NULL,398),(10259,'2014-06-15','2014-06-22','2014-06-17','Shipped',NULL,166),(10260,'2014-06-16','2014-06-22',NULL,'Cancelled','Customer heard complaints from their customers and called to cancel this order. Will notify the Sales Manager.',357),(10261,'2014-06-17','2014-06-25','2014-06-22','Shipped',NULL,233),(10262,'2014-06-24','2014-07-01',NULL,'Cancelled','This customer found a better offer from one of our competitors. Will call back to renegotiate.',141),(10263,'2014-06-28','2014-07-04','2014-07-02','Shipped',NULL,175),(10264,'2014-06-30','2014-07-06','2014-07-01','Shipped','Customer will send a truck to our local warehouse on 7/1/2004',362),(10265,'2014-07-02','2014-07-09','2014-07-07','Shipped',NULL,471),(10266,'2014-07-06','2014-07-14','2014-07-10','Shipped',NULL,386),(10267,'2014-07-07','2014-07-17','2014-07-09','Shipped',NULL,151),(10268,'2014-07-12','2014-07-18','2014-07-14','Shipped',NULL,412),(10269,'2014-07-16','2014-07-22','2014-07-18','Shipped',NULL,382),(10270,'2014-07-19','2014-07-27','2014-07-24','Shipped','Can we renegotiate this one?',282),(10271,'2014-07-20','2014-07-29','2014-07-23','Shipped',NULL,124),(10272,'2014-07-20','2014-07-26','2014-07-22','Shipped',NULL,157),(10273,'2014-07-21','2014-07-28','2014-07-22','Shipped',NULL,314),(10274,'2014-07-21','2014-07-29','2014-07-22','Shipped',NULL,379),(10275,'2014-07-23','2014-08-02','2014-07-29','Shipped',NULL,119),(10276,'2014-08-02','2014-08-11','2014-08-08','Shipped',NULL,204),(10277,'2014-08-04','2014-08-12','2014-08-05','Shipped',NULL,148),(10278,'2014-08-06','2014-08-16','2014-08-09','Shipped',NULL,112),(10279,'2014-08-09','2014-08-19','2014-08-15','Shipped','Cautious optimism. We have happy customers here, if we can keep them well stocked.  I need all the information I can get on the planned shippments of Porches',141),(10280,'2014-08-17','2014-08-27','2014-08-19','Shipped',NULL,249),(10281,'2014-08-19','2014-08-28','2014-08-23','Shipped',NULL,157),(10282,'2014-08-20','2014-08-26','2014-08-22','Shipped',NULL,124),(10283,'2014-08-20','2014-08-30','2014-08-23','Shipped',NULL,260),(10284,'2014-08-21','2014-08-29','2014-08-26','Shipped','Custom shipping instructions sent to warehouse',299),(10285,'2014-08-27','2014-09-04','2014-08-31','Shipped',NULL,286),(10286,'2014-08-28','2014-09-06','2014-09-01','Shipped',NULL,172),(10287,'2014-08-30','2014-09-06','2014-09-01','Shipped',NULL,298),(10288,'2014-09-01','2014-09-11','2014-09-05','Shipped',NULL,166),(10289,'2014-09-03','2014-09-13','2014-09-04','Shipped','We need to keep in close contact with their Marketing VP. He is the decision maker for all their purchases.',167),(10290,'2014-09-07','2014-09-15','2014-09-13','Shipped',NULL,198),(10291,'2014-09-08','2014-09-17','2014-09-14','Shipped',NULL,448),(10292,'2014-09-08','2014-09-18','2014-09-11','Shipped','They want to reevaluate their terms agreement with Finance.',131),(10293,'2014-09-09','2014-09-18','2014-09-14','Shipped',NULL,249),(10294,'2014-09-10','2014-09-17','2014-09-14','Shipped',NULL,204),(10295,'2014-09-10','2014-09-17','2014-09-14','Shipped','They want to reevaluate their terms agreement with Finance.',362),(10296,'2014-09-15','2014-09-22','2014-09-16','Shipped',NULL,415),(10297,'2014-09-16','2014-09-22','2014-09-21','Shipped','We must be cautions with this customer. Their VP of Sales resigned. Company may be heading down.',189),(10298,'2014-09-27','2014-10-05','2014-10-01','Shipped',NULL,103),(10299,'2014-09-30','2014-10-10','2014-10-01','Shipped',NULL,186),(10300,'2013-10-04','2013-10-13','2013-10-09','Shipped',NULL,128),(10301,'2013-10-05','2013-10-15','2013-10-08','Shipped',NULL,299),(10302,'2013-10-06','2013-10-16','2013-10-07','Shipped',NULL,201),(10303,'2014-10-06','2014-10-14','2014-10-09','Shipped','Customer inquired about remote controlled models and gold models.',484),(10304,'2014-10-11','2014-10-20','2014-10-17','Shipped',NULL,256),(10305,'2014-10-13','2014-10-22','2014-10-15','Shipped','Check on availability.',286),(10306,'2014-10-14','2014-10-21','2014-10-17','Shipped',NULL,187),(10307,'2014-10-14','2014-10-23','2014-10-20','Shipped',NULL,339),(10308,'2014-10-15','2014-10-24','2014-10-20','Shipped','Customer requested that FedEx Ground is used for this shipping',319),(10309,'2014-10-15','2014-10-24','2014-10-18','Shipped',NULL,121),(10310,'2014-10-16','2014-10-24','2014-10-18','Shipped',NULL,259),(10311,'2014-10-16','2014-10-23','2014-10-20','Shipped','Difficult to negotiate with customer. We need more marketing materials',141),(10312,'2014-10-21','2014-10-27','2014-10-23','Shipped',NULL,124),(10313,'2014-10-22','2014-10-28','2014-10-25','Shipped','Customer requested that FedEx Ground is used for this shipping',202),(10314,'2014-10-22','2014-11-01','2014-10-23','Shipped',NULL,227),(10315,'2014-10-29','2014-11-08','2014-10-30','Shipped',NULL,119),(10316,'2014-11-01','2014-11-09','2014-11-07','Shipped','Customer requested that ad materials (such as posters, pamphlets) be included in the shippment',240),(10317,'2014-11-02','2014-11-12','2014-11-08','Shipped',NULL,161),(10318,'2014-11-02','2014-11-09','2014-11-07','Shipped',NULL,157),(10319,'2014-11-03','2014-11-11','2014-11-06','Shipped','Customer requested that DHL is used for this shipping',456),(10320,'2014-11-03','2014-11-13','2014-11-07','Shipped',NULL,144),(10321,'2014-11-04','2014-11-12','2014-11-07','Shipped',NULL,462),(10322,'2014-11-04','2014-11-12','2014-11-10','Shipped','Customer has worked with some of our vendors in the past and is aware of their MSRP',363),(10323,'2014-11-05','2014-11-12','2014-11-09','Shipped',NULL,128),(10324,'2014-11-05','2014-11-11','2014-11-08','Shipped',NULL,181),(10325,'2014-11-05','2014-11-13','2014-11-08','Shipped',NULL,121),(10326,'2014-11-09','2014-11-16','2014-11-10','Shipped',NULL,144),(10327,'2014-11-10','2014-11-19','2014-11-13','Resolved','Order was disputed and resolved on 12/1/04. The Sales Manager was involved. Customer claims the scales of the models don\'t match what was discussed.',145),(10328,'2014-11-12','2014-11-21','2014-11-18','Shipped','Customer very concerned about the exact color of the models. There is high risk that he may dispute the order because there is a slight color mismatch',278),(10329,'2014-11-15','2014-11-24','2014-11-16','Shipped',NULL,131),(10330,'2014-11-16','2014-11-25','2014-11-21','Shipped',NULL,385),(10331,'2014-11-17','2014-11-23','2014-11-23','Shipped','Customer requested special shippment. The instructions were passed along to the warehouse',486),(10332,'2014-11-17','2014-11-25','2014-11-18','Shipped',NULL,187),(10333,'2014-11-18','2014-11-27','2014-11-20','Shipped',NULL,129),(10334,'2014-11-19','2014-11-28',NULL,'On Hold','The outstaniding balance for this customer exceeds their credit limit. Order will be shipped when a payment is received.',144),(10335,'2014-11-19','2014-11-29','2014-11-23','Shipped',NULL,124),(10336,'2014-11-20','2014-11-26','2014-11-24','Shipped','Customer requested that DHL is used for this shipping',172),(10337,'2014-11-21','2014-11-30','2014-11-26','Shipped',NULL,424),(10338,'2014-11-22','2014-12-02','2014-11-27','Shipped',NULL,381),(10339,'2014-11-23','2014-11-30','2014-11-30','Shipped',NULL,398),(10340,'2014-11-24','2014-12-01','2014-11-25','Shipped','Customer is interested in buying more Ferrari models',216),(10341,'2014-11-24','2014-12-01','2014-11-29','Shipped',NULL,382),(10342,'2014-11-24','2014-12-01','2014-11-29','Shipped',NULL,114),(10343,'2014-11-24','2014-12-01','2014-11-26','Shipped',NULL,353),(10344,'2014-11-25','2014-12-02','2014-11-29','Shipped',NULL,350),(10345,'2014-11-25','2014-12-01','2014-11-26','Shipped',NULL,103),(10346,'2014-11-29','2014-12-05','2014-11-30','Shipped',NULL,112),(10347,'2014-11-29','2014-12-07','2014-11-30','Shipped','Can we deliver the new Ford Mustang models by end-of-quarter?',114),(10348,'2014-11-01','2014-11-08','2014-11-05','Shipped',NULL,458),(10349,'2014-12-01','2014-12-07','2014-12-03','Shipped',NULL,151),(10350,'2014-12-02','2014-12-08','2014-12-05','Shipped',NULL,141),(10351,'2014-12-03','2014-12-11','2014-12-07','Shipped',NULL,324),(10352,'2014-12-03','2014-12-12','2014-12-09','Shipped',NULL,198),(10353,'2014-12-04','2014-12-11','2014-12-05','Shipped',NULL,447),(10354,'2014-12-04','2014-12-10','2014-12-05','Shipped',NULL,323),(10355,'2014-12-07','2014-12-14','2014-12-13','Shipped',NULL,141),(10356,'2014-12-09','2014-12-15','2014-12-12','Shipped',NULL,250),(10357,'2014-12-10','2014-12-16','2014-12-14','Shipped',NULL,124),(10358,'2014-12-10','2014-12-16','2014-12-16','Shipped','Customer requested that DHL is used for this shipping',141),(10359,'2014-12-15','2014-12-23','2014-12-18','Shipped',NULL,353),(10360,'2014-12-16','2014-12-22','2014-12-18','Shipped',NULL,496),(10361,'2014-12-17','2014-12-24','2014-12-20','Shipped',NULL,282),(10362,'2015-01-05','2015-01-16','2015-01-10','Shipped',NULL,161),(10363,'2015-01-06','2015-01-12','2015-01-10','Shipped',NULL,334),(10364,'2015-01-06','2015-01-17','2015-01-09','Shipped',NULL,350),(10365,'2015-01-07','2015-01-18','2015-01-11','Shipped',NULL,320),(10366,'2015-01-10','2015-01-19','2015-01-12','Shipped',NULL,381),(10367,'2015-01-12','2015-01-21','2015-01-16','Resolved','This order was disputed and resolved on 2/1/2005. Customer claimed that container with shipment was damaged. FedEx\'s investigation proved this wrong.',205),(10368,'2015-01-19','2015-01-27','2015-01-24','Shipped','Can we renegotiate this one?',124),(10369,'2015-01-20','2015-01-28','2015-01-24','Shipped',NULL,379),(10370,'2015-01-20','2015-02-01','2015-01-25','Shipped',NULL,276),(10371,'2015-01-23','2015-02-03','2015-01-25','Shipped',NULL,124),(10372,'2015-01-26','2015-02-05','2015-01-28','Shipped',NULL,398),(10373,'2015-01-31','2015-02-08','2015-02-06','Shipped',NULL,311),(10374,'2015-02-02','2015-02-09','2015-02-03','Shipped',NULL,333),(10375,'2015-02-03','2015-02-10','2015-02-06','Shipped',NULL,119),(10376,'2015-02-08','2015-02-18','2015-02-13','Shipped',NULL,219),(10377,'2015-02-09','2015-02-21','2015-02-12','Shipped','Cautious optimism. We have happy customers here, if we can keep them well stocked.  I need all the information I can get on the planned shippments of Porches',186),(10378,'2015-02-10','2015-02-18','2015-02-11','Shipped',NULL,141),(10379,'2015-02-10','2015-02-18','2015-02-11','Shipped',NULL,141),(10380,'2015-02-16','2015-02-24','2015-02-18','Shipped',NULL,141),(10381,'2015-02-17','2015-02-25','2015-02-18','Shipped',NULL,321),(10382,'2015-02-17','2015-02-23','2015-02-18','Shipped','Custom shipping instructions sent to warehouse',124),(10383,'2015-02-22','2015-03-02','2015-02-25','Shipped',NULL,141),(10384,'2015-02-23','2015-03-06','2015-02-27','Shipped',NULL,321),(10385,'2015-02-28','2015-03-09','2015-03-01','Shipped',NULL,124),(10386,'2015-03-01','2015-03-09','2015-03-06','Resolved','Disputed then Resolved on 3/15/2005. Customer doesn\'t like the craftsmaship of the models.',141),(10387,'2015-03-02','2015-03-09','2015-03-06','Shipped','We need to keep in close contact with their Marketing VP. He is the decision maker for all their purchases.',148),(10388,'2015-03-03','2015-03-11','2015-03-09','Shipped',NULL,462),(10389,'2015-03-03','2015-03-09','2015-03-08','Shipped',NULL,448),(10390,'2015-03-04','2015-03-11','2015-03-07','Shipped','They want to reevaluate their terms agreement with Finance.',124),(10391,'2015-03-09','2015-03-20','2015-03-15','Shipped',NULL,276),(10392,'2015-03-10','2015-03-18','2015-03-12','Shipped',NULL,452),(10393,'2015-03-11','2015-03-22','2015-03-14','Shipped','They want to reevaluate their terms agreement with Finance.',323),(10394,'2015-03-15','2015-03-25','2015-03-19','Shipped',NULL,141),(10395,'2015-03-17','2015-03-24','2015-03-23','Shipped','We must be cautions with this customer. Their VP of Sales resigned. Company may be heading down.',250),(10396,'2015-03-23','2015-04-02','2015-03-28','Shipped',NULL,124),(10397,'2015-03-28','2015-04-09','2015-04-01','Shipped',NULL,242),(10398,'2015-03-30','2015-04-09','2015-03-31','Shipped',NULL,353),(10399,'2015-04-01','2015-04-12','2015-04-03','Shipped',NULL,496),(10400,'2015-04-01','2015-04-11','2015-04-04','Shipped','Customer requested that DHL is used for this shipping',450),(10401,'2015-04-03','2015-04-14',NULL,'On Hold','Customer credit limit exceeded. Will ship when a payment is received.',328),(10402,'2015-04-07','2015-04-14','2015-04-12','Shipped',NULL,406),(10403,'2015-04-08','2015-04-18','2015-04-11','Shipped',NULL,201),(10404,'2015-04-08','2015-04-14','2015-04-11','Shipped',NULL,323),(10405,'2015-04-14','2015-04-24','2015-04-20','Shipped',NULL,209),(10406,'2015-04-15','2015-04-25','2015-04-21','Disputed','Customer claims container with shipment was damaged during shipping and some items were missing. I am talking to FedEx about this.',145),(10407,'2015-04-22','2015-05-04',NULL,'On Hold','Customer credit limit exceeded. Will ship when a payment is received.',450),(10408,'2015-04-22','2015-04-29','2015-04-27','Shipped',NULL,398),(10409,'2015-04-23','2015-05-05','2015-04-24','Shipped',NULL,166),(10410,'2015-04-29','2015-05-10','2015-04-30','Shipped',NULL,357),(10411,'2015-05-01','2015-05-08','2015-05-06','Shipped',NULL,233),(10412,'2015-05-03','2015-05-13','2015-05-05','Shipped',NULL,141),(10413,'2015-05-05','2015-05-14','2015-05-09','Shipped','Customer requested that DHL is used for this shipping',175),(10414,'2015-05-06','2015-05-13',NULL,'On Hold','Customer credit limit exceeded. Will ship when a payment is received.',362),(10415,'2015-05-09','2015-05-20','2015-05-12','Disputed','Customer claims the scales of the models don\'t match what was discussed. I keep all the paperwork though to prove otherwise',471),(10416,'2015-05-10','2015-05-16','2015-05-14','Shipped',NULL,386),(10417,'2015-05-13','2015-05-19','2015-05-19','Disputed','Customer doesn\'t like the colors and precision of the models.',141),(10418,'2015-05-16','2015-05-24','2015-05-20','Shipped',NULL,412),(10419,'2015-05-17','2015-05-28','2015-05-19','Shipped',NULL,382),(10420,'2015-05-29','2015-06-07',NULL,'In Process',NULL,282),(10421,'2015-05-29','2015-06-06',NULL,'In Process','Custom shipping instructions were sent to warehouse',124),(10422,'2015-05-30','2015-06-11',NULL,'In Process',NULL,157),(10423,'2015-05-30','2015-06-05',NULL,'In Process',NULL,314),(10424,'2015-05-31','2015-06-08',NULL,'In Process',NULL,141),(10425,'2015-05-31','2015-06-07',NULL,'In Process',NULL,119);

执行查询

以下语句查找每个客户的订单日期、下一个和前一个订单日期:

SELECT customerName,orderDate,LEAD(orderDate,1) OVER (PARTITION BY customerNumberORDER BY orderDate ) nextOrderDate,LAG(orderDate,1) OVER (PARTITION BY customerNumberORDER BY orderDate ) prevOrderDate
FROMorders
INNER JOIN customers USING (customerNumber);
Atelier graphique       2013-05-20      2014-09-27      NULL
Atelier graphique       2014-09-27      2014-11-25      2013-05-20
Atelier graphique       2014-11-25      NULL    2014-09-27
Signal Gift Stores      2013-05-21      2014-08-06      NULL
Signal Gift Stores      2014-08-06      2014-11-29      2013-05-21
Signal Gift Stores      2014-11-29      NULL    2014-08-06
Blauer See Auto, Co.    2013-01-09      2013-10-04      NULL
Blauer See Auto, Co.    2013-10-04      2014-03-15      2013-01-09
Blauer See Auto, Co.    2014-03-15      2014-11-05      2013-10-04
Blauer See Auto, Co.    2014-11-05      NULL    2014-03-15
Saveley & Henriot, Co.  2013-11-25      2014-01-02      NULL
Saveley & Henriot, Co.  2014-01-02      2014-03-02      2013-11-25
Saveley & Henriot, Co.  2014-03-02      NULL    2014-01-02
Dragon Souveniers, Ltd. 2013-04-16      2013-09-19      NULL
Dragon Souveniers, Ltd. 2013-09-19      2013-10-22      2013-04-16
Dragon Souveniers, Ltd. 2013-10-22      2014-08-04      2013-09-19
Dragon Souveniers, Ltd. 2014-08-04      2015-03-02      2013-10-22
Dragon Souveniers, Ltd. 2015-03-02      NULL    2014-08-04
Diecast Classics Inc.   2014-07-20      2014-08-19      NULL
Diecast Classics Inc.   2014-08-19      2014-11-02      2014-07-20
Diecast Classics Inc.   2014-11-02      2015-05-30      2014-08-19
Diecast Classics Inc.   2015-05-30      NULL    2014-11-02
Gift Depot Inc. 2013-11-05      2014-06-28      NULL
Gift Depot Inc. 2014-06-28      2015-05-05      2013-11-05
Gift Depot Inc. 2015-05-05      NULL    2014-06-28
Osaka Souveniers Co.    2014-01-12      2014-04-13      NULL
Osaka Souveniers Co.    2014-04-13      NULL    2014-01-12
Clover Collections, Co. 2014-02-12      2014-09-16      NULL
Clover Collections, Co. 2014-09-16      NULL    2014-02-12
Canadian Gift Exchange Network  2013-12-05      2014-10-22      NULL
Canadian Gift Exchange Network  2014-10-22      NULL    2013-12-05
Toys4GrownUps.com       2013-08-25      2013-11-18      NULL
Toys4GrownUps.com       2013-11-18      2015-01-12      2013-08-25
Toys4GrownUps.com       2015-01-12      NULL    2013-11-18
Mini Caravy     2014-04-13      2014-06-04      NULL
Mini Caravy     2014-06-04      2015-04-14      2014-04-13
Mini Caravy     2015-04-14      NULL    2014-06-04
King Kong Collectables, Co.     2013-11-15      2013-12-01      NULL
King Kong Collectables, Co.     2013-12-01      NULL    2013-11-15
Heintze Collectables    2013-10-17      2014-10-22      NULL
Heintze Collectables    2014-10-22      NULL    2013-10-17
Qubec Home Shopping Network     2013-11-05      2014-06-17      NULL
Qubec Home Shopping Network     2014-06-17      2015-05-01      2013-11-05
Qubec Home Shopping Network     2015-05-01      NULL    2014-06-17
Lyon Souveniers 2013-07-01      2014-12-09      NULL
Lyon Souveniers 2014-12-09      2015-03-17      2013-07-01
Lyon Souveniers 2015-03-17      NULL    2014-12-09
Toms Spezialitten, Ltd  2013-11-20      2014-10-16      NULL
Toms Spezialitten, Ltd  2014-10-16      NULL    2013-11-20
Marta's Replicas Co.    2014-08-27      2014-10-13      NULL
Marta's Replicas Co.    2014-10-13      NULL    2014-08-27
Norway Gifts By Mail, Co.       2013-10-05      2014-08-21      NULL
Norway Gifts By Mail, Co.       2014-08-21      NULL    2013-10-05
Mini Classics   2013-11-25      2014-10-15      NULL
Mini Classics   2014-10-15      NULL    2013-11-25
Corporate Gift Ideas Co.        2013-10-10      2013-10-18      NULL
Corporate Gift Ideas Co.        2013-10-18      2015-02-17      2013-10-10
Corporate Gift Ideas Co.        2015-02-17      2015-02-23      2013-10-18
Corporate Gift Ideas Co.        2015-02-23      NULL    2015-02-17
Stylish Desk Decors, Co.        2013-06-12      2013-11-06      NULL
Stylish Desk Decors, Co.        2013-11-06      2014-12-03      2013-06-12
Stylish Desk Decors, Co.        2014-12-03      NULL    2013-11-06
Tekni Collectables Inc. 2014-03-29      2014-05-18      NULL
Tekni Collectables Inc. 2014-05-18      2015-04-03      2014-03-29
Tekni Collectables Inc. 2015-04-03      NULL    2014-05-18
Australian Gift Network, Co     2013-09-25      2013-11-06      NULL
Australian Gift Network, Co     2013-11-06      2015-02-02      2013-09-25
Australian Gift Network, Co     2015-02-02      NULL    2013-11-06
Classic Gift Ideas, Inc 2013-11-13      2014-10-14      NULL
Classic Gift Ideas, Inc 2014-10-14      NULL    2013-11-13
Men 'R' US Retailers, Ltd.      2013-10-11      2014-01-09      NULL
Men 'R' US Retailers, Ltd.      2014-01-09      NULL    2013-10-11
Marseille Mini Autos    2013-05-08      2014-11-25      NULL
Marseille Mini Autos    2014-11-25      2015-01-06      2013-05-08
Marseille Mini Autos    2015-01-06      NULL    2014-11-25
Reims Collectables      2013-05-07      2013-07-10      NULL
Reims Collectables      2013-07-10      2014-11-24      2013-05-07
Reims Collectables      2014-11-24      2014-12-15      2013-07-10
Reims Collectables      2014-12-15      2015-03-30      2014-11-24
Reims Collectables      2015-03-30      NULL    2014-12-15
Gifts4AllAges.com       2014-06-30      2014-09-10      NULL
Gifts4AllAges.com       2014-09-10      2015-05-06      2014-06-30
Gifts4AllAges.com       2015-05-06      NULL    2014-09-10
Online Diecast Creations Co.    2013-01-06      2013-11-20      NULL
Online Diecast Creations Co.    2013-11-20      2014-11-04      2013-01-06
Online Diecast Creations Co.    2014-11-04      NULL    2013-11-20
Royale Belge    2013-04-11      2013-08-13      NULL
Royale Belge    2013-08-13      2014-11-22      2013-04-11
Royale Belge    2014-11-22      2015-01-10      2013-08-13
Royale Belge    2015-01-10      NULL    2014-11-22
Cruz & Sons Co. 2013-03-03      2013-11-27      NULL
Cruz & Sons Co. 2013-11-27      2014-11-16      2013-03-03
Cruz & Sons Co. 2014-11-16      NULL    2013-11-27
L'ordine Souveniers     2013-11-06      2014-07-06      NULL
L'ordine Souveniers     2014-07-06      2015-05-10      2013-11-06
L'ordine Souveniers     2015-05-10      NULL    2014-07-06
Tokyo Collectables, Ltd 2014-06-15      2014-11-23      NULL
Tokyo Collectables, Ltd 2014-11-23      2015-01-26      2014-06-15
Tokyo Collectables, Ltd 2015-01-26      2015-04-22      2014-11-23
Tokyo Collectables, Ltd 2015-04-22      NULL    2015-01-26
Auto Canal+ Petit       2014-01-15      2014-05-26      NULL
Auto Canal+ Petit       2014-05-26      2015-04-07      2014-01-15
Auto Canal+ Petit       2015-04-07      NULL    2014-05-26
Extreme Desk Decorations, Ltd   2014-03-30      2014-07-12      NULL
Extreme Desk Decorations, Ltd   2014-07-12      2015-05-16      2014-03-30
Extreme Desk Decorations, Ltd   2015-05-16      NULL    2014-07-12
Bavarian Collectables Imports, Co.      2014-09-15      NULL    NULL
Gift Ideas Corp.        2013-06-16      2013-09-03      NULL
Gift Ideas Corp.        2013-09-03      2014-12-04      2013-06-16
Gift Ideas Corp.        2014-12-04      NULL    2013-09-03
Mini Auto Werke 2013-10-21      2013-11-04      NULL
Mini Auto Werke 2013-11-04      2015-03-10      2013-10-21
Mini Auto Werke 2015-03-10      NULL    2013-11-04
Super Scale Inc.        2013-11-26      2014-05-04      NULL
Super Scale Inc.        2014-05-04      NULL    2013-11-26
Microscale Inc. 2014-04-20      2014-11-03      NULL
Microscale Inc. 2014-11-03      NULL    2014-04-20
Corrida Auto Replicas, Ltd      2013-05-28      2014-01-26      NULL
Corrida Auto Replicas, Ltd      2014-01-26      2014-11-01      2013-05-28
Corrida Auto Replicas, Ltd      2014-11-01      NULL    2014-01-26
FunGiftIdeas.com        2013-10-21      2014-11-04      NULL
FunGiftIdeas.com        2014-11-04      2015-03-03      2013-10-21
FunGiftIdeas.com        2015-03-03      NULL    2014-11-04
Australian Collectables, Ltd    2013-11-21      2014-07-02      NULL
Australian Collectables, Ltd    2014-07-02      2015-05-09      2013-11-21
Australian Collectables, Ltd    2015-05-09      NULL    2014-07-02
Frau da Collezione      2013-10-09      2014-02-09      NULL
Frau da Collezione      2014-02-09      NULL    2013-10-09
Iberia Gift Imports, Corp.      2013-11-14      2014-10-06      NULL
Iberia Gift Imports, Corp.      2014-10-06      NULL    2013-11-14
Motor Mint Distributors Inc.    2013-03-10      2014-04-03      NULL
Motor Mint Distributors Inc.    2014-04-03      2014-11-17      2013-03-10
Motor Mint Distributors Inc.    2014-11-17      NULL    2014-04-03
Double Decker Gift Stores, Ltd  2013-11-14      2014-01-22      NULL
Double Decker Gift Stores, Ltd  2014-01-22      NULL    2013-11-14
Kelly's Gift Shop       2013-07-07      2013-11-11      NULL
Kelly's Gift Shop       2013-11-11      2014-12-16      2013-07-07
Kelly's Gift Shop       2014-12-16      2015-04-01      2013-11-11
Kelly's Gift Shop       2015-04-01      NULL    2014-12-16
Australian Collectors, Co.      2013-04-29      2013-05-21      NULL
Australian Collectors, Co.      2013-05-21      2014-02-20      2013-04-29
Australian Collectors, Co.      2014-02-20      2014-11-24      2013-05-21
Australian Collectors, Co.      2014-11-24      2014-11-29      2014-02-20
Australian Collectors, Co.      2014-11-29      NULL    2014-11-24
La Rochelle Gifts       2014-07-23      2014-10-29      NULL
La Rochelle Gifts       2014-10-29      2015-02-03      2014-07-23
La Rochelle Gifts       2015-02-03      2015-05-31      2014-10-29
La Rochelle Gifts       2015-05-31      NULL    2015-02-03
Baane Mini Imports      2013-01-29      2013-10-10      NULL
Baane Mini Imports      2013-10-10      2014-10-15      2013-01-29
Baane Mini Imports      2014-10-15      2014-11-05      2013-10-10
Baane Mini Imports      2014-11-05      NULL    2014-10-15
Mini Gifts Distributors Ltd.    2013-03-26      2013-07-02      NULL
Mini Gifts Distributors Ltd.    2013-07-02      2013-08-08      2013-03-26
Mini Gifts Distributors Ltd.    2013-08-08      2013-11-12      2013-07-02
Mini Gifts Distributors Ltd.    2013-11-12      2014-03-11      2013-08-08
Mini Gifts Distributors Ltd.    2014-03-11      2014-07-20      2013-11-12
Mini Gifts Distributors Ltd.    2014-07-20      2014-08-20      2014-03-11
Mini Gifts Distributors Ltd.    2014-08-20      2014-10-21      2014-07-20
Mini Gifts Distributors Ltd.    2014-10-21      2014-11-19      2014-08-20
Mini Gifts Distributors Ltd.    2014-11-19      2014-12-10      2014-10-21
Mini Gifts Distributors Ltd.    2014-12-10      2015-01-19      2014-11-19
Mini Gifts Distributors Ltd.    2015-01-19      2015-01-23      2014-12-10
Mini Gifts Distributors Ltd.    2015-01-23      2015-02-17      2015-01-19
Mini Gifts Distributors Ltd.    2015-02-17      2015-02-28      2015-01-23
Mini Gifts Distributors Ltd.    2015-02-28      2015-03-04      2015-02-17
Mini Gifts Distributors Ltd.    2015-03-04      2015-03-23      2015-02-28
Mini Gifts Distributors Ltd.    2015-03-23      2015-05-29      2015-03-04
Mini Gifts Distributors Ltd.    2015-05-29      NULL    2015-03-23
Mini Wheels Co. 2013-03-25      2013-12-01      NULL
Mini Wheels Co. 2013-12-01      2014-11-18      2013-03-25
Mini Wheels Co. 2014-11-18      NULL    2013-12-01
Land of Toys Inc.       2013-02-24      2014-05-07      NULL
Land of Toys Inc.       2014-05-07      2014-09-08      2013-02-24
Land of Toys Inc.       2014-09-08      2014-11-15      2014-05-07
Land of Toys Inc.       2014-11-15      NULL    2014-09-08
Euro+ Shopping Channel  2013-01-31      2013-06-06      NULL
Euro+ Shopping Channel  2013-06-06      2013-06-27      2013-01-31
Euro+ Shopping Channel  2013-06-27      2013-09-28      2013-06-06
Euro+ Shopping Channel  2013-09-28      2013-10-08      2013-06-27
Euro+ Shopping Channel  2013-10-08      2013-11-19      2013-09-28
Euro+ Shopping Channel  2013-11-19      2013-12-02      2013-10-08
Euro+ Shopping Channel  2013-12-02      2013-12-03      2013-11-19
Euro+ Shopping Channel  2013-12-03      2014-01-16      2013-12-02
Euro+ Shopping Channel  2014-01-16      2014-04-29      2013-12-03
Euro+ Shopping Channel  2014-04-29      2014-05-05      2014-01-16
Euro+ Shopping Channel  2014-05-05      2014-06-24      2014-04-29
Euro+ Shopping Channel  2014-06-24      2014-08-09      2014-05-05
Euro+ Shopping Channel  2014-08-09      2014-10-16      2014-06-24
Euro+ Shopping Channel  2014-10-16      2014-12-02      2014-08-09
Euro+ Shopping Channel  2014-12-02      2014-12-07      2014-10-16
Euro+ Shopping Channel  2014-12-07      2014-12-10      2014-12-02
Euro+ Shopping Channel  2014-12-10      2015-02-10      2014-12-07
Euro+ Shopping Channel  2015-02-10      2015-02-10      2014-12-10
Euro+ Shopping Channel  2015-02-10      2015-02-16      2015-02-10
Euro+ Shopping Channel  2015-02-16      2015-02-22      2015-02-10
Euro+ Shopping Channel  2015-02-22      2015-03-01      2015-02-16
Euro+ Shopping Channel  2015-03-01      2015-03-15      2015-02-22
Euro+ Shopping Channel  2015-03-15      2015-05-03      2015-03-01
Euro+ Shopping Channel  2015-05-03      2015-05-13      2015-03-15
Euro+ Shopping Channel  2015-05-13      2015-05-31      2015-05-03
Euro+ Shopping Channel  2015-05-31      NULL    2015-05-13
Danish Wholesale Imports        2013-02-11      2014-04-09      NULL
Danish Wholesale Imports        2014-04-09      2014-06-08      2013-02-11
Danish Wholesale Imports        2014-06-08      2014-11-10      2014-04-09
Danish Wholesale Imports        2014-11-10      2015-04-15      2014-06-08
Danish Wholesale Imports        2015-04-15      NULL    2014-11-10
Muscle Machine Inc      2013-06-03      2013-12-02      NULL
Muscle Machine Inc      2013-12-02      2014-07-07      2013-06-03
Muscle Machine Inc      2014-07-07      2014-12-01      2013-12-02
Muscle Machine Inc      2014-12-01      NULL    2014-07-07
Technics Stores Inc.    2013-07-24      2013-10-28      NULL
Technics Stores Inc.    2013-10-28      2014-11-02      2013-07-24
Technics Stores Inc.    2014-11-02      2015-01-05      2013-10-28
Technics Stores Inc.    2015-01-05      NULL    2014-11-02
Handji Gifts& Co        2014-02-04      2014-06-15      NULL
Handji Gifts& Co        2014-06-15      2014-09-01      2014-02-04
Handji Gifts& Co        2014-09-01      2015-04-23      2014-06-15
Handji Gifts& Co        2015-04-23      NULL    2014-09-01
Herkku Gifts    2013-11-12      2013-11-18      NULL
Herkku Gifts    2013-11-18      2014-09-03      2013-11-12
Herkku Gifts    2014-09-03      NULL    2013-11-18
Daedalus Designs Imports        2013-11-11      2014-02-21      NULL
Daedalus Designs Imports        2014-02-21      NULL    2013-11-11
La Corne D'abondance, Co.       2013-04-01      2014-08-28      NULL
La Corne D'abondance, Co.       2014-08-28      2014-11-20      2013-04-01
La Corne D'abondance, Co.       2014-11-20      NULL    2014-08-28
Cambridge Collectables Co.      2014-03-10      2014-05-08      NULL
Cambridge Collectables Co.      2014-05-08      NULL    2014-03-10
Vitachrome Inc. 2013-01-10      2014-04-05      NULL
Vitachrome Inc. 2014-04-05      2014-11-05      2013-01-10
Vitachrome Inc. 2014-11-05      NULL    2014-04-05
Toys of Finland, Co.    2013-10-06      2014-09-30      NULL
Toys of Finland, Co.    2014-09-30      2015-02-09      2013-10-06
Toys of Finland, Co.    2015-02-09      NULL    2014-09-30
Auto-Moto Classics Inc. 2013-06-16      2014-09-07      NULL
Auto-Moto Classics Inc. 2014-09-07      2014-12-03      2013-06-16
Auto-Moto Classics Inc. 2014-12-03      NULL    2014-09-07
UK Collectables, Ltd.   2013-10-06      2014-06-01      NULL
UK Collectables, Ltd.   2014-06-01      2015-04-08      2013-10-06
UK Collectables, Ltd.   2015-04-08      NULL    2014-06-01
Online Mini Collectables        2014-08-02      2014-09-10      NULL
Online Mini Collectables        2014-09-10      NULL    2014-08-02
Enaco Distributors      2013-04-21      2013-11-26      NULL
Enaco Distributors      2013-11-26      2014-11-24      2013-04-21
Enaco Distributors      2014-11-24      NULL    2013-11-26
Boards & Toys Co.       2013-10-02      2015-02-08      NULL
Boards & Toys Co.       2015-02-08      NULL    2013-10-02
Collectable Mini Designs Co.    2014-02-19      2014-02-26      NULL
Collectable Mini Designs Co.    2014-02-26      NULL    2014-02-19
giftsbymail.co.uk       2014-03-20      2014-11-01      NULL
giftsbymail.co.uk       2014-11-01      NULL    2014-03-20
Alpha Cognac    2013-07-04      2013-11-08      NULL
Alpha Cognac    2013-11-08      2015-03-28      2013-07-04
Alpha Cognac    2015-03-28      NULL    2013-11-08
Amica Models & Co.      2014-08-17      2014-09-09      NULL
Amica Models & Co.      2014-09-09      NULL    2014-08-17
Auto Associs & Cie.     2014-02-02      2014-10-11      NULL
Auto Associs & Cie.     2014-10-11      NULL    2014-02-02
Royal Canadian Collectables, Ltd.       2014-04-02      2014-08-20      NULL
Royal Canadian Collectables, Ltd.       2014-08-20      NULL    2014-04-02
Anna's Decorations, Ltd 2013-09-11      2013-11-04      NULL
Anna's Decorations, Ltd 2013-11-04      2015-01-20      2013-09-11
Anna's Decorations, Ltd 2015-01-20      2015-03-09      2013-11-04
Anna's Decorations, Ltd 2015-03-09      NULL    2015-01-20
Rovelli Gifts   2013-02-17      2013-11-05      NULL
Rovelli Gifts   2013-11-05      2014-11-12      2013-02-17
Rovelli Gifts   2014-11-12      NULL    2013-11-05
Souveniers And Things Co.       2013-07-16      2014-07-19      NULL
Souveniers And Things Co.       2014-07-19      2014-12-17      2013-07-16
Souveniers And Things Co.       2014-12-17      2015-05-29      2014-07-19
Souveniers And Things Co.       2015-05-29      NULL    2014-12-17
Vida Sport, Ltd 2014-02-22      2014-08-30      NULL
Vida Sport, Ltd 2014-08-30      NULL    2014-02-22
Oulu Toy Supplies, Inc. 2013-09-21      2014-04-12      NULL
Oulu Toy Supplies, Inc. 2014-04-12      2015-01-31      2013-09-21
Oulu Toy Supplies, Inc. 2015-01-31      NULL    2014-04-12
Petit Auto      2014-02-18      2014-07-21      NULL
Petit Auto      2014-07-21      2015-05-30      2014-02-18
Petit Auto      2015-05-30      NULL    2014-07-21
Mini Creations Ltd.     2013-08-10      2013-11-14      NULL
Mini Creations Ltd.     2013-11-14      2015-01-07      2013-08-10
Mini Creations Ltd.     2015-01-07      NULL    2013-11-14
Down Under Souveniers, Inc      2013-06-25      2014-06-03      NULL
Down Under Souveniers, Inc      2014-06-03      2014-12-04      2013-06-25
Down Under Souveniers, Inc      2014-12-04      2015-03-11      2014-06-03
Down Under Souveniers, Inc      2015-03-11      2015-04-08      2014-12-04
Down Under Souveniers, Inc      2015-04-08      NULL    2015-03-11
Suominen Souveniers     2013-08-01      2014-05-05      NULL
Suominen Souveniers     2014-05-05      2015-01-06      2013-08-01
Suominen Souveniers     2015-01-06      NULL    2014-05-05
CAF Imports     2013-11-07      2014-03-19      NULL
CAF Imports     2014-03-19      NULL    2013-11-07
GiftsForHim.com 2013-12-02      2014-06-16      NULL
GiftsForHim.com 2014-06-16      2015-04-29      2013-12-02
GiftsForHim.com 2015-04-29      NULL    2014-06-16
Collectables For Less Inc.      2013-09-05      2014-07-21      NULL
Collectables For Less Inc.      2014-07-21      2015-01-20      2013-09-05
Collectables For Less Inc.      2015-01-20      NULL    2014-07-21
Salzburg Collectables   2013-04-28      2014-07-16      NULL
Salzburg Collectables   2014-07-16      2014-11-24      2013-04-28
Salzburg Collectables   2014-11-24      2015-05-17      2014-07-16
Salzburg Collectables   2015-05-17      NULL    2014-11-24
Classic Legends Inc.    2013-04-04      2013-10-20      NULL
Classic Legends Inc.    2013-10-20      2014-11-21      2013-04-04
Classic Legends Inc.    2014-11-21      NULL    2013-10-20
Scandinavian Gift Ideas 2013-10-23      2014-09-08      NULL
Scandinavian Gift Ideas 2014-09-08      2015-03-03      2013-10-23
Scandinavian Gift Ideas 2015-03-03      NULL    2014-09-08
The Sharp Gifts Warehouse       2014-05-11      2014-06-14      NULL
The Sharp Gifts Warehouse       2014-06-14      2015-04-01      2014-05-11
The Sharp Gifts Warehouse       2015-04-01      2015-04-22      2014-06-14
The Sharp Gifts Warehouse       2015-04-22      NULL    2015-04-01
Signal Collectibles Ltd.        2013-09-12      2014-02-10      NULL
Signal Collectibles Ltd.        2014-02-10      NULL    2013-09-12
Diecast Collectables    2013-12-09      2014-04-26      NULL
Diecast Collectables    2014-04-26      NULL    2013-12-09
Volvo Model Replicas, Co        2013-03-24      2014-11-03      NULL
Volvo Model Replicas, Co        2014-11-03      2014-11-09      2013-03-24
Volvo Model Replicas, Co        2014-11-09      2014-11-19      2014-11-03
Volvo Model Replicas, Co        2014-11-19      NULL    2014-11-09
AV Stores, Co.  2013-03-18      2014-10-14      NULL
AV Stores, Co.  2014-10-14      2014-11-17      2013-03-18
AV Stores, Co.  2014-11-17      NULL    2014-10-14
West Coast Collectables Co.     2013-12-01      2014-01-29      NULL
West Coast Collectables Co.     2014-01-29      NULL    2013-12-01

在此示例中,我们首先将客户编号的结果集划分为多个分区。

然后,我们按订单日期对每个分区进行排序。

最后,LEAD()函数应用于每个分区以获取下一个订单日期。

LAG() 函数应用于每个分区以获取上一个订单日期。

一旦后续行穿过分区边界,nextOrderDate/prevOrderDate 每个分区的最后一行中的值就是NULL。

4. nth_value()

使用 Hive 的方式创建一个表

CREATE TABLE basic_pays(employee_name VARCHAR(50) NOT NULL,department VARCHAR(50) NOT NULL,salary INT NOT NULL
) STORED AS ORC;

插入一些测试数据

INSERT INTO basic_pays(employee_name, department, salary)
VALUES('Diane Murphy','Accounting',8435),('Mary Patterson','Accounting',9998),('Jeff Firrelli','Accounting',8992),('William Patterson','Accounting',8870),('Gerard Bondur','Accounting',11472),('Anthony Bow','Accounting',6627),('Leslie Jennings','IT',8113),('Leslie Thompson','IT',5186),('Julie Firrelli','Sales',9181),('Steve Patterson','Sales',9441),('Foon Yue Tseng','Sales',6660),('George Vanauf','Sales',10563),('Loui Bondur','SCM',10449),('Gerard Hernandez','SCM',6949),('Pamela Castillo','SCM',11303),('Larry Bott','SCM',11798),('Barry Jones','SCM',10586);

不分区

以下语句使用 NTH_VALUE() 函数查找薪水第二高的员工:

SELECTemployee_name,salary,NTH_VALUE(employee_name, 2) OVER  (ORDER BY salary DESC) second_highest_salary
FROMbasic_pays;
Larry Bott      11798   NULL
Gerard Bondur   11472   Gerard Bondur
Pamela Castillo 11303   Gerard Bondur
Barry Jones     10586   Gerard Bondur
George Vanauf   10563   Gerard Bondur
Loui Bondur     10449   Gerard Bondur
Mary Patterson  9998    Gerard Bondur
Steve Patterson 9441    Gerard Bondur
Julie Firrelli  9181    Gerard Bondur
Jeff Firrelli   8992    Gerard Bondur
William Patterson       8870    Gerard Bondur
Diane Murphy    8435    Gerard Bondur
Leslie Jennings 8113    Gerard Bondur
Gerard Hernandez        6949    Gerard Bondur
Foon Yue Tseng  6660    Gerard Bondur
Anthony Bow     6627    Gerard Bondur
Leslie Thompson 5186    Gerard Bondur

分区

在此查询中,我们添加了PARTITION BY按部门划分员工的子句。

然后 NTH_VALUE() 函数独立地应用于每个分区。

SELECTemployee_name,department,salary,NTH_VALUE(employee_name, 2) OVER  (PARTITION BY departmentORDER BY salary DESCRANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) second_highest_salary
FROMbasic_pays;
George Vanauf   Sales   10563   Steve Patterson
Steve Patterson Sales   9441    Steve Patterson
Julie Firrelli  Sales   9181    Steve Patterson
Foon Yue Tseng  Sales   6660    Steve Patterson
Gerard Bondur   Accounting      11472   Mary Patterson
Mary Patterson  Accounting      9998    Mary Patterson
Jeff Firrelli   Accounting      8992    Mary Patterson
William Patterson       Accounting      8870    Mary Patterson
Diane Murphy    Accounting      8435    Mary Patterson
Anthony Bow     Accounting      6627    Mary Patterson
Leslie Jennings IT      8113    Leslie Thompson
Leslie Thompson IT      5186    Leslie Thompson
Larry Bott      SCM     11798   Pamela Castillo
Pamela Castillo SCM     11303   Pamela Castillo
Barry Jones     SCM     10586   Pamela Castillo
Loui Bondur     SCM     10449   Pamela Castillo
Gerard Hernandez        SCM     6949    Pamela Castillo

5. ntile()

使用 Hive 的方式创建一个表

CREATE TABLE  ntileDemo (val INT NOT NULL
) STORED AS ORC;

插入一些测试数据

INSERT INTO ntileDemo(val)
VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9);

查询刚才插入的数据

SELECT * FROM ntileDemo;
1
2
3
4
5
6
7
8
9

如果使用 NTILE() 函数将九行划分为四个组,则最终将在第一组中包含三行,而另外三组总共包含六行。

1       1
2       1
3       1
4       2
5       2
6       3
7       3
8       4
9       4

从输出中可以看出,第一组有三行,而其他组有两行。

让我们将组的数量从四个更改为三个,如以下查询所示:

SELECT val, NTILE (3) OVER (ORDER BY val) group_no
FROM ntileDemo;
1       1
2       1
3       1
4       2
5       2
6       2
7       3
8       3
9       3

结果集现在有三个具有相同行数的组。

Spark SQL 内置函数(六)Window Functions(基于 Spark 3.2.0)相关推荐

  1. Spark SQL 内置函数(五)Aggregate Functions(基于 Spark 3.2.0)

    前言 本文隶属于专栏<1000个问题搞定大数据技术体系>,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢! 本专栏目录结构和参考文献请见1000个问题搞定大数据技 ...

  2. Spark SQL 内置函数(一)Array Functions(基于 Spark 3.2.0)

    前言 本文隶属于专栏<1000个问题搞定大数据技术体系>,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢! 本专栏目录结构和参考文献请见1000个问题搞定大数据技 ...

  3. 第70课:Spark SQL内置函数解密与实战 每天晚上20:00YY频道现场授课频道68917580

    每天晚上20:00YY频道现场授课频道68917580每天晚上20:00YY频道现场授课频道68917580 /* * *王家林老师授课http://weibo.com/ilovepains */ 源 ...

  4. 数据库|SQL内置函数使用手册

    SQL内置函数使用大全 本文中总结了SQL中常用的内置函数,包含通用聚合函数.安全检测函数.数学统计函数.字符串函数等 通用聚合函数 语句 含义 示例 arbitrary(x) 随机返回x列中的一个值 ...

  5. 【学习笔记】16、常用内置函数(Built-in Functions)

    常用内置函数(Built-in Functions) 可以参照中文版文档:Python3 内置函数 | 菜鸟教程 常用的内置函数可以做如下分类: 1.数据类型相关 type() – 取数据类型 int ...

  6. Blink SQL内置函数大全

    字符串函数 STR_TO_MAP 功能描述 使用listDelimiter将text分隔成K-V对,然后使用keyValueDelimiter分隔每个K-V对,组装成MAP返回.默认listDelim ...

  7. 转:Oracle SQL 内置函数大全 (一)

    SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ...

  8. Oracle SQL 内置函数大全

    SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ...

  9. oracle SQL以结尾函数,Oracle SQL 内置函数大全

    https://www.cnblogs.com/lfx0692/articles/2395950.html SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数; SQL> se ...

  10. Oracle SQL 内置函数大全(转)

    SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ...

最新文章

  1. Linuxtone命令一句话
  2. 2021暑假生产实习【SSM实习项目-超市积分管理系统】博客汇总表
  3. 热门的“挑战视频”,凭什么火起来的?
  4. android gridview滚动条位置,Android GridView滚动到指定位置
  5. 库存管理系统开发过程
  6. javaweb实现教师和教室管理系统 java jsp sqlserver
  7. 谈:典型三种近场通信的特点以及未来趋势
  8. 冲击IPO:达达的负“重”上市之路
  9. vp9 segment 详细分析
  10. 抖音怎么上热门?这样发视频点赞过万!
  11. u盘插上电脑后计算机没没磁盘,U盘插上电脑没有显示“可移动磁盘”的解决
  12. 电脑编程和计算机编程有什么区别,机器人编程与电脑编程有何区别?官方专家为你详细解说!...
  13. 龙蜥社区技术委员会主席杨勇:下一代操作系统展望
  14. Stata: 面板 Granger 因果检验
  15. 【ArcGIS Pro二次开发】(6):工程(Project)的基本操作
  16. HTML-CSS笔记
  17. 利用百分位数确定考核指标的具体数字
  18. 白云机场查获具有充电宝功能的时尚女包
  19. 结构建模设计——Solidworks软件入门基本操作初体验(软件功能简介、新建零件、绘制草图、建立实体)
  20. Mathematica 13.0.1 Linux版安装教程

热门文章

  1. 使用FFMPEG3.4.2版本进行视频的解码为YUV格式
  2. python屏蔽警告
  3. SylixOS网卡驱动实现篇
  4. 解决nginx 503 Service Temporarily Unavailable 方法
  5. DeepFool论文翻译---DeepFool: a simple and accurate method to fool deep neural networks
  6. 魔兽争霸lostTemple地图
  7. 纤亿通谈-单模和多模光纤跳线有哪些不同之处?
  8. 共码未来 | 盘点 2022 Google 谷歌开发者大会多重亮点!
  9. 外篇 评相对论
  10. 邮箱传大文件怎样快_5款好用的企业邮箱推荐