按多列和条件分组

group by with multiple columns and with condition

我的查询曾经是

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

它运行良好。

由于某些情况,我在 Device_id

更改了规则
CASE
   WHEN NAME = 'xyz' THEN VEHICLE_ID
   ELSE DEVICE_ID 
END AS BIKEID

我的改动看起来像

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

我在 GROUP BY 做错了。

任何人请建议如何解决此问题。

谢谢

这是你想要的吗?

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)