|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {
|
6 | 6 | "application/vnd.databricks.v1+cell": {
|
7 |
| - "cellMetadata": {}, |
| 7 | + "cellMetadata": { |
| 8 | + "byteLimit": 2048000, |
| 9 | + "rowLimit": 10000 |
| 10 | + }, |
8 | 11 | "inputWidgets": {},
|
9 | 12 | "nuid": "516a210c-988a-43a8-9004-cb03ad7c2481",
|
10 | 13 | "showTitle": false,
|
|
21 | 24 | "execution_count": 0,
|
22 | 25 | "metadata": {
|
23 | 26 | "application/vnd.databricks.v1+cell": {
|
24 |
| - "cellMetadata": {}, |
| 27 | + "cellMetadata": { |
| 28 | + "byteLimit": 2048000, |
| 29 | + "rowLimit": 10000 |
| 30 | + }, |
25 | 31 | "inputWidgets": {},
|
26 | 32 | "nuid": "9333a394-5bb0-4f1f-8f10-60b56c9fb68e",
|
27 | 33 | "showTitle": false,
|
|
40 | 46 | "cell_type": "markdown",
|
41 | 47 | "metadata": {
|
42 | 48 | "application/vnd.databricks.v1+cell": {
|
43 |
| - "cellMetadata": {}, |
| 49 | + "cellMetadata": { |
| 50 | + "byteLimit": 2048000, |
| 51 | + "rowLimit": 10000 |
| 52 | + }, |
44 | 53 | "inputWidgets": {},
|
45 | 54 | "nuid": "b718c875-1030-4207-a68c-5fad8635212a",
|
46 | 55 | "showTitle": false,
|
|
121 | 130 | "execution_count": 0,
|
122 | 131 | "metadata": {
|
123 | 132 | "application/vnd.databricks.v1+cell": {
|
124 |
| - "cellMetadata": {}, |
| 133 | + "cellMetadata": { |
| 134 | + "byteLimit": 2048000, |
| 135 | + "rowLimit": 10000 |
| 136 | + }, |
125 | 137 | "inputWidgets": {},
|
126 | 138 | "nuid": "01e4d778-a8a5-48a7-8ebb-7033f0b84d62",
|
127 | 139 | "showTitle": false,
|
128 | 140 | "tableResultSettingsMap": {},
|
129 | 141 | "title": ""
|
130 | 142 | }
|
131 | 143 | },
|
132 |
| - "outputs": [], |
| 144 | + "outputs": [ |
| 145 | + { |
| 146 | + "output_type": "stream", |
| 147 | + "name": "stdout", |
| 148 | + "output_type": "stream", |
| 149 | + "text": [ |
| 150 | + "+----------+------------+\n|product_id|product_name|\n+----------+------------+\n| 1| LC Phone|\n| 2| LC T-Shirt|\n| 3| LC Keychain|\n+----------+------------+\n\n+----------+------------+----------+-------------------+------------------+\n|product_id|period_start|period_end|average_daily_sales| year_list|\n+----------+------------+----------+-------------------+------------------+\n| 1| 2019-01-25|2019-02-28| 100| [2019]|\n| 2| 2018-12-01|2020-01-01| 10|[2018, 2019, 2020]|\n| 3| 2019-12-01|2020-01-31| 1| [2019, 2020]|\n+----------+------------+----------+-------------------+------------------+\n\n" |
| 151 | + ] |
| 152 | + } |
| 153 | + ], |
133 | 154 | "source": [
|
134 | 155 | "product_data_1384 = [\n",
|
135 | 156 | " (1, \"LC Phone\"),\n",
|
|
146 | 167 | " (3, \"2019-12-01\", \"2020-01-31\", 1)\n",
|
147 | 168 | "]\n",
|
148 | 169 | "sales_columns_1384 = [\"product_id\", \"period_start\", \"period_end\", \"average_daily_sales\"]\n",
|
149 |
| - "sales_df_1384 = spark.createDataFrame(sales_data_1384, sales_columns_1384)\n", |
| 170 | + "sales_df_1384 = spark.createDataFrame(sales_data_1384, sales_columns_1384)\\\n", |
| 171 | + " .withColumn(\"period_start\", col(\"period_start\").cast(DateType())) \\\n", |
| 172 | + " .withColumn(\"period_end\", col(\"period_end\").cast(DateType()))\\\n", |
| 173 | + " .withColumn(\"year_list\",sequence(year(col(\"period_start\")), year(col(\"period_end\"))))\n", |
| 174 | + "\n", |
150 | 175 | "sales_df_1384.show()"
|
151 | 176 | ]
|
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "code", |
| 180 | + "execution_count": 0, |
| 181 | + "metadata": { |
| 182 | + "application/vnd.databricks.v1+cell": { |
| 183 | + "cellMetadata": { |
| 184 | + "byteLimit": 2048000, |
| 185 | + "rowLimit": 10000 |
| 186 | + }, |
| 187 | + "inputWidgets": {}, |
| 188 | + "nuid": "e86cc1dd-71e8-4e41-b745-ed97f63b2c89", |
| 189 | + "showTitle": false, |
| 190 | + "tableResultSettingsMap": {}, |
| 191 | + "title": "" |
| 192 | + } |
| 193 | + }, |
| 194 | + "outputs": [], |
| 195 | + "source": [ |
| 196 | + "sales_exploded_df_1384 = sales_df_1384.withColumn(\"report_year\", explode(col(\"year_list\")))" |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "code", |
| 201 | + "execution_count": 0, |
| 202 | + "metadata": { |
| 203 | + "application/vnd.databricks.v1+cell": { |
| 204 | + "cellMetadata": { |
| 205 | + "byteLimit": 2048000, |
| 206 | + "rowLimit": 10000 |
| 207 | + }, |
| 208 | + "inputWidgets": {}, |
| 209 | + "nuid": "cef75ba1-e2cf-4a23-a0ae-420b95e0c5cc", |
| 210 | + "showTitle": false, |
| 211 | + "tableResultSettingsMap": {}, |
| 212 | + "title": "" |
| 213 | + } |
| 214 | + }, |
| 215 | + "outputs": [], |
| 216 | + "source": [ |
| 217 | + "sales_split_df_1384 = sales_exploded_df_1384\\\n", |
| 218 | + " .withColumn(\"start_date\",greatest(col(\"period_start\"), col(\"report_year\").cast(\"string\").cast(DateType())))\\\n", |
| 219 | + " .withColumn(\"end_date\",least(col(\"period_end\"),(col(\"report_year\") + lit(1)).cast(\"string\").cast(DateType()) - expr(\"INTERVAL 1 DAY\")))\n" |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "code", |
| 224 | + "execution_count": 0, |
| 225 | + "metadata": { |
| 226 | + "application/vnd.databricks.v1+cell": { |
| 227 | + "cellMetadata": { |
| 228 | + "byteLimit": 2048000, |
| 229 | + "rowLimit": 10000 |
| 230 | + }, |
| 231 | + "inputWidgets": {}, |
| 232 | + "nuid": "9005884e-330e-43ac-9fbd-52003cc83dd0", |
| 233 | + "showTitle": false, |
| 234 | + "tableResultSettingsMap": {}, |
| 235 | + "title": "" |
| 236 | + } |
| 237 | + }, |
| 238 | + "outputs": [], |
| 239 | + "source": [ |
| 240 | + "sales_split_df_1384 = sales_split_df_1384\\\n", |
| 241 | + " .withColumn(\"days\",datediff(col(\"end_date\"), col(\"start_date\")) + lit(1))\\\n", |
| 242 | + " .withColumn(\"total_amount\",col(\"days\") * col(\"average_daily_sales\"))" |
| 243 | + ] |
| 244 | + }, |
| 245 | + { |
| 246 | + "cell_type": "code", |
| 247 | + "execution_count": 0, |
| 248 | + "metadata": { |
| 249 | + "application/vnd.databricks.v1+cell": { |
| 250 | + "cellMetadata": { |
| 251 | + "byteLimit": 2048000, |
| 252 | + "rowLimit": 10000 |
| 253 | + }, |
| 254 | + "inputWidgets": {}, |
| 255 | + "nuid": "63b455c8-05b6-46aa-9988-8aa32e95239f", |
| 256 | + "showTitle": false, |
| 257 | + "tableResultSettingsMap": {}, |
| 258 | + "title": "" |
| 259 | + } |
| 260 | + }, |
| 261 | + "outputs": [ |
| 262 | + { |
| 263 | + "output_type": "stream", |
| 264 | + "name": "stdout", |
| 265 | + "output_type": "stream", |
| 266 | + "text": [ |
| 267 | + "+----------+------------+-----------+------------+\n|product_id|product_name|report_year|total_amount|\n+----------+------------+-----------+------------+\n| 1| LC Phone| 2019| 3500|\n| 2| LC T-Shirt| 2018| 310|\n| 2| LC T-Shirt| 2019| 3650|\n| 2| LC T-Shirt| 2020| 10|\n| 3| LC Keychain| 2019| 31|\n| 3| LC Keychain| 2020| 31|\n+----------+------------+-----------+------------+\n\n" |
| 268 | + ] |
| 269 | + } |
| 270 | + ], |
| 271 | + "source": [ |
| 272 | + "sales_split_df_1384\\\n", |
| 273 | + " .join(product_df_1384, on=\"product_id\", how=\"left\") \\\n", |
| 274 | + " .select(\"product_id\", \"product_name\", \"report_year\", \"total_amount\") \\\n", |
| 275 | + " .orderBy(\"product_id\", \"report_year\").show()" |
| 276 | + ] |
152 | 277 | }
|
153 | 278 | ],
|
154 | 279 | "metadata": {
|
155 | 280 | "application/vnd.databricks.v1+notebook": {
|
156 |
| - "computePreferences": null, |
| 281 | + "computePreferences": { |
| 282 | + "hardware": { |
| 283 | + "accelerator": null, |
| 284 | + "gpuPoolId": null, |
| 285 | + "memory": null |
| 286 | + } |
| 287 | + }, |
157 | 288 | "dashboards": [],
|
158 | 289 | "environmentMetadata": {
|
159 | 290 | "base_environment": "",
|
160 |
| - "environment_version": "1" |
| 291 | + "environment_version": "2" |
161 | 292 | },
|
162 | 293 | "inputWidgetPreferences": null,
|
163 | 294 | "language": "python",
|
|
0 commit comments