0

第一个查询是我需要的 15 英里半径范围内公司的所有信息。

 SELECT DISTINCT CI.co,
                CI.name,
                CI.address1,
                CI.address2,
                CI.city,
                CI.state,
                CI.zip,
                CI.contact1,
                CI.contact1email,
                CI.contact2,
                CI.contact2email,
                CI.contact3,
                contact3email,
                Count(EI.id) AS ActiveEE
FROM   cinfo CI
       INNER JOIN einfo EI
               ON CI.co = EI.co
WHERE  NOT CI.co IN (SELECT co
                     FROM   scompanysetdetail
                     WHERE  companyset = 'REF-GCohen')
       AND enddate IS NULL
       AND EI.empstatus = 'A'
       AND CI.zip IN ( *zip codes for the 15 mile radius* )
GROUP  BY CI.co,
          CI.name,
          CI.address1,
          CI.address2,
          CI.city,
          CI.state,
          CI.zip,
          CI.contact1,
          CI.contact1email,
          CI.contact2,
          CI.contact2email,
          CI.contact3,
          CI.contact3email  

第二个查询为我提供了按公司划分的前 10 名带薪员工

 WITH cterownum
     AS (SELECT co,
                id,
                ename,
                title,
                hiredate,
                salary,
                Dense_rank()
                  OVER(
                    partition BY co
                    ORDER BY salary DESC) AS RowNum
         FROM   cps_wss_emplist)
SELECT *
FROM   cterownum
WHERE  rownum <= 10
ORDER  BY co,
          rownum ASC  

如何将这两个查询合并为一个?

4

3 回答 3

3
;WITH FirstCTE AS
(
SELECT DISTINCT CI.co,
            CI.name,
            CI.address1,
            CI.address2,
            CI.city,
            CI.state,
            CI.zip,
            CI.contact1,
            CI.contact1email,
            CI.contact2,
            CI.contact2email,
            CI.contact3,
            contact3email,
            Count(EI.id) AS ActiveEE
FROM   cinfo CI
   INNER JOIN einfo EI
           ON CI.co = EI.co
WHERE  NOT CI.co IN (SELECT co
                 FROM   scompanysetdetail
                 WHERE  companyset = 'REF-GCohen')
   AND enddate IS NULL
   AND EI.empstatus = 'A'
   AND CI.zip IN ( *zip codes for the 15 mile radius* )
GROUP  BY CI.co,
      CI.name,
      CI.address1,
      CI.address2,
      CI.city,
      CI.state,
      CI.zip,
      CI.contact1,
      CI.contact1email,
      CI.contact2,
      CI.contact2email,
      CI.contact3,
      CI.contact3email  
)
,
SecondCTE AS
(
SELECT co,
            id,
            ename,
            title,
            hiredate,
            salary,
            Dense_rank()
              OVER(
                partition BY co
                ORDER BY salary DESC) AS RowNum
     FROM   cps_wss_emplist
),
ThirdCTE AS
(
SELECT *
FROM   cterownum
WHERE  rownum <= 10
)
SELECT    *
FROM        FirstCTE F
               JOIN ThirdCTE C
                   ON F.Co = S.Co
于 2013-08-13T12:44:23.743 回答
2

试试这个——

;WITH cterownum AS
(
    SELECT co
         , id
         , ename
         , title
         , hiredate
         , Salary
         , RowNum = DENSE_RANK() OVER (PARTITION BY co ORDER BY Salary DESC) 
    FROM cps_wss_emplist
)
SELECT  
      CI.*
    , EI.ActiveEE
    , ttt.*
FROM CInfo CI
JOIN (
    SELECT co, ActiveEE = COUNT(id)
    FROM EInfo
    WHERE empStatus = 'A'
    GROUP BY co
) EI ON CI.co = EI.co
JOIN cterownum ttt ON CI.co = ttt.co
WHERE NOT CI.co IN (
        SELECT co
        FROM SCompanySetDetail
        WHERE companySet = 'REF-GCohen'
    )
    AND EndDate IS NULL
    AND CI.zip IN ('')
    AND rownum <= 10
ORDER BY co, rownum
于 2013-08-13T12:43:18.633 回答
1

为了简单起见,我只需创建这两个子查询并将结果连接在一起,然后列出所有字段:

    Select -- list all your fields that exist in both temp tables

From
(
    SELECT DISTINCT CI.co,
        CI.name,
        CI.address1,
        CI.address2,
        CI.city,
        CI.state,
        CI.zip,
        CI.contact1,
        CI.contact1email,
        CI.contact2,
        CI.contact2email,
        CI.contact3,
        contact3email,
        Count(EI.id) AS ActiveEE
    FROM   cinfo CI
    INNER JOIN einfo EI ON CI.co = EI.co
    WHERE  NOT CI.co IN
    (
        SELECT co
        FROM   scompanysetdetail
        WHERE  companyset = 'REF-GCohen')
        AND enddate IS NULL
        AND EI.empstatus = 'A'
        AND CI.zip IN ( *zip codes for the 15 mile radius* )
    )
    GROUP  BY CI.co,
              CI.name,
              CI.address1,
              CI.address2,
              CI.city,
              CI.state,
              CI.zip,
              CI.contact1,
              CI.contact1email,
              CI.contact2,
              CI.contact2email,
              CI.contact3,
              CI.contact3email 
) a
Inner Join
(         
    ;WITH cterownum
         AS (SELECT co,
                    id,
                    ename,
                    title,
                    hiredate,
                    salary,
                    Dense_rank()
                      OVER(
                        partition BY co
                        ORDER BY salary DESC) AS RowNum
             FROM   cps_wss_emplist)
    SELECT *
    FROM   cterownum
    WHERE  rownum <= 10
    ORDER  BY co,
              rownum ASC
) b On b.co = a.co
于 2013-08-13T12:42:23.480 回答