第一个查询是我在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
第二个查询给了我公司前十名付薪员工
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
如何将这两个查询合并为一个?
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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