用多列和条件分组

时间:2019-07-08 20:53:36

标签: sql ssms-2014

我的查询曾经是

playerOrigin.geometry.vertices[i]

运行正常。

由于某些条件,我在Device_id上​​更改了规则

s = "['abc','def',1234,234,'abcd','x','mnop',5,'zZzZ']"
splits = s.tr("'[]''",'').split(',') # need to pass in the comma for the split
Hash[splits.inject({}) { |memo,s| memo[s.length] ||= []; memo[s.length] << s; memo }.sort] # doesn't use Array.each but?

我的更改看起来像

SELECT 
    PROVIDER, 
    LEFT([START_TIME],13) as [HOUR],
    DEVICE_ID as BIKEID
FROM 
    table1
GROUP BY 
    PROVIDER_NAME, DEVICE_ID, LEFT([START_TIME],13)

我在CASE WHEN NAME = 'xyz' THEN VEHICLE_ID ELSE DEVICE_ID END AS BIKEID 做错了。

任何人请提出解决方法。

谢谢

1 个答案:

答案 0 :(得分:1)

这是您想要的吗?

SELECT PROVIDER, 
       LEFT([START_TIME],13) as [HOUR],
       (CASE WHEN NAME = 'xyz' THEN VEHICLE_ID
             ELSE DEVICE_ID
        END) as BIKEID
FROM table1
GROUP BY PROVIDER_NAME, 
         (CASE WHEN NAME = 'xyz' THEN VEHICLE_ID
             ELSE DEVICE_ID
          END),
         LEFT([START_TIME], 13)