Solved

Add a join of a query

Posted on 2013-12-11
6
487 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
t-sql splitting string column 5 27
Auditing in Azure SQL Database 3 28
Minus first query 1 6
Stored Procedure 2 0
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now