Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何根据每个测试循环选择特定值?_Sql_Google Bigquery - Fatal编程技术网

Sql 如何根据每个测试循环选择特定值?

Sql 如何根据每个测试循环选择特定值?,sql,google-bigquery,Sql,Google Bigquery,现在我在SQL Server表中有了一个原始数据,其中列出了数据传输速度和设备温度 key | value ---------------- test_unit | AX45CG00598 speed_01 | 142.00 speed_02 | 138.00 speed_03 | 155.00 start_temp_01 | 31.04 start_temp_02 | 33.50 start_temp_03 | 33.88 end_te

现在我在SQL Server表中有了一个原始数据,其中列出了数据传输速度和设备温度

key           | value
----------------
test_unit     | AX45CG00598
speed_01      | 142.00
speed_02      | 138.00
speed_03      | 155.00
start_temp_01 | 31.04
start_temp_02 | 33.50
start_temp_03 | 33.88
end_temp_01   | 33.87
end_temp_02   | 34.29
end_temp_03   | 34.64
我想根据测试周期选择输出类似的值

test_unit   | cycle | speed  | start_temp | end_temp
-------------------------------------
AX45CG00598 | #01   | 142.00 | 31.04      | 33.87
AX45CG00598 | #02   | 138.00 | 33.50      | 34.29
AX45CG00598 | #03   | 155.00 | 33.88      | 34.64
下面是我的代码,它根据每个周期输出周期和速度

SELECT
  (select value from unnest(sql_server_table) where key = 'test_unit') as test_unit,
  case
    when key = 'speed_01' then '#01'
    when key = 'speed_02' then '#02'
    when key = 'speed_03' then '#03'
  end as cycle,
  props.value,
FROM
  unnest(sql_server_table) props
WHERE
  props.key in ('speed_01', 'speed_02', 'speed_03')
但是,我不知道如何将
温度
也与
速度
列在同一行上。
请告知,谢谢。

以下基于示例数据的示例适用于BigQuery标准SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 id, [
    STRUCT<key STRING, value STRING>('test_unit', 'AX45CG00598'),
    ('speed_01', '142.00'),
    ('speed_02', '138.00'),
    ('speed_03', '155.00'),
    ('start_temp_01', '31.04'),
    ('start_temp_02', '33.50'),
    ('start_temp_03', '33.88'),
    ('end_temp_01', '33.87'),
    ('end_temp_02', '34.29'),
    ('end_temp_03', '34.64')
  ] props
)
SELECT id, 
  MAX(IF(key = 'test_unit', value, NULL)) test_unit, 
  '#' || cycle AS cycle,
  MAX(IF(key = 'speed_' || cycle, value, NULL)) speed, 
  MAX(IF(key = 'start_temp_' || cycle, value, NULL)) start_temp, 
  MAX(IF(key = 'end_temp_' || cycle, value, NULL)) end_temp  
FROM `project.dataset.table` t,
UNNEST(props) prop, 
UNNEST(['01', '02', '03']) cycle
GROUP BY id, cycle   

您所要求的是不可能的,除非您有测试单元的第三列。您确定您使用的是(Microsoft)SQL Server吗?从什么时候开始,它有了
unnest()
函数?为什么要在表上应用
unnest()
函数?这通常用于取消数组的排序。@a_horse_与_no_name一起\u Oops,对不起,我忘了注释它是Google SQL。SQL表表示无序集。除非有一列指定顺序,否则数据毫无意义。
Row id  test_unit   cycle   speed   start_temp  end_temp     
1   1   AX45CG00598 #01     142.00  31.04       33.87    
2   1   AX45CG00598 #02     138.00  33.50       34.29    
3   1   AX45CG00598 #03     155.00  33.88       34.64