0

我试图以这样的方式结合这两个查询来确定拥有设备的 PI 是谁(价值超过 10 万美元)。我有能力找到一个 PI 拥有的大于 100k 的所有设备。我也有能力查看所有的 PI。我只是无法将这两个查询结合起来。我尝试过使用 WHERE 子查询和 EXIST 子查询。我希望能够找到查询#2 中存在 PI 的所有设备(与其 PI 所有者匹配)。

查询 #1 用于查找特定 PI 的设备

select Account_No,Inventory_No,Building_No,Room_No,CDDEPT,Location,Normalized_MFG,Manufacturer_Name,Normalized_Model,Name,Serial_Code,CONCAT( '$', FORMAT( Cost, 2 ) ) as Cost, Equipment_Inventory_Normalized.Active 
from Temp_Equipment_Inventory.Equipment_Inventory_Normalized, `paul`.`ROOM`, `paul`.`BLDG`, `paul`.`LABORATORY`, `paul`.`PERSON` 
where (`PERSON`.`ID` = `LABORATORY`.`PI_ID` OR `PERSON`.`ID` = `LABORATORY`.`SUPV_ID`) 
AND `LABORATORY`.`RM_ID` = `ROOM`.`ID` 
AND `LABORATORY`.`ACTIVE` = '1' 
AND `ROOM`.`BLDG_ID` = `BLDG`.`ID` 
AND ((
`BLDG`.`BLDGNUM` = Equipment_Inventory_Normalized.Building_No 
AND Equipment_Inventory_Normalized.Actual_Building IS NULL 
AND (`BLDG`.`BLDGNUM` != '1023' AND `LABORATORY`.`OTHER_LEVEL` != '1' AND `ROOM`.`RMNUM` != '0199')
)OR (
`BLDG`.`BLDGNUM` = Equipment_Inventory_Normalized.Actual_Building AND 
(`BLDG`.`BLDGNUM` != '1023' AND `LABORATORY`.`OTHER_LEVEL` != '1' AND `ROOM`.`RMNUM` != '0199')
)) 
AND ((
`ROOM`.`RMNUM` = Equipment_Inventory_Normalized.Room_No 
AND Equipment_Inventory_Normalized.Actual_Room IS NULL
)OR (
`ROOM`.`RMNUM` = Equipment_Inventory_Normalized.Actual_Room
)) 
AND Equipment_Inventory_Normalized.Active !=0 
AND SurplusPending != '1'
AND Cost >= 100000 
AND `PERSON`.`CANNUM`='810010787'

查询 2 找到所有 PI

select distinct i.CAN
from CGWarehouse.CCGV10WC w
inner join CGWarehouse.CCGV10IC i
on w.PROJECT_NUMBER=i.SPONSORED_PROJECT
and w.SEQUENCE_NUMBER=i.PROJECT_SEQUENCE
where w.STATUS='A'
and i.PRIN_INVEST_CODE='Y'
and i.DEL_CODE!='Y'
and i.CAN IS NOT NULL
4

1 回答 1

2

也许您正在 WHERE 子句中寻找IN关键字?

请原谅我,但我不得不稍微清理一下您的查询格式......这有点像我的强迫症:

SELECT
    Account_No,
    Inventory_No,
    Building_No,
    Room_No,
    CDDEPT,
    Location,
    Normalized_MFG,
    Manufacturer_Name,
    Normalized_Model,
    Name,
    Serial_Code,
    CONCAT('$', FORMAT( Cost, 2 )) AS Cost,
    Equipment_Inventory_Normalized.Active
FROM
    Temp_Equipment_Inventory.Equipment_Inventory_Normalized a,
    `paul`.`ROOM`,
    `paul`.`BLDG`,
    `paul`.`LABORATORY`,
    `paul`.`PERSON` 
WHERE
    (`PERSON`.`ID` = `LABORATORY`.`PI_ID` OR `PERSON`.`ID` = `LABORATORY`.`SUPV_ID`) 
    AND `LABORATORY`.`RM_ID` = `ROOM`.`ID` 
    AND `LABORATORY`.`ACTIVE` = '1' 
    AND `ROOM`.`BLDG_ID` = `BLDG`.`ID` 
    AND (
        (
            `BLDG`.`BLDGNUM` = Equipment_Inventory_Normalized.Building_No 
            AND Equipment_Inventory_Normalized.Actual_Building IS NULL 
            AND `BLDG`.`BLDGNUM` != '1023'
            AND `LABORATORY`.`OTHER_LEVEL` != '1'
            AND `ROOM`.`RMNUM` != '0199'
        ) OR (
            `BLDG`.`BLDGNUM` = Equipment_Inventory_Normalized.Actual_Building
            AND `BLDG`.`BLDGNUM` != '1023'
            AND `LABORATORY`.`OTHER_LEVEL` != '1'
            AND `ROOM`.`RMNUM` != '0199'
        )
    ) 
    AND (
        (
            `ROOM`.`RMNUM` = Equipment_Inventory_Normalized.Room_No 
            AND Equipment_Inventory_Normalized.Actual_Room IS NULL
        ) OR (
            `ROOM`.`RMNUM` = Equipment_Inventory_Normalized.Actual_Room
        )
    ) 
    AND Equipment_Inventory_Normalized.Active !=0 
    AND SurplusPending != '1'
    AND Cost >= 100000 
    AND `PERSON`.`CANNUM` IN ( /* this assumes that the `PERSON`.`CANNUM` column matches up with the CGWarehouse.CCGV10IC.CAN column */
        SELECT DISTINCT i.CAN
        FROM
            CGWarehouse.CCGV10WC w
            INNER JOIN CGWarehouse.CCGV10IC i ON w.PROJECT_NUMBER=i.SPONSORED_PROJECT AND w.SEQUENCE_NUMBER=i.PROJECT_SEQUENCE
        WHERE
            w.STATUS='A'
            AND i.PRIN_INVEST_CODE='Y'
            AND i.DEL_CODE!='Y'
            AND i.CAN IS NOT NULL
    )
于 2012-05-08T14:36:16.133 回答