complicated SELECT

Dúvidas, dicas e truques de SQL, Select, Update, Delete, cláusulas, operações com joins, Funções em SQLs, etc
Post Reply
marquesjr
Rank: Analista Júnior
Rank: Analista Júnior
Posts: 77
Joined: Wed, 05 Dec 2007 10:51 am
Location: Fortaleza - CE

Personnel, researching the origin of a vision, I found in the bank the following Select:

As I do not have much knowledge, I could not enterder the structure of this selection, my doubt is this;

I have already tried several way add another join, which would be the description of the product (DS_Product field of the product table), I thought it was enough to add join into the penultimate and last from / wher It did not work, what select would you have to add Join?

Another question is what is the purpose of this select with null td_contagem and 0 hr_contagem?

Select all

Select Copia_Estoque.Cd_Contagem 
     , Null          dt_contagem 
     , Null          hr_contagem 
     , Null          dt_geracao 
     , Null          hr_geracao 
     , Copia_Estoque.cd_produto 
     , 0 qt_estoque 
     , 0 qt_estoque_doado 
     , 0 vl_custo_medio

Select all

Select Saldo_Atual.cd_contagem 
     , Saldo_Atual.dt_contagem 
     , Saldo_Atual.hr_contagem 
     , Saldo_Atual.dt_geracao 
     , Saldo_Atual.hr_geracao 
     , Saldo_Atual.cd_produto 
     , Saldo_Atual.Qt_estoque 
     , Saldo_Atual.Qt_estoque_doado 
     , Saldo_Atual.vl_custo_medio 
     , Nvl(Saldo_Anterior.qt_anterior, 0) Qt_Anterior 
     , Nvl(Saldo_Anterior.qt_anterior_doado, 0) Qt_Anteior_Doado 
     , Saldo_Atual.ds_unidade 
  From ( 
Select contagem.cd_contagem 
     , contagem.dt_contagem 
     , contagem.hr_contagem 
     , contagem.dt_geracao 
     , contagem.hr_geracao 
     , itcontagem.cd_produto 
     , Sum(itcontagem.qt_estoque * uni_pro.vl_Fator) qt_estoque 
     , Sum(itcontagem.qt_estoque_doado * uni_pro.vl_fator) qt_estoque_doado 
     , avg(itcontagem.vl_custo_medio / uni_pro.vl_fator ) vl_custo_medio 
     , 0 qt_anterior 
     , 0 qt_anterior_doado 
     , uni_pro.ds_unidade 
  From Dbamv.Contagem 
     , Dbamv.ItContagem 
     , Dbamv.Uni_Pro 
 Where Contagem.Cd_Contagem = ItContagem.Cd_Contagem 
   And ItContagem.Cd_Uni_Pro = Uni_Pro.Cd_Uni_Pro 
Group By Contagem.Cd_Contagem 
       , contagem.Dt_Contagem 
       , contagem.Hr_Contagem 
       , contagem.Dt_Geracao 
       , contagem.Hr_Geracao 
       , itcontagem.Cd_Produto 
       , Uni_Pro.Ds_Unidade 
       ) Saldo_Atual 
     , ( 
Select Copia_Estoque.Cd_Contagem 
     , Null          dt_contagem 
     , Null          hr_contagem 
     , Null          dt_geracao 
     , Null          hr_geracao 
     , Copia_Estoque.cd_produto 
     , 0 qt_estoque 
     , 0 qt_estoque_doado 
     , 0 vl_custo_medio 
     , Sum(Copia_Estoque.Qt_Estoque) qt_anterior 
     , Sum(Copia_Estoque.Qt_Estoque_Doado) qt_anterior_doado 
     , Null  ds_unidade 
  From Dbamv.Copia_Estoque 
Group By Copia_Estoque.Cd_Contagem 
       , Copia_Estoque.cd_produto 
       ) Saldo_Anterior 
