MySQL Group By Query

时间:2012-05-03 16:33:49

标签: mysql group-by

Please see link to get table in better format

在MySQL数据库中,我有两个表'VEHICLE'和'VEHICLE_CLASS',如下所示:

VEHICLE 
------------------------------------------- 
VEHICLE_ID  | VEHICLE_CLASS_ID  | STATUS 
------------------------------------------- 
vehicle_001 | vehicle_class_001 | 0 
vehicle_002 | vehicle_class_002 | 1 
vehicle_003 | vehicle_class_003 | 2 
vehicle_004 | vehicle_class_001 | 0 
vehicle_005 | vehicle_class_002 | 2 
vehicle_006 | vehicle_class_001 | 0 
vehicle_007 | NULL              | 1 
---------------------------------------------- 


VEHICLE_CLASS 
------------------------------------------ 
VEHICLE_CLASS_ID  | VEHICLE_CLASS_NAME 
----------------------------------------- 
vehicle_class_001 | ABC 
vehicle_class_002 | BCD 
vehicle_class_003 | EFG 
vehicle_class_004 | XYZ 
vehicle_class_005 | PQR 
vehicle_class_006 | STU 
--------------------------------------- 
  • 有三种状态0 =已启动,1 =正在进行,2 =已完成

我需要一个查询来获取结果行,如下所示:

----------------------------------------------------------------------- 
VEHICLE_CLASS_NAME | COMPLETED_ VEHICLES | NOT_COMPLETED_ VEHICLES 
--------------------------------------------------------------------------- 
ABC                |                     |vehicle_001, vehicle_004, vehicle_006 
BCD                | vehicle_005         |vehicle_002 
EFG                | vehicle_003         | 
  • 如果Status = 2则完成,否则它未完成。

这是我写的查询,但它没有给我正确的行。

SELECT 
      veh.VEHICLE_CLASS_ID, 
      vehclass.VEHICLE_CLASS_NAME, 
      IF(STATUS=2, GROUP_CONCAT(`VEHICLE_ID`),'') COMPLETED_ VEHICLES, 
      IF(STATUS<2,GROUP_CONCAT(`VEHICLE_ID`),'') NOT_COMPLETED_ VEHICLES 
   FROM 
      VEHICLE veh 
         LEFT JOIN VEHICLE_CLASS vehclass 
            on veh.VEHICLE_CLASS_ID = vehclass.VEHICLE_CLASS_ID 
   GROUP BY 
      veh.VEHICLE_CLASS_ID 
   having 
      veh.VEHICLE_CLASS_ID is not null; 

请帮帮我,我怎样才能获得结果行。 谢谢。

2 个答案:

答案 0 :(得分:1)

的MySQL

SELECT 
  vehclass.VEHICLE_CLASS_NAME, 
  GROUP_CONCAT(CASE veh.STATUS WHEN 2 THEN `VEHICLE_ID` ELSE NULL END) COMPLETED_VEHICLES, 
  GROUP_CONCAT(CASE veh.STATUS WHEN 2 THEN NULL ELSE `VEHICLE_ID` END) NOT_COMPLETED_VEHICLES 
FROM VEHICLE veh 
LEFT JOIN VEHICLE_CLASS vehclass 
  ON veh.VEHICLE_CLASS_ID = vehclass.VEHICLE_CLASS_ID 
GROUP BY veh.VEHICLE_CLASS_ID
HAVING veh.VEHICLE_CLASS_ID is not null;

SQLFiddle

MSSQL 2008

SELECT 
  vehclass.VEHICLE_CLASS_NAME, 
  STUFF((SELECT CASE v.STATUS WHEN 2 THEN ',' + v.VEHICLE_ID ELSE '' END
          FROM Vehicle v
          WHERE v.VEHICLE_CLASS_ID = veh.VEHICLE_CLASS_ID
          FOR XML PATH (''))
         , 1, 1, '') as COMPLETED_VEHICLES, 
  STUFF((SELECT CASE v.STATUS WHEN 2 THEN '' ELSE ',' + v.VEHICLE_ID END
          FROM Vehicle v
          WHERE v.VEHICLE_CLASS_ID = veh.VEHICLE_CLASS_ID
          FOR XML PATH (''))
         , 1, 1, '') as NOT_COMPLETED_VEHICLES
FROM VEHICLE veh 
LEFT JOIN VEHICLE_CLASS vehclass 
  ON veh.VEHICLE_CLASS_ID = vehclass.VEHICLE_CLASS_ID 
GROUP BY veh.VEHICLE_CLASS_ID, vehclass.VEHICLE_CLASS_NAME
HAVING veh.VEHICLE_CLASS_ID is not null;

SQLFiddle

ORACLE

为了在oracle中实现这一点,请参阅this link以了解使用什么代替GROUP_CONCAT

  1. 自定义用户定义的聚合函数 - 最有可能是您最好的选择
  2. wm_concat - 可能存在未记录的,不受支持的。
  3. listagg - 11g release 2

答案 1 :(得分:1)

SELECT 
  vechile_class.vechile_class_name,
  GROUP_CONCAT(v1.vechile_id) AS COMPLETED_VEHICLES,
  GROUP_CONCAT(v2.vechile_id) AS NOT_COMPLETED_VEHICLES
FROM vechile_class
LEFT JOIN vechile v1 ON 
  v1.vechile_class_id = vechile_class.vechile_class_id AND v1.status = 2
LEFT JOIN vechile v2 ON 
  v2.vechile_class_id = vechile_class.vechile_class_id AND v2.status <> 2
GROUP BY vechile.vechile_class_id