@@ -6,7 +6,7 @@ description:
6
6
practices for financial and scientific calculations.
7
7
---
8
8
9
- QuestDB provides a ` decimal ` data type for exact numeric calculations, essential
9
+ QuestDB provides a ` decimal ` data type for exact numeric calculations, useful
10
10
for financial computations, scientific measurements, and any scenario where
11
11
precision matters. This page explains how to use decimals effectively, including
12
12
syntax, operations, and performance considerations.
@@ -18,15 +18,6 @@ operations. Unlike floating-point types (`float` and `double`), decimals avoid
18
18
rounding errors by storing numbers as scaled integers internally. This makes
19
19
them ideal for monetary calculations where accuracy is critical.
20
20
21
- ### Key characteristics
22
-
23
- - ** Exact arithmetic** : No rounding errors during addition, subtraction, and
24
- multiplication
25
- - ** Fixed precision** : Numbers are stored with a defined number of total digits
26
- - ** Fixed scale** : A specific number of digits after the decimal point
27
- - ** Variable storage** : Automatically optimized from 1 to 32 bytes based on
28
- precision
29
-
30
21
## Decimal type in QuestDB
31
22
32
23
QuestDB implements decimals with the syntax ` DECIMAL(precision, scale) ` :
@@ -40,9 +31,10 @@ For example, `DECIMAL(10, 2)` can store values from -99,999,999.99 to
40
31
If neither the precision and scale are provided, the type defaults to a
41
32
precision of 18 and a scale of 3.
42
33
43
- ### Storage optimization
34
+ ### Storage
44
35
45
- QuestDB automatically selects the optimal storage size based on precision:
36
+ QuestDB automatically selects the optimal storage size based on the decimal's
37
+ precision:
46
38
47
39
| Precision | Storage Size | Internal Type |
48
40
| ------------ | ------------ | ------------- |
@@ -194,7 +186,8 @@ SELECT CAST(99.99m AS DOUBLE); -- Result: 99.99 (as floating-point)
194
186
195
187
- ** No implicit conversion from double/float** : Must use explicit ` CAST ` or
196
188
decimal literals
197
- - ** Integer to decimal** : Safe, no precision loss
189
+ - ** Integer to decimal** : Safe, no precision loss, the decimals have a scale of
190
+ 0
198
191
- ** Double to decimal** : May lose precision due to floating-point representation
199
192
- ** Between decimal types** : Automatic when precision/scale allows
200
193
@@ -210,16 +203,13 @@ SELECT CAST(99.99m AS DOUBLE); -- Result: 99.99 (as floating-point)
210
203
211
204
- ** Slower than floating-point** : Typically slower than ` double ` operations
212
205
- ** More storage** : May use more space than ` float ` for equivalent range
213
- - ** Complex operations** : Division and advanced math functions have overhead
206
+ - ** Complex operations** : Division have overhead
214
207
215
208
### Performance tips
216
209
217
- 1 . ** Use appropriate precision** : Don't over-specify precision beyond your needs
218
- 2 . ** Keep precision ≤ 18 when possible** : DECIMAL64 operations are faster than
219
- DECIMAL128/256
220
- 3 . ** Pre-calculate when possible** : Store commonly used calculations
221
- 4 . ** Consider doubles for analysis** : Use decimals for storage, doubles for
222
- complex analytics
210
+ - ** Use appropriate precision** : Don't over-specify precision beyond your needs
211
+ - ** Keep precision ≤ 18 when possible** : DECIMAL64 operations are faster than
212
+ DECIMAL128/256
223
213
224
214
## Common use cases
225
215
@@ -298,15 +288,15 @@ SAMPLE BY 1h;
298
288
299
289
### When to use decimals
300
290
301
- ✅ ** Use decimals for:**
291
+ ** Use decimals for:**
302
292
303
293
- Financial data (prices, amounts, exchange rates)
304
294
- Accounting calculations
305
295
- Scientific measurements requiring exact precision
306
296
- Regulatory compliance scenarios
307
297
- Any calculation where rounding errors are unacceptable
308
298
309
- ❌ ** Avoid decimals for:**
299
+ ** Avoid decimals for:**
310
300
311
301
- Scientific calculations requiring extensive math functions
312
302
- Performance-critical analytics on large datasets
@@ -348,48 +338,3 @@ SAMPLE BY 1h;
348
338
-- Good: Use decimal literal
349
339
SELECT amount + 10.00m FROM prices;
350
340
```
351
-
352
- ## Differences from doubles
353
-
354
- | Aspect | Decimal | Double |
355
- | ------------------- | ------------------------- | --------------------- |
356
- | ** Precision** | Exact | Approximate |
357
- | ** Rounding errors** | None for basic operations | Can accumulate |
358
- | ** Performance** | Slower | Faster |
359
- | ** Storage** | Variable (1-32 bytes) | Fixed (8 bytes) |
360
- | ** Use case** | Money, exact values | Scientific, analytics |
361
- | ** Literal syntax** | Requires 'm' suffix | Standard notation |
362
-
363
- ## Example: Financial reporting
364
-
365
- Here's a complete example showing decimals in a financial reporting scenario:
366
-
367
- ``` questdb-sql
368
- -- Create financial tables
369
- CREATE TABLE account_transactions (
370
- account_id SYMBOL,
371
- transaction_type SYMBOL,
372
- amount DECIMAL(15, 2),
373
- balance DECIMAL(15, 2),
374
- timestamp TIMESTAMP
375
- ) timestamp(timestamp);
376
-
377
- -- Insert sample transactions
378
- INSERT INTO account_transactions VALUES
379
- ('ACC001', 'DEPOSIT', 1000.00m, 1000.00m, '2024-01-01'),
380
- ('ACC001', 'WITHDRAWAL', 150.50m, 849.50m, '2024-01-02'),
381
- ('ACC001', 'INTEREST', 2.12m, 851.62m, '2024-01-03'),
382
- ('ACC002', 'DEPOSIT', 5000.00m, 5000.00m, '2024-01-01'),
383
- ('ACC002', 'TRANSFER', 500.00m, 4500.00m, '2024-01-02');
384
-
385
- -- Financial summary with exact calculations
386
- SELECT
387
- account_id,
388
- sum(CASE WHEN transaction_type = 'DEPOSIT' THEN amount ELSE 0.00m END) AS total_deposits,
389
- sum(CASE WHEN transaction_type = 'WITHDRAWAL' THEN amount ELSE 0.00m END) AS total_withdrawals,
390
- last(balance) AS current_balance,
391
- count(*) AS transaction_count
392
- FROM account_transactions
393
- WHERE timestamp >= '2024-01-01'
394
- GROUP BY account_id;
395
- ```
0 commit comments