如何识别SQL中的父数量

时间:2018-05-28 06:27:35

标签: sql oracle

您可以提供的任何帮助都会令人惊叹。

我有一个查询,它提取三个值:排序顺序,级别和数量

我需要的是第四列,显示父级别的数量。因此,如果级别是' 3,我需要它上面的父级的数量级别' 2'。水平可能会有所不同,这就是为什么这很棘手。

以下是Oracle数据库的SQL语句,如果第四列用于父数量,则图像显示预期结果:

select end_part_id, sort_order, level, comp_qty
from report_table
order by sort_order

Sample Table

4 个答案:

答案 0 :(得分:0)

在没有任何测试数据的情况下提供解决方案有点困难(我不会自己生成它们)但是我认为这个表达式应该可行:

COUNT(*) OVER (PARTITION BY COMP_QTY ORDER BY "LEVEL" RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)

注意,RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW是默认值,因此您可以跳过它。我只是为了清楚起见。 LEVEL是分层查询的保留字,因此您应该使用不同的别名/列名称或用引号括起来。

答案 1 :(得分:0)

这是解决方案,它有些棘手,但它可以提供您想要的结果:

SQL:

select sort_order,lvl,comp_qty,
    (select distinct first_value(comp_qty) over(order by sort_order desc,lvl desc)
    from report_table
    where sort_order <x.sort_order
    and lvl<x.lvl) PARENT_QTY
from report_table x

DEMO

答案 2 :(得分:0)

您可以通过查找Level更低select sort_order, level, comp_qty, ( select max(comp_qty) keep (dense_rank last order by sort_order) from report_table parent_record where parent_record.sort_order < report_table.sort_order and parent_record.level < report_table.level ) as parent_qty from report_table order by sort_order; 更低的记录中的最后一个值来获取参与者数量:

/*
* This method will wait for an element (10 seconds default) afterwards it will assert it existence
*
* - parameter toAppear:The condition we are waiting for.
* - parameter element: The element to be expected
* - parameter timeout: The MAX time that the function will wait for the element, 10 seconds if none is given
* - parameter file:    The file where the error will be displayed, the current file will be used in case none is provided
* - parameter line:    The code line where the error will be displayed, the current line will be used in case none is provided
*/
static func assertForElement(toAppear: Bool, _ element: XCUIElement?, timeout: TimeInterval = 10, file: String = #file, line: Int = #line) {

    guard let currentTestCase = BaseXCTestCase.CurrentTestCase else {
        return
    }

    guard let element = element else {
        let message = "Element cannot be empty"

        currentTestCase.recordFailure(withDescription: message, inFile: file, atLine: line, expected: true)

        return
    }

    let existsPredicate = NSPredicate(format: "exists == \(toAppear)")

    currentTestCase.expectation(for: existsPredicate, evaluatedWith: element, handler: nil)
    currentTestCase.waitForExpectations(timeout: timeout) { [weak currentTestCase] (error: Error?) in

        if (error != nil) {
            let appearMessage = "Failed to find \(String(describing: element)) after \(timeout) seconds."
            let disappearMessage = "Failed to see \(String(describing: element)) disappear after \(timeout) seconds."

            currentTestCase?.recordFailure(withDescription: ((toAppear == false) ? disappearMessage : appearMessage), inFile: file, atLine: line, expected: true)
        }
    }
}

答案 3 :(得分:0)

这是让它发挥作用的最终声明。再次感谢您的帮助!

(select end_part_id, sort_order, indented_lvl, comp_qty,
(select distinct first_value(i.comp_qty) over(order by i.sort_order desc,
TRIM(i.indented_lvl) desc)
from report_table i
where i.end_part_id = x.end_part_id
and i.sort_order < x.sort_order
and TRIM(i.level) < TRIM(x.level)) as "PARENT_QTY"
from report_table x
where x.end_part_id = 'XX')