我想结合两个不同的查询并得到如下所示的结果。
此查询为我提供了取消计数w.r.t vehicle_NAME
(SELECT 'bike' "vehicle_NAME", count(B.ORDER_ID) "cancel"
FROM TABLE A, CAR_BIKE B
WHERE A.vehicle_NAME IN ('bike')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status NOT IN ('0', '00')
GROUP BY A.vehicle_NAME
union
SELECT 'car' "vehicle_NAME", count(B.ORDER_ID) "cancel"
FROM TABLE A, CAR_BIKE B
WHERE A.vehicle_NAME IN ('car')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status NOT IN ('0', '00')
GROUP BY A.vehicle_NAME
union
SELECT 'cycle' "vehicle_NAME", count(B.ORDER_ID) "cancel"
FROM TABLE A, cycle_RESP B
WHERE A.vehicle_NAME IN ('cycle')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status NOT IN ('0', '00')
GROUP BY A.vehicle_NAME)
此BELOW查询为我提供了成功交付计数w.r.t vehicle_NAME
(SELECT 'bike' "vehicle_NAME", count(B.ORDER_ID) "Delivered COUNT"
FROM TABLE A, CAR_BIKE B
WHERE A.vehicle_NAME IN ('bike')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status IN ('0', '00')
GROUP BY A.vehicle_NAME)
union
SELECT 'car' "vehicle_NAME",count(B.ORDER_ID) "Delivered COUNT"
FROM TABLE A, CAR_BIKE B
WHERE A.vehicle_NAME IN ('car')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status IN ('0', '00')
GROUP BY A.vehicle_NAME)
union
SELECT 'cycle' "vehicle_NAME", count(B.ORDER_ID) "Delivered COUNT"
FROM TABLE A, cycle_RESP B
WHERE A.vehicle_NAME IN ('cycle')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status IN ('0', '00')
GROUP BY A.vehicle_NAME)
我想结合上述两个查询并获得如下所示的输出:
vehicle_NAME | cancel | Delivered COUNT
--------------------------------------
bike | 20 | 35
car | 10 | 34
cycle | 45 | 45
答案 0 :(得分:0)
试试这个答案:
SELECT "vehicle_NAME",SUM("cancel")"cancel",SUM("Delivered COUNT")"Delivered COUNT"
FROM(
SELECT "vehicle_NAME","cancel",0 AS "Delivered COUNT"
FROM
(SELECT 'bike' "vehicle_NAME", count(B.ORDER_ID) "cancel"
FROM TABLE A, CAR_BIKE B
WHERE A.vehicle_NAME IN ('bike')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status NOT IN ('0', '00')
GROUP BY A.vehicle_NAME
union
SELECT 'car' "vehicle_NAME", count(B.ORDER_ID) "cancel"
FROM TABLE A, CAR_BIKE B
WHERE A.vehicle_NAME IN ('car')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status NOT IN ('0', '00')
GROUP BY A.vehicle_NAME
union
SELECT 'cycle' "vehicle_NAME", count(B.ORDER_ID) "cancel"
FROM TABLE A, cycle_RESP B
WHERE A.vehicle_NAME IN ('cycle')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status NOT IN ('0', '00')
GROUP BY A.vehicle_NAME)D
UNION ALL
SELECT "vehicle_NAME",0 AS "cancel","Delivered COUNT"
FROM
(SELECT 'bike' "vehicle_NAME", count(B.ORDER_ID) "Delivered COUNT"
FROM TABLE A, CAR_BIKE B
WHERE A.vehicle_NAME IN ('bike')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status IN ('0', '00')
GROUP BY A.vehicle_NAME)
union
SELECT 'car' "vehicle_NAME",count(B.ORDER_ID) "Delivered COUNT"
FROM TABLE A, CAR_BIKE B
WHERE A.vehicle_NAME IN ('car')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status IN ('0', '00')
GROUP BY A.vehicle_NAME)
union
SELECT 'cycle' "vehicle_NAME", count(B.ORDER_ID) "Delivered COUNT"
FROM TABLE A, cycle_RESP B
WHERE A.vehicle_NAME IN ('cycle')
AND A.ORDER_ID = B.ORDER_ID
AND B.Delivery_Status IN ('0', '00')
GROUP BY A.vehicle_NAME)E
)D
GROUP BY "vehicle_NAME"
答案 1 :(得分:0)
您似乎想要一个简单的聚合查询:
SELECT A.vehicle_NAME,
SUM(CASE WHEN B.Delivery_Status NOT IN ('0', '00') THEN 1 ELSE 0 END) as num_cancelled,
SUM(CASE WHEN B.Delivery_Status IN ('0', '00') THEN 1 ELSE 0 END) as num_delivered
FROM TABLE A JOIN
CAR_BIKE B
ON A.ORDER_ID = B.ORDER_ID
WHERE A.vehicle_NAME IN ('bike')
GROUP BY A.vehicle_NAME;