将两个查询的结果合并为一行
Combining the results of two queries into one line
我有一个关于 UNION 的查询,要求在一行中输出结果。我尝试使用 group by,但发生错误。也许有一种解决方案可以将当前查询组合成一行?
select a.RecordID, (ACADEMICYEAR(sysdate) - c.year_begin + 1) COURSE, c.year_begin year_begin, c.Name StGroup
from fc_StudentOrders a
left join fc_OrderTypes b on b.TypeID=a.OrderType
left join fc_StudentGroups c on c.Code=a.StudentGroupID
left join RB_DEPARTMENTS d on d.code = c.faculty
where a.RecordID = 205838 and a.ORDERTYPE IN (15,56,109)
union all
select a.RecordID, (ACADEMICYEAR(sysdate) - c.year_begin + 1) COURSE, c.year_begin year_begin, c.Name StGroup
from fc_StudentOrders a
left join fc_OrderTypes b on b.TypeID=a.OrderType
left join fc_StudentGroups c on c.Code=a.StudentGroupID
left join RB_DEPARTMENTS d on d.code = c.faculty
where a.RecordID = 205838 and a.ORDERTYPE IN (1)
结果截图:
如果我没理解错的话,这可能是一种选择:将查询分成两部分(temp1
和 temp2
),然后 JOIN
它们:
WITH
temp1
AS
(SELECT a.RecordID,
(ACADEMICYEAR (SYSDATE) - c.year_begin + 1) COURSE,
c.year_begin year_begin,
c.Name StGroup
FROM fc_StudentOrders a
LEFT JOIN fc_OrderTypes b ON b.TypeID = a.OrderType
LEFT JOIN fc_StudentGroups c ON c.Code = a.StudentGroupID
LEFT JOIN RB_DEPARTMENTS d ON d.code = c.faculty
WHERE a.RecordID = 205838
AND a.ORDERTYPE IN (15, 56, 109)),
temp2
AS
(SELECT a.RecordID,
(ACADEMICYEAR (SYSDATE) - c.year_begin + 1) COURSE,
c.year_begin year_begin,
c.Name StGroup
FROM fc_StudentOrders a
LEFT JOIN fc_OrderTypes b ON b.TypeID = a.OrderType
LEFT JOIN fc_StudentGroups c ON c.Code = a.StudentGroupID
LEFT JOIN RB_DEPARTMENTS d ON d.code = c.faculty
WHERE a.RecordID = 205838
AND a.ORDERTYPE IN (1))
SELECT a.recordid,
a.course,
a.year_begin,
a.stgroup,
b.strgoup
FROM temp1 a
JOIN temp2 b
ON a.recordid = b.recordid
AND a.course = b.course
AND a.year_begin = b.year_begin;
您可以先按如下方式简化您的查询
select a.RecordID, (ACADEMICYEAR(sysdate) - c.year_begin + 1) COURSE, c.year_begin year_begin, c.Name StGroup
from fc_StudentOrders a
left join fc_OrderTypes b on b.TypeID=a.OrderType
left join fc_StudentGroups c on c.Code=a.StudentGroupID
left join RB_DEPARTMENTS d on d.code = c.faculty
where a.RecordID = 205838 and a.ORDERTYPE IN (1,15,56,109)
我有一个关于 UNION 的查询,要求在一行中输出结果。我尝试使用 group by,但发生错误。也许有一种解决方案可以将当前查询组合成一行?
select a.RecordID, (ACADEMICYEAR(sysdate) - c.year_begin + 1) COURSE, c.year_begin year_begin, c.Name StGroup
from fc_StudentOrders a
left join fc_OrderTypes b on b.TypeID=a.OrderType
left join fc_StudentGroups c on c.Code=a.StudentGroupID
left join RB_DEPARTMENTS d on d.code = c.faculty
where a.RecordID = 205838 and a.ORDERTYPE IN (15,56,109)
union all
select a.RecordID, (ACADEMICYEAR(sysdate) - c.year_begin + 1) COURSE, c.year_begin year_begin, c.Name StGroup
from fc_StudentOrders a
left join fc_OrderTypes b on b.TypeID=a.OrderType
left join fc_StudentGroups c on c.Code=a.StudentGroupID
left join RB_DEPARTMENTS d on d.code = c.faculty
where a.RecordID = 205838 and a.ORDERTYPE IN (1)
结果截图:
如果我没理解错的话,这可能是一种选择:将查询分成两部分(temp1
和 temp2
),然后 JOIN
它们:
WITH
temp1
AS
(SELECT a.RecordID,
(ACADEMICYEAR (SYSDATE) - c.year_begin + 1) COURSE,
c.year_begin year_begin,
c.Name StGroup
FROM fc_StudentOrders a
LEFT JOIN fc_OrderTypes b ON b.TypeID = a.OrderType
LEFT JOIN fc_StudentGroups c ON c.Code = a.StudentGroupID
LEFT JOIN RB_DEPARTMENTS d ON d.code = c.faculty
WHERE a.RecordID = 205838
AND a.ORDERTYPE IN (15, 56, 109)),
temp2
AS
(SELECT a.RecordID,
(ACADEMICYEAR (SYSDATE) - c.year_begin + 1) COURSE,
c.year_begin year_begin,
c.Name StGroup
FROM fc_StudentOrders a
LEFT JOIN fc_OrderTypes b ON b.TypeID = a.OrderType
LEFT JOIN fc_StudentGroups c ON c.Code = a.StudentGroupID
LEFT JOIN RB_DEPARTMENTS d ON d.code = c.faculty
WHERE a.RecordID = 205838
AND a.ORDERTYPE IN (1))
SELECT a.recordid,
a.course,
a.year_begin,
a.stgroup,
b.strgoup
FROM temp1 a
JOIN temp2 b
ON a.recordid = b.recordid
AND a.course = b.course
AND a.year_begin = b.year_begin;
您可以先按如下方式简化您的查询
select a.RecordID, (ACADEMICYEAR(sysdate) - c.year_begin + 1) COURSE, c.year_begin year_begin, c.Name StGroup
from fc_StudentOrders a
left join fc_OrderTypes b on b.TypeID=a.OrderType
left join fc_StudentGroups c on c.Code=a.StudentGroupID
left join RB_DEPARTMENTS d on d.code = c.faculty
where a.RecordID = 205838 and a.ORDERTYPE IN (1,15,56,109)