0

请点击此处查看示例表格和说明

根据附加图像,我有两个表“VEHICLE”和“VEHICLE_CLASS”。

我需要一个查询来获取结果行,其中每个 VEHICLE_CLASS 按状态分组了多少 VEHICLES,如附图所示。

查询应该是通用的,以便可以在任何数据库(MySQL/Oracle/MSSQL/DB2)中使用

请帮忙。

CREATE TABLE VEHICLE 
(
    VEHICLE_ID varchar(20), 
    VEHICLE_CLASS_ID varchar(30),
    STATUS int
);

CREATE TABLE VEHICLE_CLASS
(
    VEHICLE_CLASS_ID varchar(30),
    VEHICLE_CLASS_NAME varchar(30)
);

INSERT INTO VEHICLE
   (VEHICLE_ID, VEHICLE_CLASS_ID, STATUS)
VALUES
   ('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);


INSERT INTO VEHICLE_CLASS
   (VEHICLE_CLASS_ID, VEHICLE_CLASS_NAME)
VALUES
   ('vehicle_class_001', 'ABC'),
   ('vehicle_class_002', 'BCD'),
   ('vehicle_class_003', 'EFG'); 

这是我尝试但无法获得所需结果的查询:

SELECT veh.VEHICLE_CLASS_ID, vehclass.VEHICLE_CLASS_NAME, 
    SUM( CASE WHEN veh.STATUS=2 THEN COUNT(veh.VEHICLE_ID) end) Completed,
    SUM( CASE WHEN veh.STATUS!=2 THEN COUNT(veh.VEHICLE_ID) end) not_completed
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;
4

5 回答 5

1

你在哪里很近。而不是 SUM( CASE WHEN veh.STATUS=2 THEN COUNT(veh.VEHICLE_ID)它,然后是 1 并且您还需要按 VEHICLE_CLASS_NAME 分组(在 MySQL 中除外,但您希望它在其他人中工作)。

SELECT veh.VEHICLE_CLASS_ID, 
       vehclass.VEHICLE_CLASS_NAME, 
       Sum(CASE 
             WHEN veh.STATUS = 2 THEN 1 
           END) Completed, 
       Sum(CASE 
             WHEN veh.STATUS <> 2 THEN 1 
           END) not_completed 
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; 

演示 SQL 服务器

演示 MySQL

演示甲骨文

笔记

  • 正如 Lamak 指出的那样<>,通常使用它,但它似乎!=得到了很好的支持,但你需要测试
  • 我没有添加Else 0因为图像在 SUM_not_completed 中包含一个空格。COALACE如果您确实想要零,您可以选择这样做或添加
  • 如果您知道您在 MySQL 中,您可以删除CASE并执行Sum(veh.STATUS = 2) Completed
于 2012-05-10T15:11:30.930 回答
0

试试这个:

SELECT  veh.VEHICLE_CLASS_ID, 
        vehclass.VEHICLE_CLASS_NAME, 
        SUM(CASE WHEN veh.STATUS = 2 THEN 1 ELSE 0 END) Completed,
        SUM(CASE WHEN veh.STATUS <> 2 THEN 1 ELSE 0 END) not_completed
FROM VEHICLE veh 
LEFT JOIN VEHICLE_CLASS vehclass 
ON veh.VEHICLE_CLASS_ID = vehclass.VEHICLE_CLASS_ID
WHERE veh.VEHICLE_CLASS_ID IS NOT NULL
GROUP BY veh.VEHICLE_CLASS_ID, vehclass.VEHICLE_CLASS_NAME;
于 2012-05-10T15:12:39.483 回答
0

也许是这样的:

SELECT
    VEHICLE_CLASS.VEHICLE_CLASS_NAME,
    (
        SELECT
            COUNT(*)
        FROM
            VEHICLE
        WHERE
            VEHICLE.STATUS=2
            AND VEHICLE.VEHICLE_CLASS_ID=VEHICLE_CLASS.VEHICLE_CLASS_ID
    ) AS SUM_Completed,
    (
        SELECT
            COUNT(*)
        FROM
            VEHICLE
        WHERE
            VEHICLE.STATUS IN (1,0)
            AND VEHICLE.VEHICLE_CLASS_ID=VEHICLE_CLASS.VEHICLE_CLASS_ID
    ) AS SUM_Not_Completed
FROM
    VEHICLE_CLASS
于 2012-05-10T15:13:17.793 回答
0
select vehclass.VEHICLE_CLASS_NAME,
    SUM(case when veh.status = 2 then 1 end) Completed,
    SUM(case when veh.status != 2 then 1 end) not_completed
from VEHICLE veh
left join VEHICLE_CLASS vehclass on veh.VEHICLE_CLASS_ID = vehclass.VEHICLE_CLASS_ID
where veh.VEHICLE_CLASS_ID is not null
group by veh.VEHICLE_CLASS_NAME
于 2012-05-10T15:13:44.813 回答
0
SELECT VC.VEHICLE_CLASS_ID, VC.VEHICLE_CLASS_NAME, 
    SUM(CASE WHEN V.STATUS=2 THEN 1 ELSE 0 END) Completed,
    SUM(CASE WHEN V.STATUS!=2 THEN 1 ELSE 0 END) not_completed
FROM VEHICLE V
    JOIN VEHICLE_CLASS VC on V.VEHICLE_CLASS_ID = VC.VEHICLE_CLASS_ID
GROUP BY VC.VEHICLE_CLASS_ID, VC.VEHICLE_CLASS_NAME
于 2012-05-10T15:17:50.273 回答