用于连接具有不同列数的两个不同查询的SQL查询

时间:2018-03-28 08:00:47

标签: sql sql-server database

我在这里有两个问题:

查询1:

SELECT allowdeductname_en, SUM(SFD_comp_value_tax@;emp_id) as                                                                                                                                      
GGG                                                                                                                                                                                                 
FROM TPYDPROCMTDD md                                                                                       
LEFT JOIN TPYDPROCMTDH mh on md.procmtdh_id = mh.procmtdh_id and 
md.company_id = mh.company_id                                                                                                                                                                                                                                                    
WHERE md.allowdeducttype = 'A'                                                                                                                                                                                                                                                     
AND md.company_id = '13565'
AND mh.company_id = '13565'
AND year(mh.paydate) = 2017                                                           
AND month(mh.paydate) = 1 
AND mh.costcenter_code = 99990001
group by allowdeductname_en
order by allowdeductname_en

查询2:

SELECT distinct allowdeductname_en                                                                                                                                                                                              
FROM TPYDPROCMTDD md                                                                                       
LEFT JOIN TPYDPROCMTDH mh on md.procmtdh_id = mh.procmtdh_id and 
md.company_id = mh.company_id                                                                                                                                                                                                                                                    
WHERE md.allowdeducttype = 'A'                                                                                                                                                                                                                                                     
AND md.company_id = '13565'
AND mh.company_id = '13565'
AND year(mh.paydate) = 2017                                                           
AND month(mh.paydate) = 1
order by allowdeductname_en 

查询1和查询2的结果: result for query 1 and query 2

无论如何,我可以交叉,加入或联合这两个查询,以便它看起来像这样:

预期结果: enter image description here

我已经尝试了很多不同的方法,但仍然无法得到我想要的结果。伙计们请帮忙!

4 个答案:

答案 0 :(得分:2)

嗯,这是最直接的解决方案:

select * from (/*query number 2*/) [a]
left join (/*query number 1*/) [b] 
on [a].allowdeductname_en = [b].allowdeductname_en

但是您可以在一个查询中完成它,因为两个查询都使用相同的表,但为了提供该解决方案,我需要查看您的数据。

答案 1 :(得分:1)

select t2.allowdeductname_en, GGG=isnull(t1.GGG,0)
 from 
(SELECT distinct allowdeductname_en                                                                                                                                                                                              
FROM TPYDPROCMTDD md                                                                                       
LEFT JOIN TPYDPROCMTDH mh on md.procmtdh_id = mh.procmtdh_id and 
md.company_id = mh.company_id                                                                                                                                                                                                                                                    
WHERE md.allowdeducttype = 'A'                                                                                                                                                                                                                                                     
AND md.company_id = '13565'
AND mh.company_id = '13565'
AND year(mh.paydate) = 2017                                                           
AND month(mh.paydate) = 1 ) t2

LEFT JOIN 

(SELECT allowdeductname_en, SUM(SFD_comp_value_tax@;emp_id) as                                                                                                                                      
GGG                                                                                                                                                                                                 
FROM TPYDPROCMTDD md                                                                                       
LEFT JOIN TPYDPROCMTDH mh on md.procmtdh_id = mh.procmtdh_id and 
md.company_id = mh.company_id                                                                                                                                                                                                                                                    
WHERE md.allowdeducttype = 'A'                                                                                                                                                                                                                                                     
AND md.company_id = '13565'
AND mh.company_id = '13565'
AND year(mh.paydate) = 2017                                                           
AND month(mh.paydate) = 1 
AND mh.costcenter_code = 99990001
group by allowdeductname_en)  t1 on t2.allowdeductname_en=t1.allowdeductname_en


this is not the best query but it give you the result you want. 

答案 2 :(得分:0)

查询仅在few users from same contributors group are able to queue the build条件下有所不同。将其移至聚合函数costcenter_code = 99990001

SUM

答案 3 :(得分:0)

您可以在两者之间使用<table class="zui-table"> <thead> <tr> <th>Name</th> <th>Position</th> <th>Height</th> <th>Born</th> <th>Comment</th> </tr> </thead> <tbody> <tr class="myTROdd"> <td>Jason Thompson</td> <td>PF</td> <td>6'11"</td> <td>06-21-1986</td> <td>Normal ODD Row</td> </tr> <tr class="myTREven"> <td>Fred Bloggs</td> <td>PF</td> <td>5'7"</td> <td>01-08-1988</td> <td>Normal EVEN Row</td> </tr> <tr class="myTROdd"> <td>DeMarcus Cousins</td> <td>C</td> <td class="myBackRed">6'11"</td> <td>08-13-1990</td> <td>ODD Row with Red cell</td> </tr> <tr class="myTREven"> <td>Isaiah Thomas</td> <td>PG</td> <td class="myBackRed">5'9"</td> <td>02-07-1989</td> <td>EVEN Row with Red cell</td> </tr> <tr class="myBackYellowTROdd"> <td>Ben McLemore</td> <td>SG</td> <td class="myBackRed">6'5"</td> <td>02-11-1993</td> <td>Yellow ODD Row - Red cell missing</td> </tr> <tr class="myBackYellowTREven"> <td>Marcus Thornton</td> <td>SG</td> <td class="myBackRed">6'4"</td> <td>05-05-1987</td> <td>Yellow EVEN Row - Red cell missing</td> </tr> </tbody> </table> LEFT JOIN

ISNULL