下面有两个要给出结果的查询
select order.ORDER_NUMBER,
upc.PROD_ID,
shipmentItem.upc_id,
order.ORDER_DATE
from shipment shipment,
Order order, shipment_item shipmentItem, UPC upc
where order.ORDER_DATE > current timestamp - 24
months
and order.order_number = shipment.order_number
and shipmentItem.SHIPMENT_ID = shipment.SHIPMENT_ID
and order.user_id = '123456'
and upc.UPC_ID = shipmentItem.UPC_ID
order by order.ORDER_DATE desc
给出以下输出
Order_number prod_id upc_id order_date
1234507 6489785 38890630 2018-05-27 13:12:47.564791
1234504 1310 10855 2018-05-27 13:10:41.475177
1234504 448832 23431431 2018-05-27 13:10:41.475177
另一个条件聚合为
的查询select upc_id,
max(case
when attr_name = 'COLOR_NORMAL' then
attr_value
end) as color,
max(case
when attr_name = 'SIZE' then
attr_value
end) as size
from Unary_Upc
where UPC_ID in (38890630, 10855, 23431431)
group by UPC_ID;
给出以下输出
upc_id color size
10855 (null) (null)
23431431 White (null)
38890630 Black 8
如何结合以上两个查询,以使输出如下所示?
Order_number prod_id upc_id color size order_date
1234507 6489785 38890630 Black 8 2018-05-27 13:12:47.564791
1234504 1310 10855 (null) (null) 2018-05-27 13:10:41.475177
1234504 448832 23431431 White (null) 2018-05-27 13:10:41.475177
答案 0 :(得分:2)
您可以使用两个相关的子查询,并针对group by upc_id
异常删除了ORA-01422
,并根据ANSI-92
格式使用别名而不是完整表名来编写查询。
order
是保留关键字,因此无法创建
Oracle中名为order
的表,但名称为"order"
也是可以的。 将current timestamp - 24 months
替换为current_timestamp - interval '24' month
select o.order_number, u.prod_id, si.upc_id,
(select max(case
when attr_name = 'COLOR_NORMAL' then
attr_value
end)
from Unary_Upc
where upc_id = u.upc_id
) as color,
(select max(case
when attr_name = 'SIZE' then
attr_value
end)
from Unary_Upc
where upc_id = u.upc_id
) as size,
o.order_date
from shipment s
join "order" o on o.order_number = s.order_number
join shipment_item si on si.shipment_id = s.shipment_id
join upc u on u.upc_id = si.upc_id
where order.order_date > current_timestamp - interval '24' month
and o.user_id = '123456'
order by o.order_date desc
答案 1 :(得分:0)
您可以通过两种方法来实现它:
---方法1 ---
加入以下两个子查询:
SELECT
A.ORDER_NUMBER,
A.PROD_ID,
A.UPC_ID,
B.COLOR,
B.SIZE,
A.ORDER_DATE
FROM
(
SELECT
ORDER.ORDER_NUMBER,
UPC.PROD_ID,
SHIPMENTITEM.UPC_ID,
ORDER.ORDER_DATE
FROM
SHIPMENT SHIPMENT,
ORDER ORDER,
SHIPMENT_ITEM SHIPMENTITEM,
UPC UPC
WHERE
ORDER.ORDER_DATE > CURRENT timestamp - 24 months
AND ORDER.ORDER_NUMBER = SHIPMENT.ORDER_NUMBER
AND SHIPMENTITEM.SHIPMENT_ID = SHIPMENT.SHIPMENT_ID
AND ORDER.USER_ID = '123456'
AND UPC.UPC_ID = SHIPMENTITEM.UPC_ID
) A
JOIN (
SELECT
UPC_ID,
MAX(CASE
WHEN ATTR_NAME = 'COLOR_NORMAL' THEN ATTR_VALUE
END) AS COLOR,
MAX(CASE
WHEN ATTR_NAME = 'SIZE' THEN ATTR_VALUE
END) AS SIZE
FROM
UNARY_UPC_ATTR_VAL
WHERE
UPC_ID IN (
38890630,
10855,
23431431
)
GROUP BY
UPC_ID
) B ON ( A.UPC_ID = B.UPC_ID )
ORDER BY
A.ORDER_DATE DESC WITH UR
---方法2 ---
在第一个查询中使用UNARY_UPC_ATTR_VAL
表,并执行GROUP BY
,如下所示:
SELECT
ORDER.ORDER_NUMBER,
UPC.PROD_ID,
SHIPMENTITEM.UPC_ID,
MAX(CASE
WHEN ATTR_NAME = 'COLOR_NORMAL' THEN ATTR_VALUE
END) AS COLOR,
MAX(CASE
WHEN ATTR_NAME = 'SIZE' THEN ATTR_VALUE
END) AS SIZE,
ORDER.ORDER_DATE
FROM
SHIPMENT SHIPMENT,
ORDER ORDER,
SHIPMENT_ITEM SHIPMENTITEM,
UPC UPC,
UNARY_UPC_ATTR_VAL UUAV -- ADDED THIS TABLE HERE
WHERE
order.ORDER_DATE >current timestamp - 24 months
AND ORDER.ORDER_NUMBER = SHIPMENT.ORDER_NUMBER
AND SHIPMENTITEM.SHIPMENT_ID = SHIPMENT.SHIPMENT_ID
AND ORDER.USER_ID = '123456'
AND UPC.UPC_ID = SHIPMENTITEM.UPC_ID
-- ADDED FOLLOWING CONDITION AND JOIN HERE
AND UUAV.UPC_ID IN (
38890630,
10855,
23431431
)
AND UUAV.UPC_ID = UPC.UPC_ID
-- ADDED FOLLOWING GROUP BY
GROUP BY
ORDER.ORDER_NUMBER,
UPC.PROD_ID,
SHIPMENTITEM.UPC_ID,
ORDER.ORDER_DATE
ORDER BY
ORDER.ORDER_DATE DESC WITH UR
希望,这会为您提供帮助。
干杯!