SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Hello,


Hello,

Author
Message
luissantos
luissantos
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5295 Visits: 940
I have a little problem on my join condition, my script is:

SELECT
DISTINCT
(Case when bo.u_tpfact = 'Utente' then 'U'
When bo.u_tpfact = 'Convenção' then 'C'
When bo.u_tpfact = 'Entidade' then 'E'
When bo.u_tpfact = 'Convenção+Utente' then 'C+U'
When bo.u_tpfact = 'Convenção+Entidade' then 'C+E'
END) [Faturar_a],
Bo.obranome, BO.nome, bo2.identificacao1 [Entidade], BO.tabela1 [Convenção],bii.design [descricaoexame],
Isnull((SELECT TOP 1 u_vpart FROM cl2 INNER JOIN cl ON cl2.cl2stamp = cl.clstamp WHERE cl.nome = bo2.identificacao1 AND cl.no = bo2.u_entfact ),0.00) [Prct particular],
Isnull((SELECT TOP 1 u_vconv FROM cl2 INNER JOIN cl ON cl2.cl2stamp = cl.clstamp WHERE cl.nome = bo2.identificacao1 AND cl.no = bo2.u_entfact ),0.00) [Prct Convenção],
isnull((SELECT TOP 1 u_vsns FROM cl2 INNER JOIN cl ON cl2.cl2stamp = cl.clstamp WHERE cl.nome = bo2.identificacao1 AND cl.no = bo2.u_entfact ),0.00) [Prct SNS],

/*Valor do Exame*/
(select sum(ettdeb) from bi where bi.bistamp = bii.bistamp and bi.bistamp = fi.bistamp ) [ValorExame],

/* Calculo comissões*/
ISNULL(
(CASE WHEN RTRIM(BO.tabela1) <> 'SNS' AND BO.tabela1 <> ''
AND
(
(SELECT top 1 pn.pago FROM fi inner join ft on fi.ftstamp=ft.ftstamp
INNER JOIN pn ON pn.fistamp = fi.fistamp
where fi.bistamp = bii.bistamp and ft.ndoc in ( 3) ) = 1
OR
(SELECT top 1 pn.pago FROM fi inner join ft on fi.ftstamp=ft.ftstamp
INNER JOIN pn ON pn.fistamp = fi.fistamp
where fi.bistamp = bii.bistamp and ft.ndoc in( 5) ) = 1

)

then
ROUND(Isnull((SELECT TOP 1 u_vconv FROM cl2 INNER JOIN cl ON cl2.cl2stamp = cl.clstamp WHERE cl.nome = bo2.identificacao1 AND cl.no = bo2.u_entfact ),0.00)
* (select sum(ettdeb) from bi where bi.bistamp = bii.bistamp and bi.bistamp = fi.bistamp ) ,2)


WHEN RTRIM(BO.tabela1) = 'SNS' AND
(
(SELECT top 1 pn.pago FROM fi inner join ft on fi.ftstamp=ft.ftstamp
INNER JOIN pn ON pn.fistamp = fi.fistamp
where fi.bistamp = bii.bistamp and ft.ndoc in( 3) ) = 1
OR
(SELECT top 1 pn.pago FROM fi inner join ft on fi.ftstamp=ft.ftstamp
INNER JOIN pn ON pn.fistamp = fi.fistamp
where fi.bistamp = bii.bistamp and ft.ndoc in( 5) ) = 1

)

THEN
ROUND(Isnull((SELECT TOP 1 u_vsns FROM cl2 INNER JOIN cl ON cl2.cl2stamp = cl.clstamp WHERE cl.nome = bo2.identificacao1 AND cl.no = bo2.u_entfact ),0.00)
* (select sum(ettdeb) from bi where bi.bistamp = bii.bistamp and bi.bistamp = fi.bistamp ) ,2)


WHEN RTRIM(BO.tabela1) = '' AND
(
(SELECT top 1 pn.pago FROM fi inner join ft on fi.ftstamp=ft.ftstamp
INNER JOIN pn ON pn.fistamp = fi.fistamp
where fi.bistamp = bii.bistamp and ft.ndoc in( 5) ) = 1
OR
(SELECT top 1 pn.pago FROM fi inner join ft on fi.ftstamp=ft.ftstamp
INNER JOIN pn ON pn.fistamp = fi.fistamp
where fi.bistamp = bii.bistamp and ft.ndoc in( 3) ) = 1

)

THEN
ROUND(Isnull((SELECT TOP 1 u_vpart FROM cl2 INNER JOIN cl ON cl2.cl2stamp = cl.clstamp WHERE cl.nome = bo2.identificacao1 AND cl.no = bo2.u_entfact ),0.00)
* (select sum(ettdeb) from bi where bi.bistamp = bii.bistamp and bi.bistamp = fi.bistamp ) ,2)

END ), 0.00) [ValorComissao],

(CASE WHEN (SELECT TOP 1 pn.pago FROM fi inner join ft on fi.ftstamp=ft.ftstamp
INNER JOIN pn ON pn.fistamp = fi.fistamp
where fi.bistamp = bii.bistamp and fi.ndoc in (3,5) ) = 1 THEN 'Pago' ELSE 'Pendente' END ) [Situacao],

(Case when ft.anulado = 1 then 'Anulada' else 'OK' end) [Status Fatura],
(Case when bo.fechada = 1 then 'Fechado' else 'OK' end) [Status Exame]

FROM
bi bii right JOIN bo ON bii.bostamp = bo.bostamp
right join bo2 ON bo2.bo2stamp = bo.bostamp
full outer join fi on bii.bistamp = fi.bistamp
full outer join ft on fi.ftstamp = ft.ftstamp
full outer join pn on fi.fistamp = pn.fistamp
WHERE
bo.dataobra BETWEEN '20180101' and '20180216'
AND bo2.identificacao1 LIKE '%'+ '%' + '%'

And ft.ndoc in (1,2,3,5)

Group by

Bo.obranome, BO.nome, bo2.identificacao1 , BO.tabela1,bii.design,
bo2.u_entfact,bii.bistamp, fi.bistamp, ft.anulado, bo.fechada,bo.u_tpfact

order by obranome asc

For Each BO [Header table] i have always lines on my BI [Lines of document], then the first join are correct.
My problem is whe n i make join on table [FT, FI, PN], because theses tables only have relation on [BO and BI] if i have an invoice.
I need to return all values from my BO and BI [Tables] even if not exits on tables : FI, PN,FT.
someone could give me help.

Best regards,
Luis



DesNorton
DesNorton
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16115 Visits: 6791
Try changing this
  full outer join fi on bii.bistamp = fi.bistamp
full outer join ft on fi.ftstamp = ft.ftstamp
full outer join pn on fi.fistamp = pn.fistamp


to this
  left join fi on bii.bistamp = fi.bistamp
left join ft on fi.ftstamp = ft.ftstamp
left join pn on fi.fistamp = pn.fistamp



How to post data/code on a forum to get the best help.
Make sure that you include code in the appropriate IFCode tags.
luissantos
luissantos
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5295 Visits: 940
Hello,
Thanks for your reply.

Best regards,
Luis



luissantos
luissantos
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5295 Visits: 940
Hello,
The LEFT join condition doesn´t have solve the problem. I keep the original query and i have adding UNION ALL changing my Where condition and finally i solve the problem.
Thanks,
Luis Santos



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search