简体   繁体   中英

Oracle - SELECT DENSE_RANK OVER (ORDER BY, SUM, OVER and PARTITION BY)

I have a database where I get lots, defects and quantities (from 2 tables).

After changing the names slightly and removing some filters which I made sure weren't important for the question, my current working query looks like the following (with the help of this answer ):

WITH subquery AS (
  SELECT * FROM (
    SELECT tbl2.lot
    FROM db.tbl1 tbl1, db.tbl2 tbl2
    WHERE tbl2.key = tbl1.key
    GROUP BY tbl2.lot
    ORDER BY Sum(tbl1.qtd) DESC, tbl2.lot
  ) WHERE ROWNUM <= 10
) SELECT tbl2.lot, tbl1.defect, tbl1.desc, Sum(tbl1.qtd)
FROM db.tbl1 tbl1, db.tbl2 tbl2, subquery
WHERE tbl2.lot = subquery.lot
  AND tbl2.key = tbl1.key
GROUP BY tbl2.lot, tbl1.defect, tbl1.desc
ORDER BY Sum(Sum(tbl1.qtd)) OVER (PARTITION BY tbl2.lot) DESC, Sum(tbl1.qtd) DESC, tbl2.lot, tbl1.defect, tbl1.desc

I'm trying to improve the query a little more and I got this solution to optimize it which is what I needed but I'm getting an error when combining both answers.

In my head the solution should be the following query:

SELECT *
FROM (
  SELECT DENSE_RANK() OVER (ORDER BY Sum(Sum(tbl1.qtd)) OVER (PARTITION BY tbl2.lot) DESC, Sum(tbl1.qtd) DESC, tbl2.lot, tbl1.defect, tbl1.desc) rnk, tbl2.lot, tbl1.defect, tbl1.desc, Sum(tbl1.qtd)
  FROM db.tbl1 tbl1, db.tbl2 tbl2
  WHERE tbl2.key = tbl1.key
  GROUP BY tbl2.lot, tbl1.defect, tbl1.desc
  ORDER BY Sum(Sum(tbl1.qtd)) OVER (PARTITION BY tbl2.lot) DESC, Sum(tbl1.qtd) DESC, tbl2.lot, tbl1.defect, tbl1.desc
)
WHERE rnk <= 10
ORDER BY rnk

But I get the error It was not possible to add the table '('. (translated).

When I remove the part Sum(Sum(tbl1.qtd)) OVER (PARTITION BY tbl2.lot) DESC, inside the SELECT DENSE_RANK() OVER(ORDER BY ...) from the query it runs and works except it doesn't order the values the way I need.

I'm not sure if the problem might be having an OVER inside another one. I tried to figure out by changing parts and see what would happen but wasn't able to find the solution.

After a lot of trying I still haven't figure out if it's possible to fix the order inside the DENSE_RANK() 's OVER but I did found out a solution in between the two.

SELECT lot, def, qtd
FROM (
  SELECT DENSE_RANK() OVER (ORDER BY qtd_lot DESC) rnk, lot, def, qtd
  FROM (
    SELECT tbl2.lot lot, tbl1.def def, Sum(tbl1.qtd) qtd, Sum(Sum(tbl1.qtd)) OVER (PARTITION BY tbl2.lot) qtd_lot
    FROM db.tbl1 tbl1, db.tbl2 tbl2
    WHERE tbl2.key = tbl1.key
    GROUP BY tbl2.lot, tbl1.def
  )
)
WHERE rnk <= 10
ORDER BY rnk, qtd DESC, lot, def

It's not as good as the solution that I was trying but it is better than my previous working code. What I did was move the Sum(Sum(tbl1.qtd)) OVER (PARTITION BY tbl2.lot) out of the DENSE_RANK() and then add it with the name qtd_lot .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM