@memberjunction/query-gen 0.0.1 → 2.126.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +34 -0
  3. package/COORDINATOR.md +768 -0
  4. package/IMPLEMENTATION_PLAN.md +1753 -0
  5. package/LLM_ENTITY_GROUPING_PLAN.md +977 -0
  6. package/README.md +675 -29
  7. package/dist/cli/commands/export.d.ts +15 -0
  8. package/dist/cli/commands/export.d.ts.map +1 -0
  9. package/dist/cli/commands/export.js +178 -0
  10. package/dist/cli/commands/export.js.map +1 -0
  11. package/dist/cli/commands/generate.d.ts +19 -0
  12. package/dist/cli/commands/generate.d.ts.map +1 -0
  13. package/dist/cli/commands/generate.js +282 -0
  14. package/dist/cli/commands/generate.js.map +1 -0
  15. package/dist/cli/commands/validate.d.ts +17 -0
  16. package/dist/cli/commands/validate.d.ts.map +1 -0
  17. package/dist/cli/commands/validate.js +193 -0
  18. package/dist/cli/commands/validate.js.map +1 -0
  19. package/dist/cli/config.d.ts +51 -0
  20. package/dist/cli/config.d.ts.map +1 -0
  21. package/dist/cli/config.js +142 -0
  22. package/dist/cli/config.js.map +1 -0
  23. package/dist/cli/index.d.ts +13 -0
  24. package/dist/cli/index.d.ts.map +1 -0
  25. package/dist/cli/index.js +57 -0
  26. package/dist/cli/index.js.map +1 -0
  27. package/dist/core/EntityGrouper.d.ts +74 -0
  28. package/dist/core/EntityGrouper.d.ts.map +1 -0
  29. package/dist/core/EntityGrouper.js +246 -0
  30. package/dist/core/EntityGrouper.js.map +1 -0
  31. package/dist/core/MetadataExporter.d.ts +59 -0
  32. package/dist/core/MetadataExporter.d.ts.map +1 -0
  33. package/dist/core/MetadataExporter.js +151 -0
  34. package/dist/core/MetadataExporter.js.map +1 -0
  35. package/dist/core/QueryDatabaseWriter.d.ts +50 -0
  36. package/dist/core/QueryDatabaseWriter.d.ts.map +1 -0
  37. package/dist/core/QueryDatabaseWriter.js +152 -0
  38. package/dist/core/QueryDatabaseWriter.js.map +1 -0
  39. package/dist/core/QueryFixer.d.ts +48 -0
  40. package/dist/core/QueryFixer.d.ts.map +1 -0
  41. package/dist/core/QueryFixer.js +115 -0
  42. package/dist/core/QueryFixer.js.map +1 -0
  43. package/dist/core/QueryRefiner.d.ts +94 -0
  44. package/dist/core/QueryRefiner.d.ts.map +1 -0
  45. package/dist/core/QueryRefiner.js +267 -0
  46. package/dist/core/QueryRefiner.js.map +1 -0
  47. package/dist/core/QueryTester.d.ts +70 -0
  48. package/dist/core/QueryTester.d.ts.map +1 -0
  49. package/dist/core/QueryTester.js +243 -0
  50. package/dist/core/QueryTester.js.map +1 -0
  51. package/dist/core/QueryWriter.d.ts +57 -0
  52. package/dist/core/QueryWriter.d.ts.map +1 -0
  53. package/dist/core/QueryWriter.js +184 -0
  54. package/dist/core/QueryWriter.js.map +1 -0
  55. package/dist/core/QuestionGenerator.d.ts +58 -0
  56. package/dist/core/QuestionGenerator.d.ts.map +1 -0
  57. package/dist/core/QuestionGenerator.js +145 -0
  58. package/dist/core/QuestionGenerator.js.map +1 -0
  59. package/dist/data/schema.d.ts +230 -0
  60. package/dist/data/schema.d.ts.map +1 -0
  61. package/dist/data/schema.js +6 -0
  62. package/dist/data/schema.js.map +1 -0
  63. package/dist/index.d.ts +28 -0
  64. package/dist/index.d.ts.map +1 -0
  65. package/dist/index.js +77 -0
  66. package/dist/index.js.map +1 -0
  67. package/dist/prompts/PromptNames.d.ts +32 -0
  68. package/dist/prompts/PromptNames.d.ts.map +1 -0
  69. package/dist/prompts/PromptNames.js +35 -0
  70. package/dist/prompts/PromptNames.js.map +1 -0
  71. package/dist/utils/category-builder.d.ts +28 -0
  72. package/dist/utils/category-builder.d.ts.map +1 -0
  73. package/dist/utils/category-builder.js +90 -0
  74. package/dist/utils/category-builder.js.map +1 -0
  75. package/dist/utils/entity-helpers.d.ts +49 -0
  76. package/dist/utils/entity-helpers.d.ts.map +1 -0
  77. package/dist/utils/entity-helpers.js +189 -0
  78. package/dist/utils/entity-helpers.js.map +1 -0
  79. package/dist/utils/error-handlers.d.ts +19 -0
  80. package/dist/utils/error-handlers.d.ts.map +1 -0
  81. package/dist/utils/error-handlers.js +41 -0
  82. package/dist/utils/error-handlers.js.map +1 -0
  83. package/dist/utils/graph-helpers.d.ts +51 -0
  84. package/dist/utils/graph-helpers.d.ts.map +1 -0
  85. package/dist/utils/graph-helpers.js +82 -0
  86. package/dist/utils/graph-helpers.js.map +1 -0
  87. package/dist/utils/prompt-helpers.d.ts +25 -0
  88. package/dist/utils/prompt-helpers.d.ts.map +1 -0
  89. package/dist/utils/prompt-helpers.js +66 -0
  90. package/dist/utils/prompt-helpers.js.map +1 -0
  91. package/dist/utils/query-helpers.d.ts +23 -0
  92. package/dist/utils/query-helpers.d.ts.map +1 -0
  93. package/dist/utils/query-helpers.js +34 -0
  94. package/dist/utils/query-helpers.js.map +1 -0
  95. package/dist/utils/user-helpers.d.ts +15 -0
  96. package/dist/utils/user-helpers.d.ts.map +1 -0
  97. package/dist/utils/user-helpers.js +32 -0
  98. package/dist/utils/user-helpers.js.map +1 -0
  99. package/dist/vectors/EmbeddingService.d.ts +58 -0
  100. package/dist/vectors/EmbeddingService.d.ts.map +1 -0
  101. package/dist/vectors/EmbeddingService.js +90 -0
  102. package/dist/vectors/EmbeddingService.js.map +1 -0
  103. package/dist/vectors/SimilaritySearch.d.ts +51 -0
  104. package/dist/vectors/SimilaritySearch.d.ts.map +1 -0
  105. package/dist/vectors/SimilaritySearch.js +85 -0
  106. package/dist/vectors/SimilaritySearch.js.map +1 -0
  107. package/docs/API.md +1040 -0
  108. package/docs/ARCHITECTURE.md +1120 -0
  109. package/examples/advanced-usage.ts +401 -0
  110. package/examples/basic-usage.ts +285 -0
  111. package/package.json +48 -6
  112. package/src/cli/commands/export.ts +173 -0
  113. package/src/cli/commands/generate.ts +330 -0
  114. package/src/cli/commands/validate.ts +185 -0
  115. package/src/cli/config.ts +203 -0
  116. package/src/cli/index.ts +63 -0
  117. package/src/core/EntityGrouper.ts +318 -0
  118. package/src/core/MetadataExporter.ts +148 -0
  119. package/src/core/QueryDatabaseWriter.ts +187 -0
  120. package/src/core/QueryFixer.ts +153 -0
  121. package/src/core/QueryRefiner.ts +382 -0
  122. package/src/core/QueryTester.ts +264 -0
  123. package/src/core/QueryWriter.ts +239 -0
  124. package/src/core/QuestionGenerator.ts +199 -0
  125. package/src/data/golden-queries.json +1371 -0
  126. package/src/data/schema.ts +252 -0
  127. package/src/index.ts +49 -0
  128. package/src/prompts/PromptNames.ts +36 -0
  129. package/src/utils/category-builder.ts +97 -0
  130. package/src/utils/entity-helpers.ts +203 -0
  131. package/src/utils/error-handlers.ts +41 -0
  132. package/src/utils/graph-helpers.ts +99 -0
  133. package/src/utils/prompt-helpers.ts +79 -0
  134. package/src/utils/query-helpers.ts +32 -0
  135. package/src/utils/user-helpers.ts +39 -0
  136. package/src/vectors/EmbeddingService.ts +109 -0
  137. package/src/vectors/SimilaritySearch.ts +108 -0
  138. package/tsconfig.json +39 -0
