问题描述
早上好,
我的存储过程有问题.
在SQL存储过程中,我制作了3个临时表,如#table1,#table2,#table3,所有三个表都有2列,如#table1(total1,Code),#table2(total2,Code),#table3(total3,Code).
在这3个表代码中,数据是相同的,但总字段具有不同的值,#table1和#table2每次都有数据,但#table3几乎有空记录,但在某些情况下#table3也有记录.
我的选择查询如下.
Hi, good morning,
I have problem with stored procedure.
In SQL stored procedure I make 3 temp table like #table1,#table2,#table3 and all three table have 2 column like #table1(total1,Code),#table2(total2,Code) ,#table3(total3,Code).
In these 3 table code is data is same but total field have different value,#table1 and #table2 have data every time but #table3 have almost empty record but some case #table3 also have record.
My select Query is given below.
select t1.total1,t2.total2,t3.total3
from #table1 t1
inner join #table2 t2 on t1.Code=t2.Code
inner join #table3 t3 on t2.code=t3.Code
如果#table3为空记录,则不显示t1.total1,t2.total2,t3.total3值,但如果#table3不为空,则显示该值.
所以请帮我解决这个问题
谢谢
[edit]大小写,代码块,整洁-OriginalGriff [/edit]
[edit]糟糕:我的修改说明中存在拼写错误...-OriginalGriff [/edit]
It''s not displaying the t1.total1,t2.total2,t3.total3 value if #table3 is empty record but if #table3 not empty then its show the value.
So please help me how I solve this issue
thanks
[edit]Capitalization, code block, general tidy - OriginalGriff[/edit]
[edit]Oops: spelling error in my modification notes... - OriginalGriff[/edit]
推荐答案
SELECT t1.total1,t2.total2,t3.total3 FROM #table1 t1
INNER JOIN #table2 t2 ON t1.code = t2.code
LEFT OUTER JOIN #table3 t3 ON t1.code = t3.code
谢谢,
Imdadhusen
Thanks,
Imdadhusen
select t1.code,t1.total1,t2.total2,t3.total3
from #table1 t1
left join #table2 t2 on t2.Code=t1.Code
left join #table3 t3 on t3.code=t1.Code
情况2-这些表都不包含所有代码
Case 2 - none of the tables contains all codes
select c.code,t1.total1,t2.total2,t3.total3
from (
-- all codes
select code from #table1
union
select code from #table2
union
select code from #table3
) c
left join #table1 t2 on t1.Code=c.Code
left join #table2 t2 on t2.Code=c.Code
left join #table3 t3 on t3.code=c.Code
这篇关于SQL Server查询问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!