Where Saldo_Atual.Cd_Contagem = Saldo_Anterior.Cd_Contagem(+) 
  And Saldo_Atual.Cd_Produto = Saldo_Anterior.Cd_Produto(+)
Trevisolli
Moderador
Moderador
Posts: 2016
Joined: Wed, 12 Jan 2005 3:25 pm
Location: Araraquara - SP
Abraço,

Trevisolli
OCA Oracle PL/SQL Developer Certified Associate
OCP Oracle Forms Developer Certified Professional
Araraquara-SP

Brother,

to add the product description, you must provide the product table (in your from, through a join).
As we do not know your base, you could not know which one and if she's on your Select.

As for the value null and 0 (zero) in your select, it should probably attend a Union and, because not having these values ??in this table, null is passed and an alias for it.

Any questions, send ai, beleza?
marquesjr
Rank: Analista Júnior
Rank: Analista Júnior
Posts: 77
Joined: Wed, 05 Dec 2007 10:51 am
Location: Fortaleza - CE

all beleza, coming up year and the guy full of doubts, it's soft, but it's a simple basic doubt, I come hammering to insert the description of the product in that select o join would be this:



One of the attempt was as follows:

Select all

Select Saldo_Atual.cd_contagem 
     , Saldo_Atual.dt_contagem 
     , Saldo_Atual.dt_geracao 
     , Saldo_Atual.cd_produto 
     , Saldo_Atual.ds_unidade 
     , produto.ds_produto 
     , Saldo_Atual.Qt_estoque AS QT_APOS 
     , Nvl(Saldo_Anterior.qt_anterior, 0) AS QT_ANTERIOR 
     , Saldo_Atual.vl_custo_medio 
  From ( 
Select contagem.cd_contagem 
     , contagem.dt_contagem 
     , contagem.dt_geracao 
     , itcontagem.cd_produto 
     , produto.ds_produto 
     , Sum(itcontagem.qt_estoque * uni_pro.vl_Fator) qt_estoque 
     , avg(itcontagem.vl_custo_medio / uni_pro.vl_fator ) vl_custo_medio 
     , 0 qt_anterior 
     , uni_pro.ds_unidade 
  From Dbamv.Contagem 
     , Dbamv.ItContagem 
     , Dbamv.Uni_Pro 
     , Dbamv.produto 
 Where Contagem.Cd_Contagem = ItContagem.Cd_Contagem 
   And ItContagem.Cd_Uni_Pro = Uni_Pro.Cd_Uni_Pro  
   AND itcontagem.cd_produto = produto.cd_produto 
Group By Contagem.Cd_Contagem 
       , contagem.Dt_Contagem 
       , contagem.Dt_Geracao 
       , itcontagem.Cd_Produto 
       , Uni_Pro.Ds_Unidade 
       ) Saldo_Atual 
     , ( 
Select Copia_Estoque.Cd_Contagem 
     , Null          dt_contagem 
     , Null          dt_geracao 
     , Copia_Estoque.cd_produto 
     , produto.ds_produto 
     , 0 qt_estoque 
     , 0 vl_custo_medio 
     , Sum(Copia_Estoque.Qt_Estoque) qt_anterior 
     , Null  ds_unidade 
  From Dbamv.Copia_Estoque 
Group By Copia_Estoque.Cd_Contagem 
       , Copia_Estoque.cd_produto 
       ) Saldo_Anterior 
Where Saldo_Atual.Cd_Contagem = Saldo_Anterior.Cd_Contagem(+) 
  And Saldo_Atual.Cd_Produto = Saldo_Anterior.Cd_Produto(+)  
  ORDER BY dt_contagem;
and submitted the following error:

two]]
Trevisolli
Moderador
Moderador
Posts: 2016
Joined: Wed, 12 Jan 2005 3:25 pm
Location: Araraquara - SP
Abraço,

Trevisolli
OCA Oracle PL/SQL Developer Certified Associate
OCP Oracle Forms Developer Certified Professional
Araraquara-SP

Brother,

In the first excerpt from the select that you have altered, thus attempts:

