WITH LatestJob AS (SELECT * FROM(SELECT
aId
, Position
, StartDate
, Enddate
,SpecializationId
,ROW_NUMBER() OVER (PARTITION BY aId ORDER BY
CASE WHEN Enddate IS NULL THEN 0 ELSE 1 END ASC,
(CAST(Enddate as datetime)) DESC) AS RN
FROM EmploymentDetails ed) E WHERE RN=1
)
,EarliestStart AS (SELECT aID
, sum(DATEDIFF(YEAR,Startdate, isnull(Enddate,getdate()))) AS YearsExperience
FROM EmploymentDetails GROUP BY aId)
SELECT u.Id,u.FirstName + ' ' + u.LastName AS NAME
, lj.Position AS LatestPosition
,aps.cId,aps.ApStatusID as ApplicationStatus,aps.sId as SpecializationId
, YearsExperience
, ad.ExpectedSalary, REPLACE(ISNULL(Address1, '') + ', ' + ISNULL(Address2, '') + ', ' + ISNULL(City, ''), ', ,', ',') AS Address
FROM Users u JOIN LatestJob lj ON u.Id = lj.aid
JOIN EarliestStart ye ON ye.aId = u.Id
JOIN ApplicantDetails ad ON ad.aId = u.Id
JOIN ApplicationStatus aps ON aps.aId=u.Id
WITH LatestJob AS (SELECT * FROM(SELECT
aId
, Position
, StartDate
, Enddate
,SpecializationId
,ROW_NUMBER() OVER (PARTITION BY aId ORDER BY
CASE WHEN Enddate IS NULL THEN 0 ELSE 1 END ASC,
(CAST(Enddate as datetime)) DESC) AS RN
FROM EmploymentDetails ed) E WHERE RN=1
)
,EarliestStart AS (SELECT aID
, sum(DATEDIFF(YEAR,Startdate, isnull(Enddate,getdate()))) AS YearsExperience
FROM EmploymentDetails GROUP BY aId)
SELECT u.Id,u.FirstName + ' ' + u.LastName AS NAME
, lj.Position AS LatestPosition
, YearsExperience
, ad.ExpectedSalary, REPLACE(ISNULL(Address1, '') + ', ' + ISNULL(Address2, '') + ', ' + ISNULL(City, ''), ', ,', ',') AS Address
FROM Users u JOIN LatestJob lj ON u.Id = lj.aid
JOIN EarliestStart ye ON ye.aId = u.Id
JOIN ApplicantDetails ad ON ad.aId = u.Id
where u.RoleId=3
and u.UserStatusId=1
and lj.SpecializationId=38 [email protected]
Q
如何工會此查詢
1
A
回答
0
我認爲這可能做到這一點(我還做了一點更具可讀性):
WITH LatestJob AS (
SELECT * FROM
(SELECT
aId
,Position
,StartDate
,Enddate
,SpecializationId
,ROW_NUMBER() OVER (PARTITION BY aId ORDER BY
(CASE WHEN Enddate IS NULL THEN 0 ELSE 1 END) ASC
,CAST(Enddate AS datetime) DESC
) AS RN
FROM
EmploymentDetails ed
) E
WHERE RN = 1
)
,EarliestStart AS (
SELECT
aID
,SUM(DATEDIFF(YEAR, Startdate, ISNULL(Enddate,GETDATE()))) AS YearsExperience
FROM
EmploymentDetails
GROUP BY
aId
)
(
SELECT
u.Id
,u.FirstName + ' ' + u.LastName AS NAME
,lj.Position AS LatestPosition
,aps.cId
,aps.ApStatusID AS ApplicationStatus
,aps.sId AS SpecializationId
,YearsExperience
,ad.ExpectedSalary
,REPLACE(ISNULL(Address1, '') + ', ' + ISNULL(Address2, '') + ', ' + ISNULL(City, ''), ', ,', ',') AS Address
FROM
Users u
JOIN
LatestJob lj ON u.Id = lj.aid
JOIN
EarliestStart ye ON ye.aId = u.Id
JOIN
ApplicantDetails ad ON ad.aId = u.Id
JOIN
ApplicationStatus aps ON aps.aId=u.Id
)
UNION ALL
(
SELECT
u.Id
,u.FirstName + ' ' + u.LastName AS NAME
,lj.Position AS LatestPosition
,YearsExperience
,ad.ExpectedSalary
,REPLACE(ISNULL(Address1, '') + ', ' + ISNULL(Address2, '') + ', ' + ISNULL(City, ''), ', ,', ',') AS Address
FROM
Users u
JOIN
LatestJob lj ON u.Id = lj.aid
JOIN
EarliestStart ye ON ye.aId = u.Id
JOIN
ApplicantDetails ad ON ad.aId = u.Id
WHERE
u.RoleId = 3
AND u.UserStatusId = 1
AND lj.SpecializationId = 38 [email protected]
)
希望我沒有失去任何信息或沿着錯誤的複製下來辦法。它只解析正確,所以我不知道它是否會真正返回你想要的。
+0
非常感謝你的工作 – user335160 2010-07-29 03:23:24
0
嘗試以下操作:
WITH LatestJob AS (
SELECT * FROM (
SELECT
aId
, Position
, StartDate
, Enddate
,SpecializationId
,ROW_NUMBER() OVER (PARTITION BY aId ORDER BY
CASE WHEN Enddate IS NULL THEN 0 ELSE 1 END ASC,
(CAST(Enddate as datetime)) DESC) AS RN
FROM EmploymentDetails ed) E WHERE RN=1
)
), EarliestStart AS (
SELECT aID, sum(DATEDIFF(YEAR,Startdate, isnull(Enddate,getdate()))) AS YearsExperience
FROM EmploymentDetails GROUP BY aId
)
SELECT u.Id,u.FirstName + ' ' + u.LastName AS NAME
, lj.Position AS LatestPosition
,aps.cId,aps.ApStatusID as ApplicationStatus,aps.sId as SpecializationId
, YearsExperience
, ad.ExpectedSalary, REPLACE(ISNULL(Address1, '') + ', ' + ISNULL(Address2, '') + ', ' + ISNULL(City, ''), ', ,', ',') AS Address
FROM Users u
JOIN LatestJob lj ON u.Id = lj.aid
JOIN EarliestStart ye ON ye.aId = u.Id
JOIN ApplicantDetails ad ON ad.aId = u.Id
JOIN ApplicationStatus aps ON aps.aId=u.Id
UNION ALL
SELECT u.Id,u.FirstName + ' ' + u.LastName AS NAME
, lj.Position AS LatestPosition
, '' as cId, '' as ApplicationStatus, '' as SpecializationId
, YearsExperience
, ad.ExpectedSalary, REPLACE(ISNULL(Address1, '') + ', ' + ISNULL(Address2, '') + ', ' + ISNULL(City, ''), ', ,', ',') AS Address
FROM Users u
JOIN LatestJob lj ON u.Id = lj.aid
JOIN EarliestStart ye ON ye.aId = u.Id
JOIN ApplicantDetails ad ON ad.aId = u.Id
WHERE u.RoleId=3
AND u.UserStatusId=1
AND lj.SpecializationId=38 [email protected]
相關問題
- 1. 此查詢如何工作?
- 2. 你會如何優化此MySQL查詢
- 3. PDO預處理語句不會與此查詢有此查詢
- 4. 如何優化此查詢?
- 5. 如何優化此查詢?
- 6. 如何加快此查詢?
- 7. 如何編寫此查詢?
- 8. 如何回覆此查詢?
- 9. 如何創建此查詢
- 10. 如何加快此查詢?
- 11. 如何優化此查詢?
- 12. 如何修改此查詢?
- 13. 如何優化此查詢?
- 14. 如何編寫此查詢?
- 15. 如何優化此查詢?
- 16. 如何簡化此查詢?
- 17. 如何加入此查詢
- 18. 如何結合此查詢
- 19. 如何撰寫此查詢?
- 20. 如何改進此查詢?
- 21. 如何運行此查詢?
- 22. 如何更正此查詢?
- 23. 如何優化此查詢?
- 24. 如何更改此查詢?
- 25. 如何優化此查詢?
- 26. 如何解決此查詢?
- 27. 如何優化此查詢?
- 28. 如何執行此查詢?
- 29. 如何訂購此查詢?
- 30. 如何訂購此查詢?
您是否嘗試過兩個查詢之間寫UNION? – 2010-07-29 02:42:30
是的,我試過了,但我得到了一個錯誤 – user335160 2010-07-29 02:46:26
消息156,級別15,狀態1,行26 關鍵字'WITH'附近的語法不正確。 Msg 319,Level 15,State 1,Line 26 關鍵字'with'附近的語法不正確。如果此語句是公用表表達式或xmlnamespaces子句,則前面的語句必須以分號結尾。 – user335160 2010-07-29 02:47:08