How to make Join of 2 tables without repeating data?

Asked

Viewed 2,233 times

2

I have 2 tables, a call SR7010 and another call SR3010. The primary key of the two is the field R7_MAT and R3_MAT.

The query I’m trying to execute is this:

SELECT R7_FILIAL, R7_MAT, R7_DESCFUN, R7_DATA, R3_VALOR FROM SR7010 AS sr7
INNER JOIN SR3010 AS sr3 ON (sr3.R3_MAT = sr7.R7_MAT)
WHERE sr7.R7_MAT = 997

What happens is this: the field R3_VALOR appears with the repeated data, but the other fields (starting with R7) appear normally, and when the field R3_VALOR changes, the data of the fields R7 are repeated, thus successively.

inserir a descrição da imagem aqui

How to make it correctly bring the information, that is, only 3 lines, as below?

inserir a descrição da imagem aqui

Are records of salary changes/position.

Ex: In the 1st line, with the position of Computer Assist, on the date of 20151029, is with the salary 50,00.

On the date of 20151115, still as computer Assist, salary was changed to 55,00.

On the date of 20151126, the position changed to Anal. de informatica, with the salary of 60,00.

What happens is that the data is being listed 3 times (amount of records of actually existing changes), but the value field, because it is another table, maybe, is displaying the same value 3 times to then change to another value and repeat again 3 times (number of changes records), and so on.

When I do a table search SR7010 across the countryside R7_MAT = 997, the database returns me 3 records. The same occurs when I do a search in the table SR3010 across the countryside R3_MAT = 997. It also returns 3 records, so I’m finding it strange.

  • If the date and value fields are not relevant, you can take them out of the query and give a group by R7_DESCFUN

  • The problem is that the 5 fields are very relevant

  • Wouldn’t it be a case of adding the value field? the ratio is 1-N?

  • What you could do is use the pivot command to turn rows into columns and have a column for each post.

  • Your second picture of the data makes no sense. for example ASSIST.INFORMATICA on day (R7_DATA) 20151029 has 3 different values on that day and you selected the smallest already on day 20151015 you took the middle one. ANAL. OF INFOR you got the most value, there’s not much logic in it. In place of the image post the data so it would help to understand and simulate easier.

  • Look like salary changes. R7_DESCFUN as the position, R7_DATA as the date of salary/position change and R7_VALOR as salary

  • Ta very strange his salary change. the guy has 3 increase in the day and on another date has the increase of the same value?

  • This is exactly my problem. The correct should be according to the second image.

  • Shouldn’t it be day 20151029 = 50 and day 20151115 = 60? the first two lines ?

Show 4 more comments

2 answers

2


I changed INNER JOIN to include more relationships and it worked correctly.

SELECT sr7.R7_FILIAL, sr7.R7_MAT, sr7.R7_DATA, sr7.R7_DESCFUN, sr3.R3_VALOR, sr3.R3_FILIAL FROM SR7010 AS sr7
INNER JOIN SR3010 AS sr3 ON (sr3.R3_FILIAL = sr7.R7_FILIAL AND sr3.R3_MAT = sr7.R7_MAT AND sr3.R3_DATA = sr7.R7_DATA)
WHERE sr7.R7_MAT = 997

0

The only solution was to create a cursor as follows.

declare @SR7010 table
(
  R7_FILIAL varchar(20),
  R7_MAT varchar(20), 
  R7_DESCFUN varchar (100), 
  R7_DATA date,
  R3_VALOR numeric(18,2)
)

declare @SR7010_new table
(
  R7_FILIAL varchar(20),
  R7_MAT varchar(20), 
  R7_DESCFUN varchar (100), 
  R7_DATA date,
  R3_VALOR numeric(18,2)
)

insert into @SR7010 values
('0301','000997', 'ASSIST. INFORMATICA', '20151029' , 50.00),
('0301','000997', 'ASSIST. INFORMATICA', '20151115' , 50.00),
('0301','000997', 'ANAL. DE INFORM',     '20151126' , 50.00),
('0301','000997', 'ASSIST. INFORMATICA', '20151029' , 55.00),
('0301','000997', 'ASSIST. INFORMATICA', '20151115' , 55.00),
('0301','000997', 'ANAL. DE INFORM',     '20151126' , 55.00),
('0301','000997', 'ASSIST. INFORMATICA', '20151029' , 60.00),
('0301','000997', 'ASSIST. INFORMATICA', '20151115' , 60.00),
('0301','000997', 'ANAL. DE INFORM',     '20151126' , 60.00)


declare @R7_FILIAL varchar(20), @R7_MAT varchar(20), @R7_DESCFUN varchar (100), 
        @R7_DATA date, @R3_VALOR numeric(18,2), @linha int = 0,
        @Linhafinal int =(select top 1 count(R7_FILIAL)
                            from @SR7010
                            group by R7_FILIAL, R7_MAT, R7_DESCFUN, R7_DATA)


DECLARE db_cursor CURSOR FOR  
select R7_FILIAL, R7_MAT, R7_DESCFUN, R7_DATA
from @SR7010 E
group by R7_FILIAL, R7_MAT, R7_DESCFUN, R7_DATA
ORDER BY R7_DATA 

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @R7_FILIAL, @R7_MAT, @R7_DESCFUN, @R7_DATA  

WHILE @@FETCH_STATUS = 0   
BEGIN   

       if(@linha = 0)
       begin
            insert into @SR7010_new 
            Select @R7_FILIAL, @R7_MAT, @R7_DESCFUN, @R7_DATA,( select min(R3_VALOR) from @SR7010 where R7_DATA = @R7_DATA)
            set @linha = 1;
       end
       else
       begin
            set @linha = @linha + 1; 
            if(@Linhafinal = @linha)
            begin
                insert into @SR7010_new 
                Select @R7_FILIAL, @R7_MAT, @R7_DESCFUN, @R7_DATA,(select top 1 t.R3_VALOR from @SR7010 t where t.R7_DATA = @R7_DATA order by t.R7_DATA desc, R3_VALOR desc)
            end
            else
            begin
                insert into @SR7010_new 
                Select @R7_FILIAL, @R7_MAT, @R7_DESCFUN, @R7_DATA,(select top 1 t.R3_VALOR from @SR7010 t where t.R7_DATA > @R7_DATA order by t.R7_DATA desc)
            end
       end
       FETCH NEXT FROM db_cursor INTO @R7_FILIAL, @R7_MAT, @R7_DESCFUN, @R7_DATA    
END   

CLOSE db_cursor   
DEALLOCATE db_cursor

select * from @SR7010_new
  • With the SUM, the number of correct lines appears, however, how do I bring the value of the R3_VALOR field for each line? With the sum, it sums and repeats the sum result for the other lines

  • Sorry, I haven’t seen your R7_DATA column, all data is different.

  • You want to add up the column or bring only one row with its value?

  • The correct result should be like the second image, in the question. Each R7_VALOR related to R7_DATA, which is related to R7_DESCFUN

  • See the edition with MIN and MAX.

  • Not...now the values change with each line, however, the data that repeat are the ones that start with R7...that is, just reversed the situation :(

Show 1 more comment

Browser other questions tagged

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