我如何才能完全加入这两个表?这两个查询都返回相同的列。
预期的<MyComponent item-id="{{$item->id}}"/>
字段为JOIN
。
已经尝试了很少的选项,但始终会出现语法错误。
查询1:
MyOrder
查询2:
SELECT MIN(b.TimeFrame) Nazwa, MIN(b.MyOrder) MyOrder, SUM(b.Remaining) SumaFaktury
FROM (
SELECT
CASE
WHEN a.DaysLate BETWEEN 1 AND 14 THEN '1 to 14'
WHEN a.DaysLate BETWEEN 15 AND 30 THEN '15 to 30'
ELSE 'over 30'
END AS TimeFrame,
CASE
WHEN a.DaysLate BETWEEN 1 AND 14 THEN 1
WHEN a.DaysLate BETWEEN 15 AND 30 THEN 2
ELSE 3
END AS MyOrder,
Remaining, DaysLate
FROM (
SELECT DATEDIFF(dd, TZ, GETDATE()) DaysLate, (WN - MA) Remaining
FROM dbo.TableA
WHERE DATEDIFF(dd, TZ, GETDATE()) > 0
) a
) b
GROUP BY b.MyOrder
ORDER BY b.MyOrder ASC
答案 0 :(得分:3)
只需将每个查询用作表表达式来执行FULL OUTER JOIN
,并用不同的名称命名每个查询。 ORDER BY
仅需提取一次,最后只包含一次。
例如:
select
a.Nazwa as a_Nazwa, -- selected columns here
b.Nazwa as b_Nazwa,
coalesce(a.MyOrder, b.MyOrder) as MyOrder,
a.SumaFaktury as a_SumaFaktury,
b.SumaFaktury as b_SumaFaktury
from ( -- query 1
SELECT
MIN(b.TimeFrame) Nazwa, MIN(b.MyOrder) MyOrder,
SUM(b.Remaining) SumaFaktury
FROM (
SELECT
CASE
WHEN a.DaysLate BETWEEN 1 AND 14 THEN '1 to 14'
WHEN a.DaysLate BETWEEN 15 AND 30 THEN '15 to 30'
ELSE 'over 30'
END AS TimeFrame,
CASE
WHEN a.DaysLate BETWEEN 1 AND 14 THEN 1
WHEN a.DaysLate BETWEEN 15 AND 30 THEN 2
ELSE 3
END AS MyOrder,
Remaining, DaysLate
FROM (
SELECT DATEDIFF(dd, TZ, GETDATE()) DaysLate, (WN - MA) Remaining
FROM dbo.TableA
WHERE DATEDIFF(dd, TZ, GETDATE()) > 0
) a
) b
GROUP BY b.MyOrder
) a
full outer join ( -- query 2
SELECT
MIN(b.TimeFrame) Nazwa, MIN(b.MyOrder) MyOrder,
SUM(b.Remaining) SumaFaktury
FROM (
SELECT
CASE
WHEN a.DaysLate BETWEEN 1 AND 14 THEN '1 to 14'
WHEN a.DaysLate BETWEEN 15 AND 30 THEN '15 to 30'
ELSE 'over 30'
END AS TimeFrame,
CASE
WHEN a.DaysLate BETWEEN 1 AND 14 THEN 1
WHEN a.DaysLate BETWEEN 15 AND 30 THEN 2
ELSE 3
END AS MyOrder,
Remaining, DaysLate
FROM (
SELECT DATEDIFF(dd, TERMIN_ZAP, GETDATE()) DaysLate, (SALDO_WN) Remaining
FROM dbo.TableB
WHERE DATEDIFF(dd, TERMIN_ZAP, GETDATE()) > 0
) a
) b
GROUP BY b.MyOrder
) b on a.MyOrder = b.MyOrder -- the join condition
order by coalesce(a.MyOrder, b.MyOrder) ASC -- the ordering
在主要选择中,您可能不希望使用COALESCE()
,这取决于您要如何处理列。