如何在Oracle中使用分区获取记录数

时间:2018-11-12 01:36:33

标签: sql oracle oracle12c window-functions

我有以下查询:

SELECT 
    F.IID, 
    F.E_NUM AS M_E_NUM, 
    MCI.E_NUM AS MCI_E_NUM, 
    F.C_NUM AS M_C_NUM, 
    MCI.C_NUM AS MCI_C_NUM,
    F.ET_ID AS M_ET_ID, 
    EDIE.ET_ID AS ED_INDV_ET_ID,
    COUNT(*) OVER (PARTITION BY F.IID) IID_COUNT
FROM FT_T F JOIN CEMEI_T MCI ON F.IID = MCI.IID
    JOIN EDE_T EDE ON MCI.E_NUM = EDE.E_NUM
    JOIN EDIE_T EDIE ON EDIE.IID = F.IID AND EDIE.ET_ID = EDE.ET_ID
WHERE 
    F.DEL_F = 'N'
    AND MCI.EFF_END_DT IS NULL
    AND MCI.TOS = 'BVVB'
    AND EDE.PTEND_DT IS NULL
    AND EDE.DEL_S = 'N'
    AND EDE.CUR_IND = 'A'
    AND EDIE.TAR_N = 'Y'
    AND F.IID IN
        (
         SELECT DISTINCT IID 
         FROM FT_T 
         WHERE GROUP_ID = 'BG' 
            AND DEL_F = 'N' 
            AND (IID, E_NUM) NOT IN 
                (
                 SELECT IID, E_NUM FROM CEMEI_T 
                 WHERE TOS = 'BVVB' AND EFF_END_DT IS NULL
                )
        );

我基本上是从几个表中获取信息,并为它们创建一个平面记录。

一切正常,除了现在我需要找出FT_T表中是否有两个记录具有相同的IID并将其显示为结果集的一部分。

我尝试使用分区,但是即使IID中有2条记录具有相同的FT_T记录,结果集中的所有行仍返回一个计数。

我最初说我要从几个表中收集信息的原因是由于这样的事实:FT_T如果没有两个记录可用于同一IID,则可能没有我需要的所有信息。 ,因此我必须从查询中JOIN的其他表中检索它们。但是,我需要知道哪个FT_T.IIDFT_T中有两条记录(或多于一条)。

2 个答案:

答案 0 :(得分:1)

也许您需要计算join并进行过滤之前的计数:

SELECT . . .
FROM (SELECT F.*,
             COUNT(*) OVER (PARTITION BY F.IID) as IID_CNT
      FROM FT_T F
     ) JOIN
     CEMEI_T MCI
     ON F.IID = MCI.IID JOIN
     EDE_T EDE
     ON MCI.E_NUM = EDE.E_NUM JOIN
     EDIE_T EDIE
     ON EDIE.IID = F.IID AND EDIE.ET_ID = EDE.ET_ID
 . . .

答案 1 :(得分:0)

这仅仅是评论/观察,但需要格式化

var data = [{ name: 'Dallas Stars', roster: ['Alexander Radulov', 'Ben Bishop', 'Jamie Benn', 'Tyler Pitlick', 'Miro Heiskanen' ] }, { name: 'Los Angeles Kings', roster: ['Jonathan Quick', 'Jonny Brodzinski', 'Oscar Fantenberg' ] }, { name: 'San Jose Sharks', roster: ['Joe Thornton', 'Brent Burns', 'Joe Pavelski', 'Antti Suomela' ] }, { name: 'Columbus Blue Jackets', roster: ['Sonny Milano', 'Brandon Dubinsky', 'Nick Foligno', 'Pierre-Luc Dubois' ] } ]; var res = data .map(({name, roster}) => roster.map(playerName => ({name, playerName}))) .flat(); console.log(res);in(...)select distinct一起使用似乎很复杂,如果某些值为NULL可能会成为问题。我建议您考虑改用EXISTS和NOT EXISTS。例如

not in(...,...)