查詢表:
| 不同的水果 | 水果總數 | 水果部分 |
|---|---|---|
| 3 | 5 | |
| 2 | 6 | |
| 2 | 2 | |
| 4 | 6 | |
| 1 | 1 | |
| 6 | 6 | |
| 6 | 16 | |
| 4 | 20 |
更新表:
| 不同的水果 | 水果總數 | 水果部分 |
|---|---|---|
| 3 | 5 | 3/5 |
| 2 | 6 | 2/6 |
| 2 | 2 | 2/2 |
| 4 | 6 | 4/6 |
| 1 | 1 | 1/1 |
| 6 | 6 | 6/6 |
| 6 | 16 | 6/6 |
| 4 | 20 | 4/6 |
一共有6種不同的水果。但是,每條記錄的總果實可能大于 6。
我想使用這樣的 case 陳述句:
WHEN Total Fruits < 6 THEN Fruits Fraction = Different Fruits/Total Fruits
ELSE Fruits Fraction = Different Fruits/6
我不想簡化分數,可能我認為它會使用字符資料型別。
請幫忙。
提前致謝。
uj5u.com熱心網友回復:
你可以試試這個:
SELECT "different fruits", "total fruits",
CASE
WHEN "total fruits" > 6 THEN "different fruits"||'/'||'6'
ELSE "different fruits"||'/'||"total fruits"
END AS fruits_fraction
FROM your_table
db<>小提琴鏈接在這里
uj5u.com熱心網友回復:
不需要CASE表達。
你可以用MIN()標量函式來做到:
SELECT DifferentFruits, TotalFruits,
DifferentFruits || '/' || MIN(TotalFruits, 6) FruitsFraction
FROM tablename;
或者,如果您想更新表:
UPDATE tablename
SET FruitsFraction = DifferentFruits || '/' || MIN(TotalFruits, 6);
請參閱演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357303.html
