如何使用子查询过滤两次?

时间:2018-01-09 15:16:20

标签: mysql sql sql-server sql-server-2012

我试图选择约束相互矛盾的数据。例如,有一个数据集包含status_desc为' 90 - Lost Opportunity'当Sproject_Number不为null时。我想过滤两次,所以首先我将所有Sproject_number都设为null然后我能够再次过滤此查询以获得一个表格,其中status_desc是' 90-Lost Opportunity'

SELECT ProjectNumber,
       PName
FROM
(
    SELECT DISTINCT
           T0.project_number_ext AS ProjectNumber,
           T0.project_key AS PKey,
           T0.sproject_number AS sPNumber,
           T0.project_number AS PNumber,
           T0.schedule_finish AS SFinish,
           T0.status_desc AS SDesc,
           T0.Project_name AS PName
    FROM trimergo.rpt_getProjectPOC T0
    WHERE T0.sproject_number IS NULL
)
GROUP BY ProjectNumber,
         PName;

2 个答案:

答案 0 :(得分:0)

我想你只需要这个。子查询不需要。

Auth::user()

答案 1 :(得分:0)

这段代码有效,我试图选择约束相互矛盾的数据。例如,当Sproject_Number不为null时,有一些数据集包含status_desc为'90 - Lost Opportunity'。我想过滤两次,所以首先我得到所有的空数据集,然后我过滤该查询以关闭我需要的数据集

SELECT
T1.ProjectNumber , T1.PName, T1.SDesc

FROM
( 

SELECT DISTINCT 
 T0.project_number_ext as ProjectNumber
,T0.project_key as PKey
,T0.sproject_number as sPNumber 
,T0.project_number as PNumber
,T0.schedule_finish as SFinish
,T0.status_desc as SDesc
,T0.Project_name as PName

From trimergo.rpt_getProjectPOC T0

WHERE T0.sproject_number IS NULL ) AS T1

Where T1.SDesc LIKE '10 - In Proposal%'
OR T1.SDesc like '90-Lost Opportunity%'

Group by ProjectNumber, PName, SDesc