0

我有以下 SELECT 语句,并且基于我在这里看到的内容:SQL Select Max Date with Multiple records我的示例以相同的方式设置。我在 Oracle 11g 上。它不是为每个asset_tag 返回一条记录,而是返回多个记录。没有源表中的记录那么多,但(我认为)应该多。如果我运行内部 SELECT 语句,它还会返回正确的记录集(每个asset_tag 1 个),这真的让我很难过。

SELECT 
outside.asset_tag,
outside.description, 
outside.asset_type, 
outside.asset_group, 
outside.status_code, 
outside.license_no, 
outside.rentable_yn, 
outside.manufacture_code, 
outside.model, 
outside.manufacture_vin, 
outside.vehicle_yr, 
outside.meter_id, 
outside.mtr_uom, 
outside.mtr_reading, 
outside.last_read_date
FROM mp_vehicle_asset_profile outside
RIGHT OUTER JOIN
  (
  SELECT asset_tag, max(last_read_date) as last_read_date
  FROM mp_vehicle_asset_profile
  group by asset_tag
  ) inside
ON outside.last_read_date=inside.last_read_date

有什么建议么?

4

2 回答 2

0

尝试分析函数:

SELECT  outside.asset_tag,
        outside.description, 
        outside.asset_type, 
        outside.asset_group, 
        outside.status_code, 
        outside.license_no, 
        outside.rentable_yn, 
        outside.manufacture_code, 
        outside.model, 
        outside.manufacture_vin, 
        outside.vehicle_yr, 
        outside.meter_id, 
        outside.mtr_uom, 
        outside.mtr_reading, 
        outside.last_read_date
FROM (  SELECT *, ROW_NUMBER() OVER(PARTITION BY asset_tag ORDER BY last_read_date DESC) Corr
        FROM mp_vehicle_asset_profile) outside
WHERE Corr = 1
于 2012-05-25T21:41:03.853 回答
0

我认为您需要添加...

AND outside.asset_tag=inside.asset_tag

...符合您ON列表中的标准。

RIGHT OUTER JOIN也不需要a 。An将给出相同的结果(并且INNER JOIN可能更有效),因为在.asset_taglast_read_datemp_vehicle_asset_profile

即使这样,如果存在“关系”,查询可能会为每个资产标签返回多行 - 即多行具有相同的last_read_date. 相比之下,@Lamak 的基于分析的答案将在这种情况下任意选择一行。

您的评论表明您想通过选择最高的行来打破mtr_reading平局last_read_date

您可以通过将子句中的更改修改ORDER BYOVER

ORDER BY last_read_date DESC, mtr_reading DESC

如果仍然存在关联(即多行具有相同asset_taglast_read_date、 和mtr_reading),则查询将再次随意选择恰好一行。

您可以修改我的基于聚合的答案以使用以下最高打破平局mtr_reading

SELECT  
    outside.asset_tag, 
    outside.description,
    outside.asset_type,
    outside.asset_group,
    outside.status_code,
    outside.license_no,
    outside.rentable_yn,
    outside.manufacture_code,
    outside.model,
    outside.manufacture_vin,
    outside.vehicle_yr,
    outside.meter_id,
    outside.mtr_uom,
    outside.mtr_reading,
    outside.last_read_date 
FROM 
    mp_vehicle_asset_profile outside 
    INNER JOIN 
    ( 
        SELECT
            asset_tag, 
            MAX(last_read_date) AS last_read_date,
            MAX(mtr_reading) KEEP (DENSE_RANK FIRST ORDER BY last_read_date DESC) AS mtr_reading
        FROM
            mp_vehicle_asset_profile 
        GROUP BY
            asset_tag 
    ) inside 
    ON 
        outside.asset_tag = inside.asset_tag
        AND
        outside.last_read_date = inside.last_read_date
        AND
        outside.mtr_reading = inside.mtr_reading

如果仍然存在关联(即多行具有相同asset_taglast_read_date、 和mtr_reading),则查询可能再次返回多行。

基于分析和聚合的答案不同的另一种方式是它们对空值的处理。如果 、 或 中的任何一个asset_taglast_read_datemtr_reading,则基于分析的答案将返回相关行,但基于聚合的答案不会(因为TRUE当涉及空时,连接中的相等条件不会计算。

于 2012-05-26T12:46:57.313 回答