如何在Oracle中编写查询

时间:2016-02-26 19:24:23

标签: sql oracle

我有一个连接两个表的查询,结果显示如下:

ID   Provider  Vendor   Location  Funding
-----------------------------------------
1     AAAA     ABC       Dallas   NSF
2     AAAA     ABC       Houston  NIH
3     CCCC     TEST      NYC      NASA
4     FFFF     Exam      Boston   FDA
5     FFFF     Appl      Miami     FDA
6     EEEE     Google    San Jose  FDA

我的问题是,我想只生成同时包含资金的ID。例如:

输出:

ID   Provider  Vendor   Location  Funding
-----------------------------------------

1     AAAA     ABC       Dallas   NSF
2     AAAA     ABC       Houston  NIH

数据列是几千条记录,因此我正在寻找最佳的SQL方法。

感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

如果您想要原始行,可以使用窗口函数执行此操作:

select t.*
from (select t.*, count(distinct funding) over (partition by provider) as cnt
      from t
     ) t
where cnt > 1;

如果您只想要提供者,请使用聚合:

select provider
from t
where funding in ('NSF', 'NIH')
group by provider
having count(distinct funding) = 2;

答案 1 :(得分:0)

这应该这样做:

假设:通过ID,您可以参考提供商

示例数据:

CREATE TABLE #temp
                  (
             ID INT, Provider VARCHAR(10), Vendor VARCHAR(10), Location VARCHAR(10), Funding VARCHAR(10)
                  );

INSERT INTO #temp
VALUES
       (1, 'AAAA', 'ABC', 'Dallas', 'NSF'
       ),
       (2, 'AAAA', 'ABC', 'Houston', 'NIH'
       ),
       (3, 'CCCC', 'TEST', 'NYC', 'NASA'
       ),
       (4, 'FFFF', 'Exam', 'Boston', 'FDA'
       ),
       (5, 'FFFF', 'Appl', 'Miami', 'FDA'
       ),
       (6, 'EEEE', 'Google', 'San Jose', 'FDA'
       );

查询您是否只想要拥有NIH或NSF的ID:

SELECT ID, Provider, Vendor, Location, Funding
FROM #temp
WHERE Funding IN
                 ('NSF', 'NIH'
                 );

结果查询1:

listening port

查询您是否想要同时拥有NIH和NSF的ID:

;WITH CTE
     AS
        (SELECT DISTINCT
                Provider, Funding, ROW_NUMBER() OVER
                                                    (PARTITION BY Provider ORDER BY Funding
                                                    ) AS rn
         FROM #temp
         WHERE Funding IN
                          ('NSF', 'NIH'
                          )
        )
     SELECT *
     FROM #temp
     WHERE Provider IN
                      (
                       SELECT DISTINCT
                              Provider
                       FROM CTE
                       WHERE rn = 2
                      );

结果查询2:

enter image description here