Select all

 
Select Saldo_Atual.cd_contagem  
     , Saldo_Atual.dt_contagem  
     , Saldo_Atual.dt_geracao  
     , Saldo_Atual.cd_produto  
     , Saldo_Atual.ds_unidade  
     , Saldo_Atual.ds_produto  
     , Saldo_Atual.Qt_estoque AS QT_APOS  
     , Nvl(Saldo_Anterior.qt_anterior, 0) AS QT_ANTERIOR  
     , Saldo_Atual.vl_custo_medio  
  From ( 
... 
The alias is baldness, by Error this.

Whatever, send us to us.
marquesjr
Rank: Analista Júnior
Rank: Analista Júnior
Posts: 77
Joined: Wed, 05 Dec 2007 10:51 am
Location: Fortaleza - CE

Select all

Select Copia_Estoque.Cd_Contagem  
     , Null          dt_contagem  
     , Null          dt_geracao  
     , Copia_Estoque.cd_produto  
     , produto.ds_produto ---*****
was the same error, only in the highlighted line
Trevisolli
Moderador
Moderador
Posts: 2016
Joined: Wed, 12 Jan 2005 3:25 pm
Location: Araraquara - SP
Abraço,

Trevisolli
OCA Oracle PL/SQL Developer Certified Associate
OCP Oracle Forms Developer Certified Professional
Araraquara-SP

Do you have the product table in this stretch ai brother?
If you do not have and, it is a Union, place:

Select all

 
null produto 
marquesjr
Rank: Analista Júnior
Rank: Analista Júnior
Posts: 77
Joined: Wed, 05 Dec 2007 10:51 am
Location: Fortaleza - CE

Boy, it's not right ..., Select violent. thanks brother!
Trevisolli
Moderador
Moderador
Posts: 2016
Joined: Wed, 12 Jan 2005 3:25 pm
Location: Araraquara - SP
Abraço,

Trevisolli
OCA Oracle PL/SQL Developer Certified Associate
OCP Oracle Forms Developer Certified Professional
Araraquara-SP

beleza Brother,

Needing, it counts on us here.
marquesjr
Rank: Analista Júnior
Rank: Analista Júnior
Posts: 77
Joined: Wed, 05 Dec 2007 10:51 am
Location: Fortaleza - CE

Taking advantage of the Christmas spirit, I come to bring a new punctual doubt:

Select all

     , Sum(itcontagem.qt_estoque / uni_pro.vl_Fator) qt_estoque  
     , avg(itcontagem.vl_custo_medio * uni_pro.vl_fator ) vl_custo_medio  
     , 0 qt_anterior 
How do I use a splitter on the line:

Select all

     , 0 qt_anterior
]]

equal to the used in the line;

Select all

     , Sum(itcontagem.qt_estoque / uni_pro.vl_Fator) qt_estoque
I want to split the QT_Aterior by VL_FATOR.

I tried the following codes:

Select all

     , (0 qt_anterior / uni_pro.vl_fator)

Select all

     , 0 qt_anterior / uni_pro.vl_fator

Select all

     , 0 (qt_anterior / uni_pro.vl_fator)
User avatar
Porva
Rank: DBA Sênior
Rank: DBA Sênior
Posts: 342
Joined: Mon, 29 Jan 2007 7:36 am
Location: São Paulo/SP
Rafael S. Nunes
São Paulo/SP

But the 'qt_anterior' you had not created as alias?

If it is, I think there is no way to put it and at some calculation!, Unless you really have this value somewhere else, the qt_entior as value / field.
marquesjr
Rank: Analista Júnior
Rank: Analista Júnior
Posts: 77
Joined: Wed, 05 Dec 2007 10:51 am
Location: Fortaleza - CE

The previous QT is from the Copy_Stock table.

Select all

Copia_Estoque.Qt_Estoque
Post Reply
  • Information
  • Who is online

    Users browsing this forum: Majestic-12 [Bot] and 17 guests