Mengapa kolom dengan pembagian ditampilkan sebagai 0?
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Dalam dialek tertentu, termasuk Postgres dan Redshift, melakukan penghitungan antara bilangan bulat (seperti membagi jumlah) akan menampilkan bilangan bulat meskipun hasilnya adalah desimal dalam matematika normal. Misalnya, Anda dapat membuat tindakan seperti berikut:
measure: sold_ratio {
type: number
sql: ${sold_count} / ${total_count} ;;
value_format: "0.00" # Number with exactly 2 decimals (1.23)
}
measure: sold_percent {
type: number
sql: 100 * ${sold_count} / ${count} ;;
value_format: "0.00"
}
Namun, saat Anda menjalankan ukuran di Jelajahi, kolom Rasio Terjual akan menampilkan nol, dan kolom Persentase Terjual tidak akan diisi dengan tempat desimal. Jawaban ini salah:
Persentase Item Inventaris yang Terjual
Rasio Item Inventaris Terjual
Jumlah Item Inventaris
Jumlah Item Inventaris Terjual
48,00
0
1.165.224
560.223
Solusi: Menampilkan kolom sebagai angka floating point
Jika penghitungan dikalikan dengan non-bilangan bulat, nilai akan ditransmisikan sebagai float, dan desimal akan ditampilkan seperti yang diharapkan. Anda dapat mengalikan pembilang dengan angka desimal (seperti 1,0 atau 100,0) untuk memaksa SQL menampilkan hasil desimal:
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-07-31 UTC."],[],[],null,["# Why are my fields with division showing up as 0?\n\nIn certain dialects, including Postgres and Redshift, performing a calculation between integers (like dividing counts) will return an integer even if the result would be a decimal in normal math. For example, you might make measures like the following: \n\n```\nmeasure: sold_ratio {\n type: number\n sql: ${sold_count} / ${total_count} ;;\n value_format: \"0.00\" # Number with exactly 2 decimals (1.23)\n}\nmeasure: sold_percent {\n type: number\n sql: 100 * ${sold_count} / ${count} ;;\n value_format: \"0.00\"\n}\n```\n\n\nHowever, when you run the measures in an Explore, the **Sold Ratio** column returns zero, and the **Sold Percent** column does not have its decimal places populated. This is not correct:\n\n\u003cbr /\u003e\n\n\nThe solution: Cast your field as a floating-point number\n--------------------------------------------------------\n\n\nIf the calculation is multiplied by a non-integer, the values will cast as floats, and decimals will be returned as expected. You can multiply the numerator by a decimal number (like 1.0 or 100.0) to force SQL to return a decimal result: \n\n```\nmeasure: sold_ratio {\n type: number\n sql: 1.0 * ${sold_count} / ${total_count};;\n value_format: \"0.00\"\n}\nmeasure: sold_percent {\n type: Number\n sql: 100.0 * ${sold_count} / ${count};;\n value_format: \"0.00\"\n}\n```\n\n\nThe resulting Explore **Data** table now displays the expected results:\n\n\n\u003cbr /\u003e"]]