SQL bring result with repeated column numbers all only the largest

Asked

Viewed 255 times

0

Hello guys I’m trying to get a data but I’m not able to filter the way I need, example :

select a.cod, a.nome, b.pedido, b.versao from cliente a 
inner join pedido b On a.cod= b.cod
order by b.pedido

Now the problem a version has more than one type

Below is the example of how the result is coming:

that was the result but only brings now if I put the budget if the budget is null the person puts the initial and final date. More when I put now it brings everything disregarding the command

Then it brings right if you put the quotation. If put by initial and final date in the variables it returns to bring everything without considering the latest version.

  • 1

    Version 3 would be requested 3? The result of your query does not match the SQL you posted.

  • Hello this is just a basic example to figure out. An order can have more versions so it can not be fixed 3 , same in the example it has several versions until turn request in case I just want it to bring everything of the latest version

  • Jameson: post the result of the execution of the code you transcribed. And add information about the REQUEST table; for example, other columns that influence the result.

3 answers

1

What I understood is that for each request you must return the line with the latest version. There are some ways to get this result; here is one of them:

-- código #1
with PedidoClassificado as (
SELECT cod, pedido, versao,
       seq= row_number() over (partition by pedido order by versao desc)
  from pedido
)
SELECT B.cod, A.nome, B.pedido, B.versao
  from cliente as A 
       inner join PedidoClassificado as B on A.cod = B.cod
  where B.seq = 1;
  • I’ll try here

1


I believe you have a mistake in yours scrip for, apparently, you compare the order code with the client code on join, see inner join pedido b On a.cod= b.cod

You can add a AND in his join that will filter through MAX version, see

select 
    a.cod, 
    a.nome, 
    b.pedido, 
    b.versao 
from cliente a 
    inner join pedido b On a.cod= b.codigo_cliente AND b.versao = (select max(pedido.versao) from pedido where pedido.codigo_cliente = a.id)
order by b.pedido

For reference I made an example on SQL Fiddle


EDIT

What I’m seeing wrong are the checks with isnull, in them you say that if the field is null, it returns itself (ISNULL (@vORCV_COD , @vORCV_COD)) doesn’t make sense that.

Try the following query:

DECLARE
    @vORCV_COD INT ,
    @vDAT_INI CHAR(10),
    @vDAT_FIM CHAR(10)

SET @vORCV_COD =  null-- 333829
SET @vDAT_INI = '20180405' --data inicial
SET @vDAT_FIM = '20180501' -- data final

SELECT 
    A.ORCV_COD, 
    A.VEOV_VAL,
    A.VEOV_QTT_VER ,
    A.VEOV_PER_MARLUC,
    E.PARC_COD,
    A.PEDS_COD,
    I.PEDS_DAT_CAD,
    A.VEOV_DAT_CAD,
    V.VEND_NOM_FAN,
    B.AORC_OBS,
    B.AORC_DAT_CAD,
    C.USUA_NOM,
    D.TAOR_NOM,
    F.STOV_NOM,
    ISNULL(G.PARC_NOM_RAZ, J.PAOV_NOM_EMP) AS 'RAZAO SOCIAL',
    ISNULL(H.OROV_NOM, 'ORIGEM NÃO INDICADA') AS 'ORIGEM'
FROM TCOM_VERORV A 
    LEFT JOIN TCOM_ANDORC B         ON A.ORCV_COD = B.ORCV_COD 
    LEFT JOIN TCOM_TIPANDORC D      ON D.TAOR_COD = B.TAOR_COD
    INNER JOIN TCOM_ORCVEN E        ON E.ORCV_COD  = A.ORCV_COD
    INNER JOIN TCOM_STAORV F        ON  E.STOV_COD = F.STOV_COD
    LEFT JOIN TACE_USUARIO C        ON C.USUA_COD  = B.USUA_COD          
    INNER JOIN TCOM_VENDEDOR V      ON V.VEND_COD = E.VEND_COD
    LEFT JOIN TCOM_PARCEIRO G       ON G.PARC_COD = E.PARC_COD
    LEFT JOIN TCOM_ORIORV H         ON H.OROV_COD = E.OROV_COD
    LEFT JOIN TCOM_PEDSAI I         ON I.PEDS_COD = A.PEDS_COD
    LEFT JOIN TCOM_PARORV J         ON A.ORCV_COD = J.ORCV_COD
WHERE (1=1)
    AND ISNULL(A.ORCV_COD, 0) = ISNULL (@vORCV_COD , ISNULL(A.ORCV_COD, 0)) 
    AND A.VEOV_QTT_VER = (SELECT MAX(X.VEOV_QTT_VER) FROM TCOM_VERORV X WHERE ISNULL(X.ORCV_COD, 0) = ISNULL(A.ORCV_COD, 0))
    AND CONVERT(CHAR(10), ORCV_DAT_CAD , 112) BETWEEN ISNULL (@vDAT_INI, CONVERT(CHAR(10), ORCV_DAT_CAD , 112)) AND ISNULL (@vDAT_FIM, CONVERT(CHAR(10), ORCV_DAT_CAD , 112))

Explanation:

In addition to fixing the indentation, the only thing I actually altered was the where us isnull, if the variable is null it must compare with itself, and if the field is null it will compare 0 with 0.

  • Who gave the Down Vote could at least explain the reason?

  • hello, thanks for the help in the way you did to achieve results but I’m just not able to bring by date I will improve the above example, to see if you can help me

  • @Jameson did not get it right, in case it may have null versions?

  • Hello no, have the budget and datIni variables and end if the person does not put the quote will put the dates . the versions are not null all budgets have plus budgets have more than one version . the example q VC gave worked by putting the number only budget more if I leave null and try to search for the dates it brings all versions despising the q i did with the example q VC me

  • Passed. In subquery q VC passed I think q needs to bring the variables of the dates , but this way do not get result it seems that makes a loop q does not stay several minutes there

  • I don’t know if I understand you very well, but from what I saw in the print you put in the question, you use a OR, shouldn’t be AND. Another thing, avoid using images, it is better to put your code as text.

  • So I also tried to use AND. but I’ll see if I can post and why I was not able to format the code here. More there in the subquery would put the date I want ?

  • It is possible yes, add to query all we try to leave her as you want ;)

  • Hello, posted all the code I am implementing as the example you suggested worked by putting the quotation filter more if I leave blank and filter by date it brings normal without filtering the last quotations.

  • still can’t. With the second AND it does not bring result if put OR it brings all records and all versions not only the last ,and if searching for the budget brings normal result the way I need

  • The date you are searching has data?

  • Yes it does, normal.

  • Let’s go to the chat

  • worked out very well. Thank you for your patience.

  • Imagine, we’re there for it ;)

Show 10 more comments

0

Try using the WHERE SQL command as below:

select 
     a.cod, 
     a.nome, 
     b.pedido, 
     b.versao 
from cliente a 
inner join pedido b 
   ON a.cod= b.cod
where b.versao = 3
order by b.pedido;
  • No and this, I know how to do what I presico and bring the latest versions of the orders, not every order only has 3 versions can have 1 or 10 until approved request,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.