Oracle SQL - 搜索特定记录

时间:2018-03-20 08:38:14

标签: sql oracle

我想知道如何执行以下操作:

所以,我这里有一个示例表:

ID:   Name:          Occupation:     Startdate:    Enddate: 
1 John Journalist 01/01/2000 01/01/2000 2 John Baker 01/01/2002 01/01/2004 3 John Butcher 01/01/2004 (null) 4 Mark Baker 05/03/2000 (null) 5 Petrus Lawyer 01/01/2001 01/01/2002 6 Petrus Baker 01/01/2002 (null) 7 Andre Journalist 01/01/1999 01/01/2000 8 Andre Baker 01/01/2000 (null)

所以,这就是我想要的:我想找到所有首先成为记者然后转而成为面包师的人的名字。所以,我不想找到那些只是面包师的人,也不想找到首先是记者,然后是面包师的人,然后再去找屠夫。所以,基本上,我希望查询返回Andre的两个记录。

有办法做到这一点吗?

编辑:我应该提一下这个数据库不被编辑,所以没有插入或任何会以任何方式改变数据库的东西,我想要的是尽可能简单的选择查询,如果可能的话。

4 个答案:

答案 0 :(得分:1)

这会使用您的数据技巧,即前一个职业的this.setState({experience: response.data.experience}) 与下一个职业的enddate匹配:

startdate

对于那些以记者开始,成为屠夫,然后成为面包师的人来说,这不会起作用。但根据你的评论,这不是你想要做的事情。

在现实生活中,有界日期范围通常不会重叠。也就是说,with candidates as ( select name, enddate from your_table where occupation = 'Journalist' intersect select name, startdate from your_table where occupation = 'Baker' and enddate is null -- this is current record ) select name from candidates / 应为startdate。毕竟,人们通常不会在午餐时间开始新工作!

答案 1 :(得分:1)

func interpretRawFrameData(_ frameData: inout FrameData) -> CMSampleBuffer? {
    let size = UInt32(frameData.count)

    var naluType = frameData[4] & 0x1F
    var frame: CMSampleBuffer?

    // The start indices for nested packets. Default to 0.
    var ppsStartIndex = 0
    var frameStartIndex = 0

    switch naluType {
    // SPS
    case 7:
        print("===== NALU type SPS")
        for i in 4..<40 {
            if frameData[i] == 0 && frameData[i+1] == 0 && frameData[i+2] == 0 && frameData[i+3] == 1 {
                ppsStartIndex = i
                spsSize = i - 4 // Does not include the size of the header
                sps = Array(frameData[4..<i])

                // Set naluType to the nested PPS packet's NALU type
                naluType = frameData[i + 4] & 0x1F
                break
            }
        }
        // If nested frame was found, fallthrough
        if ppsStartIndex != 0 { fallthrough }
    // PPS
    case 8:
        print("===== NALU type PPS")
        for i in ppsStartIndex+4..<ppsStartIndex+34 {
            if frameData[i] == 0 && frameData[i+1] == 0 && frameData[i+2] == 0 && frameData[i+3] == 1 {
                frameStartIndex = i
                ppsSize = i - spsSize - 8 // Does not include the size of the header. Subtract 8 to account for both the SPS and PPS headers
                pps = Array(frameData[ppsStartIndex+4..<i])

                // Set naluType to the nested packet's NALU type
                naluType = frameData[i+4] & 0x1F
                break
            }
        }
        // If nested frame was found, fallthrough
        if frameStartIndex != 0 { fallthrough }
    // IDR frame
    case 5:
        print("===== NALU type IDR frame");

        // Replace start code with size
        let adjustedSize = size - UInt32(ppsSize) - UInt32(spsSize) - 8
        var blockSize = CFSwapInt32HostToBig(adjustedSize)
        memcpy(&frameData[frameStartIndex], &blockSize, 4)

        if createFormatDescription() {
            frame = decodeFrameData(Array(frameData[frameStartIndex...]))
        }
    // B/P frame
    default:
        print("===== NALU type B/P frame");

        // Replace start code with size
        var blockSize = CFSwapInt32HostToBig(size)
        memcpy(&frameData[frameStartIndex], &blockSize, 4)

        frame = decodeFrameData(Array(frameData[frameStartIndex...]))
        break;
    }

    return frame != nil ? frame : nil
}

答案 2 :(得分:0)

检查一下:

WITH tbl
     AS (SELECT '1'          id,
                'John'       name,
                'Journalist' occupation,
                '01/01/2000' startdate,
                '01/01/2000' enddate
           FROM DUAL
         UNION ALL
         SELECT '2', 'John', 'Baker', '01/01/2002', '01/01/2004' FROM DUAL
         UNION ALL
         SELECT '3',
                'John',
                'Butcher',
                '01/01/2004',
                NULL
           FROM DUAL
         UNION ALL
         SELECT '4',
                'Mark',
                'Baker',
                '05/03/2000',
                NULL
           FROM DUAL
         UNION ALL
         SELECT '5', 'Petrus', 'Lawyer', '01/01/2001', '01/01/2002' FROM DUAL
         UNION ALL
         SELECT '6',
                'Petrus',
                'Baker',
                '01/01/2002',
                NULL
           FROM DUAL
         UNION ALL
         SELECT '7',
                'Andre',
                'Journalist',
                '01/01/1999',
                '01/01/2000'
           FROM DUAL
         UNION ALL
         SELECT '8',
                'Andre',
                'Baker',
                '01/01/2000',
                NULL
           FROM DUAL)
SELECT id,
       name,
       occupation,
       startdate,
       enddate
  FROM (SELECT t.*,
               ROW_NUMBER () OVER (PARTITION BY name ORDER BY startdate) rn
          FROM tbl t) mn_tbl
 WHERE     name IN
              (SELECT name
                 FROM (SELECT t.*,
                              ROW_NUMBER ()
                                 OVER (PARTITION BY name ORDER BY startdate)
                                 rn
                         FROM tbl t)
                WHERE rn = 1 AND occupation = 'Journalist') -- First occupation was Journalist
       AND EXISTS
              (SELECT name
                 FROM (SELECT t.*,
                              ROW_NUMBER ()
                                 OVER (PARTITION BY name ORDER BY startdate)
                                 rn
                         FROM tbl t)
                WHERE rn = 2 AND name = mn_tbl.name AND occupation = 'Baker') -- Second occupation was Baker
       AND NOT EXISTS
              (SELECT name
                 FROM (SELECT t.*,
                              ROW_NUMBER ()
                                 OVER (PARTITION BY name ORDER BY startdate)
                                 rn
                         FROM tbl t)
                WHERE rn > 2 AND name = mn_tbl.name)    -- No more occupations

答案 3 :(得分:0)

这不是很漂亮,它不能超出两个职业......但它有效:)

计算明显将消除所有超过2个职业的人。然后,丑陋的伎俩是将记者照顾者的开始日期与面包师的开始日期进行比较。

select name, count(*)  
  from candidates
 group 
    by name
having count(distinct occupation) = 2 -- Only had two occupations
   and min(case when occupation = 'Journalist' then startdate end) /* First Journalist */
     < min(case when occupation = 'Baker'      then startdate end) /* Then baker */