@@ -0,0 +1,1371 @@
1
+ [
2
+ {
3
+ "name": "Top Customers by Revenue",
4
+ "userQuestion": "Who are our top 10 customers by total revenue?",
5
+ "description": "Returns the top customers ranked by total revenue with their contact information",
6
+ "technicalDescription": "Aggregates order totals by customer, joins customer details, orders by revenue descending",
7
+ "sql": "SELECT TOP 10 c.ID, c.Name, c.Email, c.Phone, SUM(o.TotalAmount) AS TotalRevenue, COUNT(o.ID) AS OrderCount FROM [dbo].[vwCustomers] c INNER JOIN [dbo].[vwOrders] o ON c.ID = o.CustomerID WHERE o.Status = 'Completed' AND o.OrderDate >= {{ startDate | sqlString }} GROUP BY c.ID, c.Name, c.Email, c.Phone ORDER BY TotalRevenue DESC",
8
+ "parameters": [
9
+ {
10
+ "name": "startDate",
11
+ "type": "date",
12
+ "isRequired": true,
13
+ "description": "Start date for revenue calculation period",
14
+ "usage": ["WHERE clause date filter"],
15
+ "defaultValue": null,
16
+ "sampleValue": "2024-01-01"
17
+ }
18
+ ],
19
+ "selectClause": [
20
+ {
21
+ "name": "ID",
22
+ "description": "Customer unique identifier",
23
+ "type": "string",
24
+ "optional": false
25
+ },
26
+ {
27
+ "name": "Name",
28
+ "description": "Customer full name",
29
+ "type": "string",
30
+ "optional": false
31
+ },
32
+ {
33
+ "name": "Email",
34
+ "description": "Customer email address",
35
+ "type": "string",
36
+ "optional": false
37
+ },
38
+ {
39
+ "name": "Phone",
40
+ "description": "Customer phone number",
41
+ "type": "string",
42
+ "optional": true
43
+ },
44
+ {
45
+ "name": "TotalRevenue",
46
+ "description": "Sum of all completed order amounts",
47
+ "type": "decimal",
48
+ "optional": false
49
+ },
50
+ {
51
+ "name": "OrderCount",
52
+ "description": "Total number of orders placed",
53
+ "type": "int",
54
+ "optional": false
55
+ }
56
+ ]
57
+ },
58
+ {
59
+ "name": "Monthly Sales Trend",
60
+ "userQuestion": "What is our monthly sales trend for the past year?",
61
+ "description": "Returns monthly revenue, order count, and average order value grouped by month",
62
+ "technicalDescription": "Aggregates sales data by month using DATEPART, calculates revenue and averages, orders chronologically",
63
+ "sql": "SELECT YEAR(OrderDate) AS Year, MONTH(OrderDate) AS Month, COUNT(ID) AS OrderCount, SUM(TotalAmount) AS Revenue, AVG(TotalAmount) AS AvgOrderValue FROM [dbo].[vwOrders] WHERE OrderDate >= DATEADD(YEAR, -1, GETDATE()) AND Status = 'Completed' GROUP BY YEAR(OrderDate), MONTH(OrderDate) ORDER BY Year, Month",
64
+ "parameters": [],
65
+ "selectClause": [
66
+ {
67
+ "name": "Year",
68
+ "description": "Year of the sales data",
69
+ "type": "int",
70
+ "optional": false
71
+ },
72
+ {
73
+ "name": "Month",
74
+ "description": "Month number (1-12)",
75
+ "type": "int",
76
+ "optional": false
77
+ },
78
+ {
79
+ "name": "OrderCount",
80
+ "description": "Number of orders in the month",
81
+ "type": "int",
82
+ "optional": false
83
+ },
84
+ {
85
+ "name": "Revenue",
86
+ "description": "Total revenue for the month",
87
+ "type": "decimal",
88
+ "optional": false
89
+ },
90
+ {
91
+ "name": "AvgOrderValue",
92
+ "description": "Average order value for the month",
93
+ "type": "decimal",
94
+ "optional": false
95
+ }
96
+ ]
97
+ },
98
+ {
99
+ "name": "Products Low in Stock",
100
+ "userQuestion": "Which products are running low on inventory?",
101
+ "description": "Returns products where current quantity is below the reorder threshold",
102
+ "technicalDescription": "Filters products by comparing QuantityOnHand with ReorderLevel, includes supplier info",
103
+ "sql": "SELECT p.ID, p.Name, p.SKU, p.QuantityOnHand, p.ReorderLevel, p.UnitPrice, s.Name AS SupplierName, s.Email AS SupplierEmail FROM [dbo].[vwProducts] p LEFT JOIN [dbo].[vwSuppliers] s ON p.SupplierID = s.ID WHERE p.QuantityOnHand <= p.ReorderLevel AND p.IsActive = 1 ORDER BY p.QuantityOnHand ASC",
104
+ "parameters": [],
105
+ "selectClause": [
106
+ {
107
+ "name": "ID",
108
+ "description": "Product unique identifier",
109
+ "type": "string",
110
+ "optional": false
111
+ },
112
+ {
113
+ "name": "Name",
114
+ "description": "Product name",
115
+ "type": "string",
116
+ "optional": false
117
+ },
118
+ {
119
+ "name": "SKU",
120
+ "description": "Product stock keeping unit",
121
+ "type": "string",
122
+ "optional": false
123
+ },
124
+ {
125
+ "name": "QuantityOnHand",
126
+ "description": "Current inventory quantity",
127
+ "type": "int",
128
+ "optional": false
129
+ },
130
+ {
131
+ "name": "ReorderLevel",
132
+ "description": "Minimum quantity before reorder needed",
133
+ "type": "int",
134
+ "optional": false
135
+ },
136
+ {
137
+ "name": "UnitPrice",
138
+ "description": "Price per unit",
139
+ "type": "decimal",
140
+ "optional": false
141
+ },
142
+ {
143
+ "name": "SupplierName",
144
+ "description": "Name of the supplier",
145
+ "type": "string",
146
+ "optional": true
147
+ },
148
+ {
149
+ "name": "SupplierEmail",
150
+ "description": "Supplier contact email",
151
+ "type": "string",
152
+ "optional": true
153
+ }
154
+ ]
155
+ },
156
+ {
157
+ "name": "Customer Order History",
158
+ "userQuestion": "What is the complete order history for a specific customer?",
159
+ "description": "Returns all orders for a customer with details and line items",
160
+ "technicalDescription": "Joins orders and order items by customer ID, includes product details and totals",
161
+ "sql": "SELECT o.ID AS OrderID, o.OrderDate, o.Status, o.TotalAmount, oi.ProductID, p.Name AS ProductName, oi.Quantity, oi.UnitPrice, oi.LineTotal FROM [dbo].[vwOrders] o INNER JOIN [dbo].[vwOrderItems] oi ON o.ID = oi.OrderID INNER JOIN [dbo].[vwProducts] p ON oi.ProductID = p.ID WHERE o.CustomerID = {{ customerId | sqlString }} ORDER BY o.OrderDate DESC, oi.LineNumber ASC",
162
+ "parameters": [
163
+ {
164
+ "name": "customerId",
165
+ "type": "string",
166
+ "isRequired": true,
167
+ "description": "Unique identifier of the customer",
168
+ "usage": ["WHERE clause for customer filtering"],
169
+ "defaultValue": null,
170
+ "sampleValue": "CUST-12345"
171
+ }
172
+ ],
173
+ "selectClause": [
174
+ {
175
+ "name": "OrderID",
176
+ "description": "Order unique identifier",
177
+ "type": "string",
178
+ "optional": false
179
+ },
180
+ {
181
+ "name": "OrderDate",
182
+ "description": "Date the order was placed",
183
+ "type": "date",
184
+ "optional": false
185
+ },
186
+ {
187
+ "name": "Status",
188
+ "description": "Current order status",
189
+ "type": "string",
190
+ "optional": false
191
+ },
192
+ {
193
+ "name": "TotalAmount",
194
+ "description": "Total order amount",
195
+ "type": "decimal",
196
+ "optional": false
197
+ },
198
+ {
199
+ "name": "ProductID",
200
+ "description": "Product identifier",
201
+ "type": "string",
202
+ "optional": false
203
+ },
204
+ {
205
+ "name": "ProductName",
206
+ "description": "Name of the ordered product",
207
+ "type": "string",
208
+ "optional": false
209
+ },
210
+ {
211
+ "name": "Quantity",
212
+ "description": "Quantity ordered",
213
+ "type": "int",
214
+ "optional": false
215
+ },
216
+ {
217
+ "name": "UnitPrice",
218
+ "description": "Price per unit at time of order",
219
+ "type": "decimal",
220
+ "optional": false
221
+ },
222
+ {
223
+ "name": "LineTotal",
224
+ "description": "Total for this line item",
225
+ "type": "decimal",
226
+ "optional": false
227
+ }
228
+ ]
229
+ },
230
+ {
231
+ "name": "Revenue by Product Category",
232
+ "userQuestion": "What is our revenue breakdown by product category?",
233
+ "description": "Returns total revenue, order count, and units sold for each product category",
234
+ "technicalDescription": "Aggregates order items by product category, calculates revenue and quantities",
235
+ "sql": "SELECT pc.ID, pc.Name AS CategoryName, COUNT(DISTINCT o.ID) AS OrderCount, SUM(oi.Quantity) AS UnitsSold, SUM(oi.LineTotal) AS Revenue FROM [dbo].[vwProductCategories] pc INNER JOIN [dbo].[vwProducts] p ON pc.ID = p.CategoryID INNER JOIN [dbo].[vwOrderItems] oi ON p.ID = oi.ProductID INNER JOIN [dbo].[vwOrders] o ON oi.OrderID = o.ID WHERE o.Status = 'Completed' AND o.OrderDate BETWEEN {{ startDate | sqlString }} AND {{ endDate | sqlString }} GROUP BY pc.ID, pc.Name ORDER BY Revenue DESC",
236
+ "parameters": [
237
+ {
238
+ "name": "startDate",
239
+ "type": "date",
240
+ "isRequired": true,
241
+ "description": "Start date of analysis period",
242
+ "usage": ["WHERE clause date range filter"],
243
+ "defaultValue": null,
244
+ "sampleValue": "2024-01-01"
245
+ },
246
+ {
247
+ "name": "endDate",
248
+ "type": "date",
249
+ "isRequired": true,
250
+ "description": "End date of analysis period",
251
+ "usage": ["WHERE clause date range filter"],
252
+ "defaultValue": null,
253
+ "sampleValue": "2024-12-31"
254
+ }
255
+ ],
256
+ "selectClause": [
257
+ {
258
+ "name": "ID",
259
+ "description": "Category unique identifier",
260
+ "type": "string",
261
+ "optional": false
262
+ },
263
+ {
264
+ "name": "CategoryName",
265
+ "description": "Product category name",
266
+ "type": "string",
267
+ "optional": false
268
+ },
269
+ {
270
+ "name": "OrderCount",
271
+ "description": "Number of orders containing this category",
272
+ "type": "int",
273
+ "optional": false
274
+ },
275
+ {
276
+ "name": "UnitsSold",
277
+ "description": "Total units sold in this category",
278
+ "type": "int",
279
+ "optional": false
280
+ },
281
+ {
282
+ "name": "Revenue",
283
+ "description": "Total revenue for this category",
284
+ "type": "decimal",
285
+ "optional": false
286
+ }
287
+ ]
288
+ },
289
+ {
290
+ "name": "Pending Orders Report",
291
+ "userQuestion": "What orders are currently pending or in process?",
292
+ "description": "Returns all orders that are not yet completed with customer and shipping details",
293
+ "technicalDescription": "Filters orders by status, joins customer info, ordered by oldest first",
294
+ "sql": "SELECT o.ID, o.OrderDate, o.Status, o.TotalAmount, c.Name AS CustomerName, c.Email, c.Phone, o.ShippingAddress, o.EstimatedDeliveryDate, DATEDIFF(DAY, o.OrderDate, GETDATE()) AS DaysPending FROM [dbo].[vwOrders] o INNER JOIN [dbo].[vwCustomers] c ON o.CustomerID = c.ID WHERE o.Status IN ('Pending', 'Processing', 'Shipped') ORDER BY o.OrderDate ASC",
295
+ "parameters": [],
296
+ "selectClause": [
297
+ {
298
+ "name": "ID",
299
+ "description": "Order unique identifier",
300
+ "type": "string",
301
+ "optional": false
302
+ },
303
+ {
304
+ "name": "OrderDate",
305
+ "description": "Date order was placed",
306
+ "type": "date",
307
+ "optional": false
308
+ },
309
+ {
310
+ "name": "Status",
311
+ "description": "Current order status",
312
+ "type": "string",
313
+ "optional": false
314
+ },
315
+ {
316
+ "name": "TotalAmount",
317
+ "description": "Order total amount",
318
+ "type": "decimal",
319
+ "optional": false
320
+ },
321
+ {
322
+ "name": "CustomerName",
323
+ "description": "Customer name",
324
+ "type": "string",
325
+ "optional": false
326
+ },
327
+ {
328
+ "name": "Email",
329
+ "description": "Customer email",
330
+ "type": "string",
331
+ "optional": false
332
+ },
333
+ {
334
+ "name": "Phone",
335
+ "description": "Customer phone",
336
+ "type": "string",
337
+ "optional": true
338
+ },
339
+ {
340
+ "name": "ShippingAddress",
341
+ "description": "Shipping address",
342
+ "type": "string",
343
+ "optional": true
344
+ },
345
+ {
346
+ "name": "EstimatedDeliveryDate",
347
+ "description": "Expected delivery date",
348
+ "type": "date",
349
+ "optional": true
350
+ },
351
+ {
352
+ "name": "DaysPending",
353
+ "description": "Number of days since order was placed",
354
+ "type": "int",
355
+ "optional": false
356
+ }
357
+ ]
358
+ },
359
+ {
360
+ "name": "Employee Sales Performance",
361
+ "userQuestion": "What is each salesperson's performance this quarter?",
362
+ "description": "Returns sales metrics for each employee including revenue, order count, and average deal size",
363
+ "technicalDescription": "Aggregates orders by assigned employee, calculates totals and averages for current quarter",
364
+ "sql": "SELECT e.ID, e.FirstName, e.LastName, e.Department, COUNT(o.ID) AS OrderCount, SUM(o.TotalAmount) AS TotalRevenue, AVG(o.TotalAmount) AS AvgOrderValue, MIN(o.OrderDate) AS FirstSale, MAX(o.OrderDate) AS LastSale FROM [dbo].[vwEmployees] e LEFT JOIN [dbo].[vwOrders] o ON e.ID = o.SalesRepID AND o.Status = 'Completed' AND o.OrderDate >= DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()), 0) WHERE e.Department = 'Sales' AND e.IsActive = 1 GROUP BY e.ID, e.FirstName, e.LastName, e.Department ORDER BY TotalRevenue DESC",
365
+ "parameters": [],
366
+ "selectClause": [
367
+ {
368
+ "name": "ID",
369
+ "description": "Employee unique identifier",
370
+ "type": "string",
371
+ "optional": false
372
+ },
373
+ {
374
+ "name": "FirstName",
375
+ "description": "Employee first name",
376
+ "type": "string",
377
+ "optional": false
378
+ },
379
+ {
380
+ "name": "LastName",
381
+ "description": "Employee last name",
382
+ "type": "string",
383
+ "optional": false
384
+ },
385
+ {
386
+ "name": "Department",
387
+ "description": "Employee department",
388
+ "type": "string",
389
+ "optional": false
390
+ },
391
+ {
392
+ "name": "OrderCount",
393
+ "description": "Number of orders closed",
394
+ "type": "int",
395
+ "optional": false
396
+ },
397
+ {
398
+ "name": "TotalRevenue",
399
+ "description": "Total revenue generated",
400
+ "type": "decimal",
401
+ "optional": false
402
+ },
403
+ {
404
+ "name": "AvgOrderValue",
405
+ "description": "Average order value",
406
+ "type": "decimal",
407
+ "optional": false
408
+ },
409
+ {
410
+ "name": "FirstSale",
411
+ "description": "Date of first sale this quarter",
412
+ "type": "date",
413
+ "optional": true
414
+ },
415
+ {
416
+ "name": "LastSale",
417
+ "description": "Date of most recent sale",
418
+ "type": "date",
419
+ "optional": true
420
+ }
421
+ ]
422
+ },
423
+ {
424
+ "name": "Product Performance Analysis",
425
+ "userQuestion": "Which products are our best and worst performers?",
426
+ "description": "Returns products with sales metrics, profit margins, and inventory turnover",
427
+ "technicalDescription": "Joins products with order items, calculates sales, profit, and turnover rate",
428
+ "sql": "SELECT p.ID, p.Name, p.SKU, p.Category, COUNT(DISTINCT oi.OrderID) AS OrderCount, SUM(oi.Quantity) AS UnitsSold, SUM(oi.LineTotal) AS Revenue, SUM((oi.UnitPrice - p.Cost) * oi.Quantity) AS GrossProfit, (SUM((oi.UnitPrice - p.Cost) * oi.Quantity) / NULLIF(SUM(oi.LineTotal), 0)) * 100 AS ProfitMarginPct, p.QuantityOnHand, CASE WHEN p.QuantityOnHand > 0 THEN CAST(SUM(oi.Quantity) AS DECIMAL) / p.QuantityOnHand ELSE NULL END AS TurnoverRate FROM [dbo].[vwProducts] p LEFT JOIN [dbo].[vwOrderItems] oi ON p.ID = oi.ProductID LEFT JOIN [dbo].[vwOrders] o ON oi.OrderID = o.ID AND o.Status = 'Completed' AND o.OrderDate >= {{ startDate | sqlString }} WHERE p.IsActive = 1 GROUP BY p.ID, p.Name, p.SKU, p.Category, p.Cost, p.QuantityOnHand ORDER BY Revenue DESC",
429
+ "parameters": [
430
+ {
431
+ "name": "startDate",
432
+ "type": "date",
433
+ "isRequired": true,
434
+ "description": "Start date for performance analysis",
435
+ "usage": ["WHERE clause date filter"],
436
+ "defaultValue": null,
437
+ "sampleValue": "2024-01-01"
438
+ }
439
+ ],
440
+ "selectClause": [
441
+ {
442
+ "name": "ID",
443
+ "description": "Product unique identifier",
444
+ "type": "string",
445
+ "optional": false
446
+ },
447
+ {
448
+ "name": "Name",
449
+ "description": "Product name",
450
+ "type": "string",
451
+ "optional": false
452
+ },
453
+ {
454
+ "name": "SKU",
455
+ "description": "Product SKU",
456
+ "type": "string",
457
+ "optional": false
458
+ },
459
+ {
460
+ "name": "Category",
461
+ "description": "Product category",
462
+ "type": "string",
463
+ "optional": true
464
+ },
465
+ {
466
+ "name": "OrderCount",
467
+ "description": "Number of orders containing this product",
468
+ "type": "int",
469
+ "optional": false
470
+ },
471
+ {
472
+ "name": "UnitsSold",
473
+ "description": "Total units sold",
474
+ "type": "int",
475
+ "optional": false
476
+ },
477
+ {
478
+ "name": "Revenue",
479
+ "description": "Total revenue generated",
480
+ "type": "decimal",
481
+ "optional": false
482
+ },
483
+ {
484
+ "name": "GrossProfit",
485
+ "description": "Total gross profit",
486
+ "type": "decimal",
487
+ "optional": false
488
+ },
489
+ {
490
+ "name": "ProfitMarginPct",
491
+ "description": "Profit margin percentage",
492
+ "type": "decimal",
493
+ "optional": false
494
+ },
495
+ {
496
+ "name": "QuantityOnHand",
497
+ "description": "Current inventory level",
498
+ "type": "int",
499
+ "optional": false
500
+ },
501
+ {
502
+ "name": "TurnoverRate",
503
+ "description": "Inventory turnover rate",
504
+ "type": "decimal",
505
+ "optional": true
506
+ }
507
+ ]
508
+ },
509
+ {
510
+ "name": "Customer Segmentation by Purchase Frequency",
511
+ "userQuestion": "How can we segment customers by their purchase frequency?",
512
+ "description": "Returns customers grouped by purchase frequency with lifetime value",
513
+ "technicalDescription": "Aggregates orders per customer, categorizes by frequency, calculates metrics",
514
+ "sql": "SELECT c.ID, c.Name, c.Email, COUNT(o.ID) AS TotalOrders, SUM(o.TotalAmount) AS LifetimeValue, AVG(o.TotalAmount) AS AvgOrderValue, MIN(o.OrderDate) AS FirstOrderDate, MAX(o.OrderDate) AS LastOrderDate, DATEDIFF(DAY, MAX(o.OrderDate), GETDATE()) AS DaysSinceLastOrder, CASE WHEN COUNT(o.ID) >= 10 THEN 'Frequent' WHEN COUNT(o.ID) >= 5 THEN 'Regular' WHEN COUNT(o.ID) >= 2 THEN 'Occasional' ELSE 'One-Time' END AS Segment FROM [dbo].[vwCustomers] c LEFT JOIN [dbo].[vwOrders] o ON c.ID = o.CustomerID AND o.Status = 'Completed' WHERE c.IsActive = 1 GROUP BY c.ID, c.Name, c.Email ORDER BY TotalOrders DESC",
515
+ "parameters": [],
516
+ "selectClause": [
517
+ {
518
+ "name": "ID",
519
+ "description": "Customer unique identifier",
520
+ "type": "string",
521
+ "optional": false
522
+ },
523
+ {
524
+ "name": "Name",
525
+ "description": "Customer name",
526
+ "type": "string",
527
+ "optional": false
528
+ },
529
+ {
530
+ "name": "Email",
531
+ "description": "Customer email",
532
+ "type": "string",
533
+ "optional": false
534
+ },
535
+ {
536
+ "name": "TotalOrders",
537
+ "description": "Total number of orders",
538
+ "type": "int",
539
+ "optional": false
540
+ },
541
+ {
542
+ "name": "LifetimeValue",
543
+ "description": "Total amount spent",
544
+ "type": "decimal",
545
+ "optional": false
546
+ },
547
+ {
548
+ "name": "AvgOrderValue",
549
+ "description": "Average order value",
550
+ "type": "decimal",
551
+ "optional": false
552
+ },
553
+ {
554
+ "name": "FirstOrderDate",
555
+ "description": "Date of first order",
556
+ "type": "date",
557
+ "optional": true
558
+ },
559
+ {
560
+ "name": "LastOrderDate",
561
+ "description": "Date of most recent order",
562
+ "type": "date",
563
+ "optional": true
564
+ },
565
+ {
566
+ "name": "DaysSinceLastOrder",
567
+ "description": "Days since last purchase",
568
+ "type": "int",
569
+ "optional": true
570
+ },
571
+ {
572
+ "name": "Segment",
573
+ "description": "Customer frequency segment",
574
+ "type": "string",
575
+ "optional": false
576
+ }
577
+ ]
578
+ },
579
+ {
580
+ "name": "Supplier Performance Metrics",
581
+ "userQuestion": "How are our suppliers performing on delivery and quality?",
582
+ "description": "Returns supplier metrics including on-time delivery rate and defect rate",
583
+ "technicalDescription": "Aggregates purchase orders and returns by supplier, calculates performance KPIs",
584
+ "sql": "SELECT s.ID, s.Name, s.Email, s.Phone, COUNT(DISTINCT po.ID) AS TotalPurchaseOrders, SUM(po.TotalAmount) AS TotalSpend, AVG(DATEDIFF(DAY, po.OrderDate, po.DeliveryDate)) AS AvgDeliveryDays, SUM(CASE WHEN po.DeliveryDate <= po.ExpectedDeliveryDate THEN 1 ELSE 0 END) * 100.0 / NULLIF(COUNT(po.ID), 0) AS OnTimeDeliveryPct, COUNT(r.ID) AS ReturnCount, SUM(r.Quantity) AS UnitsReturned, SUM(r.Amount) AS ReturnAmount FROM [dbo].[vwSuppliers] s LEFT JOIN [dbo].[vwPurchaseOrders] po ON s.ID = po.SupplierID AND po.Status = 'Completed' AND po.OrderDate >= {{ startDate | sqlString }} LEFT JOIN [dbo].[vwReturns] r ON s.ID = r.SupplierID AND r.ReturnDate >= {{ startDate | sqlString }} WHERE s.IsActive = 1 GROUP BY s.ID, s.Name, s.Email, s.Phone ORDER BY TotalSpend DESC",
585
+ "parameters": [
586
+ {
587
+ "name": "startDate",
588
+ "type": "date",
589
+ "isRequired": true,
590
+ "description": "Start date for supplier performance period",
591
+ "usage": ["WHERE clause date filter"],
592
+ "defaultValue": null,
593
+ "sampleValue": "2024-01-01"
594
+ }
595
+ ],
596
+ "selectClause": [
597
+ {
598
+ "name": "ID",
599
+ "description": "Supplier unique identifier",
600
+ "type": "string",
601
+ "optional": false
602
+ },
603
+ {
604
+ "name": "Name",
605
+ "description": "Supplier name",
606
+ "type": "string",
607
+ "optional": false
608
+ },
609
+ {
610
+ "name": "Email",
611
+ "description": "Supplier email",
612
+ "type": "string",
613
+ "optional": false
614
+ },
615
+ {
616
+ "name": "Phone",
617
+ "description": "Supplier phone",
618
+ "type": "string",
619
+ "optional": true
620
+ },
621
+ {
622
+ "name": "TotalPurchaseOrders",
623
+ "description": "Number of purchase orders",
624
+ "type": "int",
625
+ "optional": false
626
+ },
627
+ {
628
+ "name": "TotalSpend",
629
+ "description": "Total amount spent with supplier",
630
+ "type": "decimal",
631
+ "optional": false
632
+ },
633
+ {
634
+ "name": "AvgDeliveryDays",
635
+ "description": "Average delivery time in days",
636
+ "type": "int",
637
+ "optional": true
638
+ },
639
+ {
640
+ "name": "OnTimeDeliveryPct",
641
+ "description": "On-time delivery percentage",
642
+ "type": "decimal",
643
+ "optional": true
644
+ },
645
+ {
646
+ "name": "ReturnCount",
647
+ "description": "Number of returns",
648
+ "type": "int",
649
+ "optional": false
650
+ },
651
+ {
652
+ "name": "UnitsReturned",
653
+ "description": "Total units returned",
654
+ "type": "int",
655
+ "optional": false
656
+ },
657
+ {
658
+ "name": "ReturnAmount",
659
+ "description": "Total value of returns",
660
+ "type": "decimal",
661
+ "optional": false
662
+ }
663
+ ]
664
+ },
665
+ {
666
+ "name": "Invoice Aging Report",
667
+ "userQuestion": "Which invoices are overdue and by how much?",
668
+ "description": "Returns unpaid invoices with aging buckets and customer contact info",
669
+ "technicalDescription": "Filters unpaid invoices, calculates days overdue, groups into aging buckets",
670
+ "sql": "SELECT i.ID, i.InvoiceNumber, i.InvoiceDate, i.DueDate, i.Amount, i.AmountPaid, i.Amount - i.AmountPaid AS Balance, DATEDIFF(DAY, i.DueDate, GETDATE()) AS DaysOverdue, CASE WHEN DATEDIFF(DAY, i.DueDate, GETDATE()) <= 0 THEN 'Current' WHEN DATEDIFF(DAY, i.DueDate, GETDATE()) <= 30 THEN '1-30 Days' WHEN DATEDIFF(DAY, i.DueDate, GETDATE()) <= 60 THEN '31-60 Days' WHEN DATEDIFF(DAY, i.DueDate, GETDATE()) <= 90 THEN '61-90 Days' ELSE '90+ Days' END AS AgingBucket, c.Name AS CustomerName, c.Email, c.Phone FROM [dbo].[vwInvoices] i INNER JOIN [dbo].[vwCustomers] c ON i.CustomerID = c.ID WHERE i.Status = 'Unpaid' OR (i.Status = 'Partial' AND i.Amount > i.AmountPaid) ORDER BY DaysOverdue DESC",
671
+ "parameters": [],
672
+ "selectClause": [
673
+ {
674
+ "name": "ID",
675
+ "description": "Invoice unique identifier",
676
+ "type": "string",
677
+ "optional": false
678
+ },
679
+ {
680
+ "name": "InvoiceNumber",
681
+ "description": "Invoice number",
682
+ "type": "string",
683
+ "optional": false
684
+ },
685
+ {
686
+ "name": "InvoiceDate",
687
+ "description": "Invoice date",
688
+ "type": "date",
689
+ "optional": false
690
+ },
691
+ {
692
+ "name": "DueDate",
693
+ "description": "Payment due date",
694
+ "type": "date",
695
+ "optional": false
696
+ },
697
+ {
698
+ "name": "Amount",
699
+ "description": "Total invoice amount",
700
+ "type": "decimal",
701
+ "optional": false
702
+ },
703
+ {
704
+ "name": "AmountPaid",
705
+ "description": "Amount already paid",
706
+ "type": "decimal",
707
+ "optional": false
708
+ },
709
+ {
710
+ "name": "Balance",
711
+ "description": "Remaining balance due",
712
+ "type": "decimal",
713
+ "optional": false
714
+ },
715
+ {
716
+ "name": "DaysOverdue",
717
+ "description": "Number of days overdue",
718
+ "type": "int",
719
+ "optional": false
720
+ },
721
+ {
722
+ "name": "AgingBucket",
723
+ "description": "Aging category",
724
+ "type": "string",
725
+ "optional": false
726
+ },
727
+ {
728
+ "name": "CustomerName",
729
+ "description": "Customer name",
730
+ "type": "string",
731
+ "optional": false
732
+ },
733
+ {
734
+ "name": "Email",
735
+ "description": "Customer email",
736
+ "type": "string",
737
+ "optional": false
738
+ },
739
+ {
740
+ "name": "Phone",
741
+ "description": "Customer phone",
742
+ "type": "string",
743
+ "optional": true
744
+ }
745
+ ]
746
+ },
747
+ {
748
+ "name": "New Customer Acquisition by Source",
749
+ "userQuestion": "Where are our new customers coming from?",
750
+ "description": "Returns customer acquisition metrics by marketing source and channel",
751
+ "technicalDescription": "Groups new customers by acquisition source, calculates counts and revenue",
752
+ "sql": "SELECT c.AcquisitionSource, c.AcquisitionChannel, COUNT(c.ID) AS NewCustomers, SUM(o.TotalAmount) AS Revenue, AVG(o.TotalAmount) AS AvgFirstOrderValue, MIN(c.CreatedAt) AS FirstCustomer, MAX(c.CreatedAt) AS LastCustomer FROM [dbo].[vwCustomers] c LEFT JOIN [dbo].[vwOrders] o ON c.ID = o.CustomerID AND o.OrderNumber = 1 WHERE c.CreatedAt BETWEEN {{ startDate | sqlString }} AND {{ endDate | sqlString }} GROUP BY c.AcquisitionSource, c.AcquisitionChannel ORDER BY NewCustomers DESC",
753
+ "parameters": [
754
+ {
755
+ "name": "startDate",
756
+ "type": "date",
757
+ "isRequired": true,
758
+ "description": "Start date for acquisition period",
759
+ "usage": ["WHERE clause date range filter"],
760
+ "defaultValue": null,
761
+ "sampleValue": "2024-01-01"
762
+ },
763
+ {
764
+ "name": "endDate",
765
+ "type": "date",
766
+ "isRequired": true,
767
+ "description": "End date for acquisition period",
768
+ "usage": ["WHERE clause date range filter"],
769
+ "defaultValue": null,
770
+ "sampleValue": "2024-12-31"
771
+ }
772
+ ],
773
+ "selectClause": [
774
+ {
775
+ "name": "AcquisitionSource",
776
+ "description": "Marketing source (Google Ads, Facebook, etc)",
777
+ "type": "string",
778
+ "optional": true
779
+ },
780
+ {
781
+ "name": "AcquisitionChannel",
782
+ "description": "Marketing channel (Paid Search, Social, etc)",
783
+ "type": "string",
784
+ "optional": true
785
+ },
786
+ {
787
+ "name": "NewCustomers",
788
+ "description": "Number of new customers acquired",
789
+ "type": "int",
790
+ "optional": false
791
+ },
792
+ {
793
+ "name": "Revenue",
794
+ "description": "Revenue from first orders",
795
+ "type": "decimal",
796
+ "optional": false
797
+ },
798
+ {
799
+ "name": "AvgFirstOrderValue",
800
+ "description": "Average first order value",
801
+ "type": "decimal",
802
+ "optional": false
803
+ },
804
+ {
805
+ "name": "FirstCustomer",
806
+ "description": "Date of first customer in period",
807
+ "type": "date",
808
+ "optional": true
809
+ },
810
+ {
811
+ "name": "LastCustomer",
812
+ "description": "Date of most recent customer",
813
+ "type": "date",
814
+ "optional": true
815
+ }
816
+ ]
817
+ },
818
+ {
819
+ "name": "Abandoned Cart Analysis",
820
+ "userQuestion": "What items are frequently left in abandoned shopping carts?",
821
+ "description": "Returns products most commonly found in abandoned carts with values",
822
+ "technicalDescription": "Joins abandoned carts with cart items, aggregates by product, orders by frequency",
823
+ "sql": "SELECT p.ID, p.Name, p.Category, p.UnitPrice, COUNT(DISTINCT ci.CartID) AS AbandonedCount, SUM(ci.Quantity) AS TotalQuantity, SUM(ci.Quantity * ci.UnitPrice) AS PotentialRevenue, AVG(ci.Quantity) AS AvgQuantity FROM [dbo].[vwProducts] p INNER JOIN [dbo].[vwCartItems] ci ON p.ID = ci.ProductID INNER JOIN [dbo].[vwShoppingCarts] sc ON ci.CartID = sc.ID WHERE sc.Status = 'Abandoned' AND sc.LastModified >= {{ startDate | sqlString }} GROUP BY p.ID, p.Name, p.Category, p.UnitPrice ORDER BY AbandonedCount DESC",
824
+ "parameters": [
825
+ {
826
+ "name": "startDate",
827
+ "type": "date",
828
+ "isRequired": true,
829
+ "description": "Start date for cart analysis",
830
+ "usage": ["WHERE clause date filter"],
831
+ "defaultValue": null,
832
+ "sampleValue": "2024-01-01"
833
+ }
834
+ ],
835
+ "selectClause": [
836
+ {
837
+ "name": "ID",
838
+ "description": "Product unique identifier",
839
+ "type": "string",
840
+ "optional": false
841
+ },
842
+ {
843
+ "name": "Name",
844
+ "description": "Product name",
845
+ "type": "string",
846
+ "optional": false
847
+ },
848
+ {
849
+ "name": "Category",
850
+ "description": "Product category",
851
+ "type": "string",
852
+ "optional": true
853
+ },
854
+ {
855
+ "name": "UnitPrice",
856
+ "description": "Current unit price",
857
+ "type": "decimal",
858
+ "optional": false
859
+ },
860
+ {
861
+ "name": "AbandonedCount",
862
+ "description": "Number of abandoned carts containing this product",
863
+ "type": "int",
864
+ "optional": false
865
+ },
866
+ {
867
+ "name": "TotalQuantity",
868
+ "description": "Total quantity in abandoned carts",
869
+ "type": "int",
870
+ "optional": false
871
+ },
872
+ {
873
+ "name": "PotentialRevenue",
874
+ "description": "Potential revenue from abandoned items",
875
+ "type": "decimal",
876
+ "optional": false
877
+ },
878
+ {
879
+ "name": "AvgQuantity",
880
+ "description": "Average quantity per abandoned cart",
881
+ "type": "decimal",
882
+ "optional": false
883
+ }
884
+ ]
885
+ },
886
+ {
887
+ "name": "Shipping Performance by Carrier",
888
+ "userQuestion": "Which shipping carriers are performing best?",
889
+ "description": "Returns carrier performance metrics including delivery time and cost",
890
+ "technicalDescription": "Aggregates shipments by carrier, calculates delivery times and costs",
891
+ "sql": "SELECT sh.CarrierName, COUNT(sh.ID) AS TotalShipments, AVG(DATEDIFF(DAY, sh.ShipDate, sh.DeliveryDate)) AS AvgDeliveryDays, SUM(CASE WHEN sh.DeliveryDate <= sh.EstimatedDeliveryDate THEN 1 ELSE 0 END) * 100.0 / NULLIF(COUNT(sh.ID), 0) AS OnTimePct, AVG(sh.ShippingCost) AS AvgShippingCost, SUM(sh.ShippingCost) AS TotalShippingCost, COUNT(CASE WHEN sh.Status = 'Damaged' OR sh.Status = 'Lost' THEN 1 END) AS IssueCount FROM [dbo].[vwShipments] sh WHERE sh.ShipDate >= {{ startDate | sqlString }} AND sh.Status IN ('Delivered', 'Damaged', 'Lost') GROUP BY sh.CarrierName ORDER BY TotalShipments DESC",
892
+ "parameters": [
893
+ {
894
+ "name": "startDate",
895
+ "type": "date",
896
+ "isRequired": true,
897
+ "description": "Start date for shipping analysis",
898
+ "usage": ["WHERE clause date filter"],
899
+ "defaultValue": null,
900
+ "sampleValue": "2024-01-01"
901
+ }
902
+ ],
903
+ "selectClause": [
904
+ {
905
+ "name": "CarrierName",
906
+ "description": "Shipping carrier name",
907
+ "type": "string",
908
+ "optional": false
909
+ },
910
+ {
911
+ "name": "TotalShipments",
912
+ "description": "Number of shipments",
913
+ "type": "int",
914
+ "optional": false
915
+ },
916
+ {
917
+ "name": "AvgDeliveryDays",
918
+ "description": "Average delivery time in days",
919
+ "type": "int",
920
+ "optional": false
921
+ },
922
+ {
923
+ "name": "OnTimePct",
924
+ "description": "On-time delivery percentage",
925
+ "type": "decimal",
926
+ "optional": false
927
+ },
928
+ {
929
+ "name": "AvgShippingCost",
930
+ "description": "Average shipping cost per shipment",
931
+ "type": "decimal",
932
+ "optional": false
933
+ },
934
+ {
935
+ "name": "TotalShippingCost",
936
+ "description": "Total shipping cost",
937
+ "type": "decimal",
938
+ "optional": false
939
+ },
940
+ {
941
+ "name": "IssueCount",
942
+ "description": "Number of damaged or lost shipments",
943
+ "type": "int",
944
+ "optional": false
945
+ }
946
+ ]
947
+ },
948
+ {
949
+ "name": "Product Return Rate Analysis",
950
+ "userQuestion": "Which products have the highest return rates?",
951
+ "description": "Returns products with return rates, reasons, and impact on revenue",
952
+ "technicalDescription": "Calculates return rate by comparing returns to sales for each product",
953
+ "sql": "SELECT p.ID, p.Name, p.Category, COUNT(DISTINCT oi.OrderID) AS UnitsSold, COUNT(DISTINCT r.ID) AS UnitsReturned, (COUNT(DISTINCT r.ID) * 100.0 / NULLIF(COUNT(DISTINCT oi.OrderID), 0)) AS ReturnRatePct, SUM(r.Amount) AS ReturnAmount, STRING_AGG(r.Reason, ', ') AS ReturnReasons FROM [dbo].[vwProducts] p LEFT JOIN [dbo].[vwOrderItems] oi ON p.ID = oi.ProductID LEFT JOIN [dbo].[vwOrders] o ON oi.OrderID = o.ID AND o.Status = 'Completed' AND o.OrderDate >= {{ startDate | sqlString }} LEFT JOIN [dbo].[vwReturns] r ON oi.ID = r.OrderItemID WHERE p.IsActive = 1 GROUP BY p.ID, p.Name, p.Category HAVING COUNT(DISTINCT r.ID) > 0 ORDER BY ReturnRatePct DESC",
954
+ "parameters": [
955
+ {
956
+ "name": "startDate",
957
+ "type": "date",
958
+ "isRequired": true,
959
+ "description": "Start date for return analysis",
960
+ "usage": ["WHERE clause date filter"],
961
+ "defaultValue": null,
962
+ "sampleValue": "2024-01-01"
963
+ }
964
+ ],
965
+ "selectClause": [
966
+ {
967
+ "name": "ID",
968
+ "description": "Product unique identifier",
969
+ "type": "string",
970
+ "optional": false
971
+ },
972
+ {
973
+ "name": "Name",
974
+ "description": "Product name",
975
+ "type": "string",
976
+ "optional": false
977
+ },
978
+ {
979
+ "name": "Category",
980
+ "description": "Product category",
981
+ "type": "string",
982
+ "optional": true
983
+ },
984
+ {
985
+ "name": "UnitsSold",
986
+ "description": "Number of units sold",
987
+ "type": "int",
988
+ "optional": false
989
+ },
990
+ {
991
+ "name": "UnitsReturned",
992
+ "description": "Number of units returned",
993
+ "type": "int",
994
+ "optional": false
995
+ },
996
+ {
997
+ "name": "ReturnRatePct",
998
+ "description": "Return rate percentage",
999
+ "type": "decimal",
1000
+ "optional": false
1001
+ },
1002
+ {
1003
+ "name": "ReturnAmount",
1004
+ "description": "Total value of returns",
1005
+ "type": "decimal",
1006
+ "optional": false
1007
+ },
1008
+ {
1009
+ "name": "ReturnReasons",
1010
+ "description": "Comma-separated list of return reasons",
1011
+ "type": "string",
1012
+ "optional": true
1013
+ }
1014
+ ]
1015
+ },
1016
+ {
1017
+ "name": "Customer Lifetime Value Projection",
1018
+ "userQuestion": "What is the projected lifetime value of our customer segments?",
1019
+ "description": "Returns customer segments with historical metrics and LTV projections",
1020
+ "technicalDescription": "Segments customers, calculates historical metrics, projects future value",
1021
+ "sql": "SELECT CASE WHEN TotalOrders >= 10 THEN 'VIP' WHEN TotalOrders >= 5 THEN 'Loyal' WHEN TotalOrders >= 2 THEN 'Growing' ELSE 'New' END AS Segment, COUNT(CustomerID) AS CustomerCount, AVG(TotalOrders) AS AvgOrders, AVG(TotalRevenue) AS AvgHistoricalValue, AVG(AvgOrderValue) AS AvgOrderSize, AVG(DaysBetweenOrders) AS AvgDaysBetweenOrders, AVG(TotalRevenue * (365.0 / NULLIF(DaysBetweenOrders, 0)) * 3) AS ProjectedLTV3Year FROM (SELECT c.ID AS CustomerID, COUNT(o.ID) AS TotalOrders, SUM(o.TotalAmount) AS TotalRevenue, AVG(o.TotalAmount) AS AvgOrderValue, DATEDIFF(DAY, MIN(o.OrderDate), MAX(o.OrderDate)) / NULLIF(COUNT(o.ID) - 1, 0) AS DaysBetweenOrders FROM [dbo].[vwCustomers] c LEFT JOIN [dbo].[vwOrders] o ON c.ID = o.CustomerID AND o.Status = 'Completed' WHERE c.IsActive = 1 GROUP BY c.ID) AS CustomerMetrics GROUP BY CASE WHEN TotalOrders >= 10 THEN 'VIP' WHEN TotalOrders >= 5 THEN 'Loyal' WHEN TotalOrders >= 2 THEN 'Growing' ELSE 'New' END ORDER BY AvgHistoricalValue DESC",
1022
+ "parameters": [],
1023
+ "selectClause": [
1024
+ {
1025
+ "name": "Segment",
1026
+ "description": "Customer segment name",
1027
+ "type": "string",
1028
+ "optional": false
1029
+ },
1030
+ {
1031
+ "name": "CustomerCount",
1032
+ "description": "Number of customers in segment",
1033
+ "type": "int",
1034
+ "optional": false
1035
+ },
1036
+ {
1037
+ "name": "AvgOrders",
1038
+ "description": "Average number of orders",
1039
+ "type": "decimal",
1040
+ "optional": false
1041
+ },
1042
+ {
1043
+ "name": "AvgHistoricalValue",
1044
+ "description": "Average historical revenue per customer",
1045
+ "type": "decimal",
1046
+ "optional": false
1047
+ },
1048
+ {
1049
+ "name": "AvgOrderSize",
1050
+ "description": "Average order value",
1051
+ "type": "decimal",
1052
+ "optional": false
1053
+ },
1054
+ {
1055
+ "name": "AvgDaysBetweenOrders",
1056
+ "description": "Average days between orders",
1057
+ "type": "int",
1058
+ "optional": true
1059
+ },
1060
+ {
1061
+ "name": "ProjectedLTV3Year",
1062
+ "description": "Projected 3-year lifetime value",
1063
+ "type": "decimal",
1064
+ "optional": true
1065
+ }
1066
+ ]
1067
+ },
1068
+ {
1069
+ "name": "Inventory Replenishment Schedule",
1070
+ "userQuestion": "Which products need to be reordered and when?",
1071
+ "description": "Returns products with reorder recommendations based on sales velocity",
1072
+ "technicalDescription": "Calculates sales velocity, current inventory, and projected stockout dates",
1073
+ "sql": "SELECT p.ID, p.Name, p.SKU, p.QuantityOnHand, p.ReorderLevel, p.LeadTimeDays, s.Name AS SupplierName, CAST(SUM(oi.Quantity) AS DECIMAL) / NULLIF(DATEDIFF(DAY, MIN(o.OrderDate), MAX(o.OrderDate)), 0) AS DailySalesVelocity, CAST(p.QuantityOnHand AS DECIMAL) / NULLIF(CAST(SUM(oi.Quantity) AS DECIMAL) / NULLIF(DATEDIFF(DAY, MIN(o.OrderDate), MAX(o.OrderDate)), 0), 0) AS DaysOfSupply, DATEADD(DAY, CAST(p.QuantityOnHand AS DECIMAL) / NULLIF(CAST(SUM(oi.Quantity) AS DECIMAL) / NULLIF(DATEDIFF(DAY, MIN(o.OrderDate), MAX(o.OrderDate)), 0), 0), GETDATE()) AS ProjectedStockoutDate, CASE WHEN CAST(p.QuantityOnHand AS DECIMAL) / NULLIF(CAST(SUM(oi.Quantity) AS DECIMAL) / NULLIF(DATEDIFF(DAY, MIN(o.OrderDate), MAX(o.OrderDate)), 0), 0) <= p.LeadTimeDays THEN 'Urgent' WHEN p.QuantityOnHand <= p.ReorderLevel THEN 'Soon' ELSE 'Normal' END AS Priority FROM [dbo].[vwProducts] p LEFT JOIN [dbo].[vwSuppliers] s ON p.SupplierID = s.ID LEFT JOIN [dbo].[vwOrderItems] oi ON p.ID = oi.ProductID LEFT JOIN [dbo].[vwOrders] o ON oi.OrderID = o.ID AND o.Status = 'Completed' AND o.OrderDate >= DATEADD(DAY, -90, GETDATE()) WHERE p.IsActive = 1 AND p.QuantityOnHand <= p.ReorderLevel * 1.5 GROUP BY p.ID, p.Name, p.SKU, p.QuantityOnHand, p.ReorderLevel, p.LeadTimeDays, s.Name ORDER BY Priority, DaysOfSupply ASC",
1074
+ "parameters": [],
1075
+ "selectClause": [
1076
+ {
1077
+ "name": "ID",
1078
+ "description": "Product unique identifier",
1079
+ "type": "string",
1080
+ "optional": false
1081
+ },
1082
+ {
1083
+ "name": "Name",
1084
+ "description": "Product name",
1085
+ "type": "string",
1086
+ "optional": false
1087
+ },
1088
+ {
1089
+ "name": "SKU",
1090
+ "description": "Product SKU",
1091
+ "type": "string",
1092
+ "optional": false
1093
+ },
1094
+ {
1095
+ "name": "QuantityOnHand",
1096
+ "description": "Current inventory quantity",
1097
+ "type": "int",
1098
+ "optional": false
1099
+ },
1100
+ {
1101
+ "name": "ReorderLevel",
1102
+ "description": "Reorder threshold",
1103
+ "type": "int",
1104
+ "optional": false
1105
+ },
1106
+ {
1107
+ "name": "LeadTimeDays",
1108
+ "description": "Supplier lead time in days",
1109
+ "type": "int",
1110
+ "optional": false
1111
+ },
1112
+ {
1113
+ "name": "SupplierName",
1114
+ "description": "Supplier name",
1115
+ "type": "string",
1116
+ "optional": true
1117
+ },
1118
+ {
1119
+ "name": "DailySalesVelocity",
1120
+ "description": "Average daily sales rate",
1121
+ "type": "decimal",
1122
+ "optional": true
1123
+ },
1124
+ {
1125
+ "name": "DaysOfSupply",
1126
+ "description": "Estimated days until stockout",
1127
+ "type": "decimal",
1128
+ "optional": true
1129
+ },
1130
+ {
1131
+ "name": "ProjectedStockoutDate",
1132
+ "description": "Estimated stockout date",
1133
+ "type": "date",
1134
+ "optional": true
1135
+ },
1136
+ {
1137
+ "name": "Priority",
1138
+ "description": "Reorder priority (Urgent/Soon/Normal)",
1139
+ "type": "string",
1140
+ "optional": false
1141
+ }
1142
+ ]
1143
+ },
1144
+ {
1145
+ "name": "Customer Churn Risk Analysis",
1146
+ "userQuestion": "Which customers are at risk of churning?",
1147
+ "description": "Returns customers showing signs of churn based on purchase patterns",
1148
+ "technicalDescription": "Identifies customers with declining engagement or long gaps since last order",
1149
+ "sql": "SELECT c.ID, c.Name, c.Email, c.Phone, COUNT(o.ID) AS TotalOrders, SUM(o.TotalAmount) AS LifetimeValue, MAX(o.OrderDate) AS LastOrderDate, DATEDIFF(DAY, MAX(o.OrderDate), GETDATE()) AS DaysSinceLastOrder, AVG(DATEDIFF(DAY, LAG(o.OrderDate) OVER (PARTITION BY c.ID ORDER BY o.OrderDate), o.OrderDate)) AS AvgDaysBetweenOrders, CASE WHEN DATEDIFF(DAY, MAX(o.OrderDate), GETDATE()) > AVG(DATEDIFF(DAY, LAG(o.OrderDate) OVER (PARTITION BY c.ID ORDER BY o.OrderDate), o.OrderDate)) * 2 THEN 'High Risk' WHEN DATEDIFF(DAY, MAX(o.OrderDate), GETDATE()) > AVG(DATEDIFF(DAY, LAG(o.OrderDate) OVER (PARTITION BY c.ID ORDER BY o.OrderDate), o.OrderDate)) * 1.5 THEN 'Medium Risk' ELSE 'Low Risk' END AS ChurnRisk FROM [dbo].[vwCustomers] c INNER JOIN [dbo].[vwOrders] o ON c.ID = o.CustomerID AND o.Status = 'Completed' WHERE c.IsActive = 1 GROUP BY c.ID, c.Name, c.Email, c.Phone HAVING COUNT(o.ID) >= 2 AND DATEDIFF(DAY, MAX(o.OrderDate), GETDATE()) > 30 ORDER BY ChurnRisk DESC, DaysSinceLastOrder DESC",
1150
+ "parameters": [],
1151
+ "selectClause": [
1152
+ {
1153
+ "name": "ID",
1154
+ "description": "Customer unique identifier",
1155
+ "type": "string",
1156
+ "optional": false
1157
+ },
1158
+ {
1159
+ "name": "Name",
1160
+ "description": "Customer name",
1161
+ "type": "string",
1162
+ "optional": false
1163
+ },
1164
+ {
1165
+ "name": "Email",
1166
+ "description": "Customer email",
1167
+ "type": "string",
1168
+ "optional": false
1169
+ },
1170
+ {
1171
+ "name": "Phone",
1172
+ "description": "Customer phone",
1173
+ "type": "string",
1174
+ "optional": true
1175
+ },
1176
+ {
1177
+ "name": "TotalOrders",
1178
+ "description": "Total number of orders",
1179
+ "type": "int",
1180
+ "optional": false
1181
+ },
1182
+ {
1183
+ "name": "LifetimeValue",
1184
+ "description": "Total lifetime value",
1185
+ "type": "decimal",
1186
+ "optional": false
1187
+ },
1188
+ {
1189
+ "name": "LastOrderDate",
1190
+ "description": "Date of most recent order",
1191
+ "type": "date",
1192
+ "optional": false
1193
+ },
1194
+ {
1195
+ "name": "DaysSinceLastOrder",
1196
+ "description": "Days since last purchase",
1197
+ "type": "int",
1198
+ "optional": false
1199
+ },
1200
+ {
1201
+ "name": "AvgDaysBetweenOrders",
1202
+ "description": "Average days between orders",
1203
+ "type": "int",
1204
+ "optional": true
1205
+ },
1206
+ {
1207
+ "name": "ChurnRisk",
1208
+ "description": "Churn risk level (High/Medium/Low)",
1209
+ "type": "string",
1210
+ "optional": false
1211
+ }
1212
+ ]
1213
+ },
1214
+ {
1215
+ "name": "Sales Commission Calculation",
1216
+ "userQuestion": "What are the sales commissions owed to each sales rep?",
1217
+ "description": "Returns commission amounts by sales rep based on completed orders and rates",
1218
+ "technicalDescription": "Aggregates orders by sales rep, applies commission rates, calculates totals",
1219
+ "sql": "SELECT e.ID, e.FirstName, e.LastName, e.CommissionRate, COUNT(o.ID) AS OrderCount, SUM(o.TotalAmount) AS TotalSales, SUM(o.TotalAmount * e.CommissionRate / 100) AS CommissionOwed, MIN(o.OrderDate) AS FirstSale, MAX(o.OrderDate) AS LastSale FROM [dbo].[vwEmployees] e INNER JOIN [dbo].[vwOrders] o ON e.ID = o.SalesRepID WHERE o.Status = 'Completed' AND o.OrderDate BETWEEN {{ startDate | sqlString }} AND {{ endDate | sqlString }} AND e.CommissionRate > 0 GROUP BY e.ID, e.FirstName, e.LastName, e.CommissionRate ORDER BY CommissionOwed DESC",
1220
+ "parameters": [
1221
+ {
1222
+ "name": "startDate",
1223
+ "type": "date",
1224
+ "isRequired": true,
1225
+ "description": "Start date of commission period",
1226
+ "usage": ["WHERE clause date range filter"],
1227
+ "defaultValue": null,
1228
+ "sampleValue": "2024-01-01"
1229
+ },
1230
+ {
1231
+ "name": "endDate",
1232
+ "type": "date",
1233
+ "isRequired": true,
1234
+ "description": "End date of commission period",
1235
+ "usage": ["WHERE clause date range filter"],
1236
+ "defaultValue": null,
1237
+ "sampleValue": "2024-01-31"
1238
+ }
1239
+ ],
1240
+ "selectClause": [
1241
+ {
1242
+ "name": "ID",
1243
+ "description": "Employee unique identifier",
1244
+ "type": "string",
1245
+ "optional": false
1246
+ },
1247
+ {
1248
+ "name": "FirstName",
1249
+ "description": "Employee first name",
1250
+ "type": "string",
1251
+ "optional": false
1252
+ },
1253
+ {
1254
+ "name": "LastName",
1255
+ "description": "Employee last name",
1256
+ "type": "string",
1257
+ "optional": false
1258
+ },
1259
+ {
1260
+ "name": "CommissionRate",
1261
+ "description": "Commission rate percentage",
1262
+ "type": "decimal",
1263
+ "optional": false
1264
+ },
1265
+ {
1266
+ "name": "OrderCount",
1267
+ "description": "Number of orders closed",
1268
+ "type": "int",
1269
+ "optional": false
1270
+ },
1271
+ {
1272
+ "name": "TotalSales",
1273
+ "description": "Total sales amount",
1274
+ "type": "decimal",
1275
+ "optional": false
1276
+ },
1277
+ {
1278
+ "name": "CommissionOwed",
1279
+ "description": "Total commission amount",
1280
+ "type": "decimal",
1281
+ "optional": false
1282
+ },
1283
+ {
1284
+ "name": "FirstSale",
1285
+ "description": "Date of first sale in period",
1286
+ "type": "date",
1287
+ "optional": true
1288
+ },
1289
+ {
1290
+ "name": "LastSale",
1291
+ "description": "Date of last sale in period",
1292
+ "type": "date",
1293
+ "optional": true
1294
+ }
1295
+ ]
1296
+ },
1297
+ {
1298
+ "name": "Profit Margin by Product Line",
1299
+ "userQuestion": "What are our profit margins across different product lines?",
1300
+ "description": "Returns profitability analysis by product category with cost breakdown",
1301
+ "technicalDescription": "Aggregates sales and costs by category, calculates margins and contribution",
1302
+ "sql": "SELECT pc.ID, pc.Name AS ProductLine, COUNT(DISTINCT p.ID) AS ProductCount, SUM(oi.Quantity) AS UnitsSold, SUM(oi.LineTotal) AS Revenue, SUM(oi.Quantity * p.Cost) AS TotalCost, SUM(oi.LineTotal - (oi.Quantity * p.Cost)) AS GrossProfit, (SUM(oi.LineTotal - (oi.Quantity * p.Cost)) / NULLIF(SUM(oi.LineTotal), 0)) * 100 AS GrossProfitMarginPct, SUM(oi.LineTotal) / NULLIF((SELECT SUM(LineTotal) FROM [dbo].[vwOrderItems] oi2 INNER JOIN [dbo].[vwOrders] o2 ON oi2.OrderID = o2.ID WHERE o2.Status = 'Completed' AND o2.OrderDate >= {{ startDate | sqlString }}), 0) * 100 AS RevenueContributionPct FROM [dbo].[vwProductCategories] pc INNER JOIN [dbo].[vwProducts] p ON pc.ID = p.CategoryID INNER JOIN [dbo].[vwOrderItems] oi ON p.ID = oi.ProductID INNER JOIN [dbo].[vwOrders] o ON oi.OrderID = o.ID WHERE o.Status = 'Completed' AND o.OrderDate >= {{ startDate | sqlString }} GROUP BY pc.ID, pc.Name ORDER BY GrossProfit DESC",
1303
+ "parameters": [
1304
+ {
1305
+ "name": "startDate",
1306
+ "type": "date",
1307
+ "isRequired": true,
1308
+ "description": "Start date for profitability analysis",
1309
+ "usage": ["WHERE clause date filter"],
1310
+ "defaultValue": null,
1311
+ "sampleValue": "2024-01-01"
1312
+ }
1313
+ ],
1314
+ "selectClause": [
1315
+ {
1316
+ "name": "ID",
1317
+ "description": "Product line unique identifier",
1318
+ "type": "string",
1319
+ "optional": false
1320
+ },
1321
+ {
1322
+ "name": "ProductLine",
1323
+ "description": "Product line name",
1324
+ "type": "string",
1325
+ "optional": false
1326
+ },
1327
+ {
1328
+ "name": "ProductCount",
1329
+ "description": "Number of products in line",
1330
+ "type": "int",
1331
+ "optional": false
1332
+ },
1333
+ {
1334
+ "name": "UnitsSold",
1335
+ "description": "Total units sold",
1336
+ "type": "int",
1337
+ "optional": false
1338
+ },
1339
+ {
1340
+ "name": "Revenue",
1341
+ "description": "Total revenue",
1342
+ "type": "decimal",
1343
+ "optional": false
1344
+ },
1345
+ {
1346
+ "name": "TotalCost",
1347
+ "description": "Total cost of goods sold",
1348
+ "type": "decimal",
1349
+ "optional": false
1350
+ },
1351
+ {
1352
+ "name": "GrossProfit",
1353
+ "description": "Total gross profit",
1354
+ "type": "decimal",
1355
+ "optional": false
1356
+ },
1357
+ {
1358
+ "name": "GrossProfitMarginPct",
1359
+ "description": "Gross profit margin percentage",
1360
+ "type": "decimal",
1361
+ "optional": false
1362
+ },
1363
+ {
1364
+ "name": "RevenueContributionPct",
1365
+ "description": "Percentage of total company revenue",
1366
+ "type": "decimal",
1367
+ "optional": false
1368
+ }
1369
+ ]
1370
+ }
1371
+ ]