0
我很努力地將查詢與另一個查看組合成單個查詢(或spw)。SQL將來自子查詢的多行組合到一個字段中
首先查詢:
SELECT 'Status: ' + Status + CHAR(10) + 'Crew Information:' + CHAR(10) + 'Instructor: ' + IP + CHAR(10) + 'Student: ' + SP + CHAR(10) + CASE WHEN ST.ACM1 = 'NA' THEN '' ELSE 'ACM1: ' + ST.ACM1 + Char(10)
END + CASE WHEN ST.ACM2 = 'NA' THEN '' ELSE 'ACM2: ' + ST.ACM2 + Char(10) END + 'Lesson: ' + Lesson + CHAR(10)
+ CASE WHEN ST.ScheduleRemarks = '' THEN '' ELSE ' REM:' + ST.ScheduleRemarks + Char(10) END + 'Equipment: ' + EquipmentName + ' (' + Callsign + ')' + CHAR(10) + 'Start: ' + CONVERT(VARCHAR(10),
ScheduleTO, 104) + ' - ' + CONVERT(VARCHAR(5), ScheduleTO, 108) + ' UTC' + CHAR(10) + 'Start: ' + CONVERT(VARCHAR(10), ScheduleTD, 104) + ' - ' + CONVERT(VARCHAR(5), ScheduleTD, 108) + ' UTC' + CHAR(10)
+ CASE WHEN ST.ATC = 'NA' THEN '' ELSE ST.ATC END AS SInfo, IDIP, IDSP, IDA1, IDA2
FROM (SELECT CASE WHEN Schedule.StatusRelease = 1 THEN 'OK' ELSE CASE WHEN Schedule.StatusRequest = 1 THEN 'STBY' ELSE 'N/A' END END AS Status, dbo.Schedule.ContactIDIP,
dbo.Schedule.ContactIDSP, dbo.MasterLesson.Lesson, dbo.Equipment.EquipmentName, dbo.CallSignList.Callsign, dbo.Schedule.Meeting, dbo.Schedule.ScheduleRemarks,
dbo.Schedule.StatusRelease, dbo.Schedule.StatusRequest, dbo.Schedule.ScheduleDay, dbo.ContactList.FullNameTLC AS IP, ContactList1.FullNameTLC AS SP,
CASE WHEN Schedule.ContactIDACM1 = 0 THEN 'NA' ELSE ContactList2.FullNameTLC END AS ACM1,
CASE WHEN Schedule.ContactIDACM2 = 0 THEN 'NA' ELSE ContactList3.FullNameTLC END AS ACM2, dbo.Schedule.ScheduleTO, dbo.Schedule.ScheduleTD,
CASE WHEN Schedule.RouteID = 0 THEN 'NA' ELSE 'Route: ' + Airport.ICAO + ' - ' + Airport1.ICAO + ' - ' + Airport2.ICAO END AS ATC, dbo.ContactList.Id AS IDIP, ContactList1.Id AS IDSP,
ContactList2.Id AS IDA1, ContactList3.Id AS IDA2
FROM dbo.Schedule INNER JOIN
dbo.StudentLesson ON dbo.Schedule.ScheduleLessonID = dbo.StudentLesson.StudentLessonID INNER JOIN
dbo.MasterLesson ON dbo.StudentLesson.LessonID = dbo.MasterLesson.ID INNER JOIN
dbo.Equipment ON dbo.Schedule.EquipmentID = dbo.Equipment.ID LEFT OUTER JOIN
dbo.CallSignList ON dbo.CallSignList.ID = dbo.Schedule.CallSignListID LEFT OUTER JOIN
dbo.Route ON dbo.Route.ID = dbo.Schedule.RouteID INNER JOIN
dbo.ContactList ON dbo.ContactList.Id = dbo.Schedule.ContactIDIP INNER JOIN
dbo.ContactList AS ContactList1 ON ContactList1.Id = dbo.Schedule.ContactIDSP LEFT OUTER JOIN
dbo.ContactList AS ContactList2 ON dbo.Schedule.ContactIDACM1 = ContactList2.Id LEFT OUTER JOIN
dbo.ContactList AS ContactList3 ON dbo.Schedule.ContactIDACM2 = ContactList3.Id LEFT OUTER JOIN
dbo.Airport ON dbo.Route.AirportID_Dep = dbo.Airport.ID LEFT OUTER JOIN
dbo.Airport AS Airport1 ON dbo.Route.AirportID_Via = Airport1.ID LEFT OUTER JOIN
dbo.Airport AS Airport2 ON dbo.Route.AirportID_Arr = Airport2.ID
WHERE (dbo.Schedule.StatusRelease = 1) AND (dbo.Schedule.ScheduleDay > GETDATE() - 45) OR
(dbo.Schedule.ScheduleDay > GETDATE() - 45) AND (dbo.Schedule.StatusRequest = 1)) AS ST
第二個查詢:
SELECT DISTINCT
ST.IDIP,
substring((Select + CHAR(10) +STn1.SInfo AS [text()]
FROM
dbo.ST1 STn1
WHERE
STn1.IDIP = ST.IDIP
ORDER BY
STn1.IDIP
FOR XML PATH ('')), 2, 1000) [ScheduleInfo]
FROM dbo.ST1 ST
我的問題是 - 第一個查詢保存爲視圖。所以第二個查詢工作得很好。
但是 - 如何將它們合併成單個查詢? 當我選擇第一個查詢作爲'STsub'作爲子查詢
並用STSub替換dbo.ST1 - 我得到一個無效的對象或錯誤。
我做錯了什麼?
查詢的性能如何?我在連接中至少計算了十幾個表。我只能想知道查詢計劃是什麼樣子? –