[英]Need help calculating a column under certain circumstances
我是菜鳥,因此對未決的菜鳥等表示歉意。
這是我正在使用的:
該表從遠程傳感器捕獲數據,包括攝氏溫度和濕度。 在這種情況下,我專門使用event_type =“ Sensor”,event_description =“ Celsius”和event_data =攝氏溫度。
我想創建一個視圖,該視圖提供用於將攝氏溫度轉換為華氏溫度的列。 (或Kelvin等)
表格欄:
event_id int(11) AI PK
event_date date
event_time time
event_type char(50)
event_description char(50)
event_data char(100)
event_platform char(100)
謝謝你的幫助!
event_id event_date event_time event_type event_description event_data
618 2017-11-04 20:17:03 Sensor Celsius 26.2999992371
617 2017-11-04 20:17:03 Sensor Humidity 62.0
616 2017-11-04 19:17:03 Sensor Celsius 27.1000003815
615 2017-11-04 19:17:03 Sensor Humidity 59.7000007629
614 2017-11-04 18:17:03 Sensor Celsius 28.5
613 2017-11-04 18:17:03 Sensor Humidity 56.9000015259
612 2017-11-04 17:17:03 Sensor Celsius 31.1000003815
611 2017-11-04 17:17:03 Sensor Humidity 51.4000015259
我預見到該桌子的設計有問題。 為什么要在char(100)列中捕獲數字數據? 那真的不好。 也不確定為什么要使用這么多固定寬度的char列,這會浪費大量的存儲空間。
select
event_type
, event_date date
, event_time time
, event_description
, event_data as Celcius
, cast(event_data as decimal(12,3)) * 1.8 + 32 as Fahrenheit
, cast(event_data as decimal(12,3)) + 273.15 as Kelvin
from yourtable
where event_type = 'Sensor'
and event_description = 'Celsius'
nb:選擇十進制(12,3)沒有科學依據,只是我的手指着地
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.