Solved

Add a join of a query

Posted on 2013-12-11
6
490 Views
Last Modified: 2013-12-11
I have this query to compare values in two tables, Lindoc and ObrLinDoc.  Now i need join another table (Customers) to return in each line also Name and Id of customers.

SELECT
      	
       CodigoProduto
      , Designacao
      , CONVERT(DECIMAL(18,4),SUM(Quant_Orc))                                              AS [Quant Orcamentado]
      , CONVERT(DECIMAL(18,4),SUM(Quant_Imputado))                                         AS [Quant Imputado]
      , CONVERT(DECIMAL(18,4),SUM(Quant_Imputado)) - CONVERT(DECIMAL(18,4),SUM(Quant_Orc)) AS Desvio
	  , CONVERT(DECIMAL(18,2),sum (Venda_Imputado))                                        AS [Imputado Venda]  
	  , CONVERT(DECIMAL(18,2),sum (Custo_Imputado))                                        As [Imputado Custo]
FROM (
       
	    SELECT
              CodigoProduto
			  , '0' as mov
              , Designacao
              , SUM(quantidade)        AS Quant_Orc
              , 0                      AS Quant_Imputado 
			  , sum(TotalLinha)        As Venda_Imputado 
			  ,0                       AS Custo_Imputado
			 
        FROM lindoc
		     inner join cabdoc on
		     lindoc.IdStampCab=cabdoc.IdStamp
		WHERE CABdoc.TipoDoc='DO' and cabdoc.IdStamp='DOC20131015162329687'                   -- Preciso saber qual o numero documento
		 
		GROUP BY
              CodigoProduto
              , designacao
			 

        UNION ALL


                SELECT
                      CodigoProduto
					  , mov
                      , Designacao
                      , 0                                                                             AS Quant_Orc
					  , CASE mov WHEN 'S' THEN sum(quantidade) ELSE sum(quantidade)*-1                END AS Quant_Imputado
					  , 0                                                                             As Venda_Imputado 
			          , CASE mov WHEN 'S' THEN sum(TotalLinhaCusto) ELSE sum(TotalLinhaCusto)*-1 END  AS Custo_Imputado
                
				FROM ObrLinDoc
				WHERE CodigoObra=1                                      -- Preciso saber numero de obra
                GROUP BY
                        CodigoProduto
						,mov
                      , Designacao

     ) as ObrLin
	 Where CodigoProduto Is Not Null
	 
GROUP BY
         CodigoProduto
       , designacao

Open in new window

0
Comment
Question by:rflorencio
  • 4
  • 2
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39711136
>Now i need join another table (Customers) to return in each line also Name and Id of customers.

Insert in line 52, and replace ?? with whatever column is the relationship between Customers and your subquery.  May need to add this column to the subquery if it doesn't already exists.
LEFT JOIN Customers c ON OrbLin.?? = c.??

Open in new window

Then you can add to your SELECT clause,
,c.id as customer_id, c.name as customer_name

Open in new window

0
 

Author Comment

by:rflorencio
ID: 39711232
I need join CustId but is in  Cabdoc.Custid table (line 23), can you help.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39711313
>Cabdoc.Custid table (line 23)
table.column, so technically Cabdoc.Custid is a column.

In that case, you're going to have to add CustId to both SELECT statements in the UNION subquery.  Once that's done, the main query can use it in the LEFT JOIN.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:rflorencio
ID: 39711526
Hi  jimhorn, can you take a look to see if all is well, the result returned to me after running it seems correct.

SELECT
        CodCli
	  , NomeCli
	  , CodigoProduto
      , Designacao
      , CONVERT(DECIMAL(18,4),SUM(Quant_Orc))                                              AS [Quant Orcamentado]
      , CONVERT(DECIMAL(18,4),SUM(Quant_Imputado))                                         AS [Quant Imputado]
      , CONVERT(DECIMAL(18,4),SUM(Quant_Imputado)) - CONVERT(DECIMAL(18,4),SUM(Quant_Orc)) AS Desvio
	  , CONVERT(DECIMAL(18,2),sum (Venda_Imputado))                                        AS [Imputado Venda]  
	  , CONVERT(DECIMAL(18,2),sum (Custo_Imputado))                                        As [Imputado Custo]
FROM (
       
	    SELECT
		      clientes.CodigoCliente as CodCli
			  ,clientes.Nome as NomeCli	
              ,CodigoProduto
			  , '0' as mov
              , Designacao
              , SUM(quantidade)        AS Quant_Orc
              , 0                      AS Quant_Imputado 
			  , sum(TotalLinha)        As Venda_Imputado 
			  ,0                       AS Custo_Imputado
			 
        FROM lindoc
		     inner join cabdoc on lindoc.IdStampCab=cabdoc.IdStamp
			 inner join Clientes on CabDoc.CodigoEntidade=Clientes.CodigoCliente
		WHERE CABdoc.TipoDoc='DO' and cabdoc.IdStamp='DOC20131015162329687'                   -- Preciso saber qual o numero documento
		 
		GROUP BY
		
              CodigoProduto
              , designacao
			  , Clientes.CodigoCliente
			  , Clientes.Nome
			 

        UNION ALL


                SELECT
					  clientes.CodigoCliente as CodCli
					  ,clientes.Nome as NomeCli	
                      ,CodigoProduto
					  , ObrLinDoc.mov
                      , Designacao
                      , 0                                                                             AS Quant_Orc
					  , CASE ObrLinDoc.mov WHEN 'S' THEN sum(quantidade) ELSE sum(quantidade)*-1                END AS Quant_Imputado
					  , 0                                                                             As Venda_Imputado 
			          , CASE ObrLinDoc.mov WHEN 'S' THEN sum(TotalLinhaCusto) ELSE sum(TotalLinhaCusto)*-1 END  AS Custo_Imputado
                
				FROM ObrLinDoc
				 inner join ObrFichaObra on ObrLinDoc.CodigoObra=ObrFichaObra.CodigoObra
			     inner join Clientes on ObrFichaObra.CodigoCliente=Clientes.CodigoCliente
				WHERE ObrLinDoc.CodigoObra=1                                      -- Preciso saber numero de obra
                GROUP BY
                        CodigoProduto
						,ObrLinDoc.mov
                      , Designacao
					  , Clientes.CodigoCliente
			  , Clientes.Nome

     ) as ObrLin
	 Where CodigoProduto Is Not Null
	
GROUP BY
         CodigoProduto
       , designacao
	   , CodCli
	   , nomecli

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39711537
Looks correct, although since I'm not connected to your data source I can't verify.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39711556
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question