1

したがって、同じテーブルから同じ列を返す2つのサブクエリがあります

クエリ#1:

SELECT E.Id,E.Title,E.LocationId,P.LocationId,E.DepartmentId,P.DepartmentId,E.DateCreated,E.IsActive,E.IsHotJob,E.RequisitionId,E.RequisitionIdString,E.RewardSettingId,E.EmploymentOpportunityStatusId 
FROM EmploymentOpportunities E, Profiles P
WHERE E.EmploymentOpportunityStatusId = 9 AND E.IsActive = 1 AND E.IsHotjob = 1  
     AND P.Id = 'C5F07EBB-CE81-4133-A462-241A5F84D418' AND (P.DepartmentId != E.DepartmentId AND P.LocationId != E.LocationId)
ORDER BY E.DateCreated DESC

クエリ#2:

SELECT E.Id,E.Title,E.LocationId,P.LocationId,E.DepartmentId,P.DepartmentId,E.DateCreated,E.IsActive,E.IsHotJob,E.RequisitionId,E.RequisitionIdString,E.RewardSettingId,E.EmploymentOpportunityStatusId 
FROM EmploymentOpportunities E, Profiles P
WHERE E.EmploymentOpportunityStatusId = 9 AND E.IsActive = 1 AND E.IsHotjob = 0 AND 
      P.Id = 'C5F07EBB-CE81-4133-A462-241A5F84D418' AND (P.DepartmentId = E.DepartmentId OR P.LocationId = E.LocationId)
ORDER BY E.DateCreated DESC

この2つのクエリを1つにまとめたいのですが、順序を維持したいので、どういうわけかクエリ#1をクエリ#2にスタックします。

これは可能ですか?

4

2 に答える 2

0
SELECT 1 SetNumber, E.Id,E.Title,E.LocationId,P.LocationId,E.DepartmentId,
  P.DepartmentId,E.DateCreated,E.IsActive,E.IsHotJob,E.RequisitionId,
  E.RequisitionIdString,E.RewardSettingId,E.EmploymentOpportunityStatusId
FROM EmploymentOpportunities E, Profiles P
WHERE E.EmploymentOpportunityStatusId = 9 AND E.IsActive = 1 
  AND E.IsHotjob = 1 P.Id = 'C5F07EBB-CE81-4133-A462-241A5F84D418' 
  AND (P.DepartmentId != E.DepartmentId AND P.LocationId != E.LocationId)

union all

SELECT 2, E.Id,E.Title,E.LocationId,P.LocationId,E.DepartmentId,P.DepartmentId,
  E.DateCreated,E.IsActive,E.IsHotJob,E.RequisitionId,E.RequisitionIdString,
  E.RewardSettingId,E.EmploymentOpportunityStatusId
FROM EmploymentOpportunities E, Profiles P
WHERE E.EmploymentOpportunityStatusId = 9 AND E.IsActive = 1 
  AND E.IsHotjob = 0 AND P.Id = 'C5F07EBB-CE81-4133-A462-241A5F84D418' 
  AND (P.DepartmentId = E.DepartmentId OR P.LocationId = E.LocationId)

ORDER BY SetNumber, DateCreated desc
于 2013-03-25T18:48:52.843 に答える
0

使用するだけUNION ALL

SELECT * FROM
(
    Your Query1

    UNION ALL      // do not use order by here

    Your Query2 
) AS someName
ORDER BY yourColumn DESC

-を使用することもできますUNIONが、重複がある場合は除外されます。

于 2013-03-25T18:50:06.277 に答える