这是我的问题:我有两个查询,返回的数据如下:
Name Quantity Test
-------------------------
X 10 L
X 13 M
X 17 S
Name Total
-------------
X 87
我需要一个返回如下结果的查询:
Name Quantity Test Total
--------------------------------
X 10 L 87
X 13 M NULL
X 17 S NULL
查询:
SELECT ag.vendnm
,bo3.u_nome
,bi.ETTDEB
,v.total2019
FROM bo(NOLOCK)
INNER JOIN bi(NOLOCK) ON (bi.bostamp = bo.bostamp)
INNER JOIN bi2(NOLOCK) ON (bi2.bi2stamp = bi.bistamp)
INNER JOIN bo2(NOLOCK) ON (bo2.bo2stamp = bo.bostamp)
INNER JOIN bo3(NOLOCK) ON (bo3.bo3stamp = bo.bostamp)
INNER JOIN ag(NOLOCK) ON (ag.nome = bo3.u_nome)
INNER JOIN (
SELECT cliente
,sum(valor) AS total2019
FROM u_vendas2019
WHERE month(data) <= month(getdate())
GROUP BY cliente
) AS v ON (v.cliente = bo3.u_nome)
WHERE bo.ndos = 1
AND year(bo.dataopen) = '2020'
AND bo3.u_nome = 'Caroll'
AND bo2.anulado = 0
GROUP BY ag.vendnm
,bo3.u_nome
,v.total2019
,bi.ETTDEB
我尝试使用左联接,但是没有用。不知道该怎么做。
答案 0 :(得分:0)
select a.NAME,
a.QUANTITY,
a.TEST,
b.TOTAL
from TABLE_A a
left join TABLE_B b on a.NAME = b.NAME
答案 1 :(得分:0)
尝试类似的方法:据我了解,您只希望在每个不同名称的第4栏中输入
select table1.name,table1.quantity,table1.test,
case when k.row = 1 then table2.total
else null
end
from table1 inner join
(
select
name,row_number() over(order by name asc) row
from table1
) k on table1.name = k.name
inner join table2 on table1.name = table2.name