0

I'm trying to get data from SQL database with SELECT:

enter image description here

SELECT DISTINCT T2D.NO_ARTIKLU,
                T2D.NAZEV_ARTIKLU,
                T2D.DATUM,
                T10.NO_PARTNERA
    FROM D3000S.DIA2ZZ0D T2D, D3000S.DIA1ZZ00 T10, D3000S.DIA2ZZ00 T20
    WHERE T10.NO_PARTNERA = T2D.NO_PARTNERA AND T2D.AKTIVITA > 0

The problem is -> There are most of duplicates that I want to filter with SELECT and I need to show only article with the latest datetime.

Thank you for the answers.

Swerph
  • 1
  • 1

2 Answers2

0

Please see below query .Rank can be used to eliminate the duplicate values.

  SELECT * FROM
  (
 SELECT DISTINCT T2D.NO_ARTIKLU,
            T2D.NAZEV_ARTIKLU,
            T2D.DATUM,
            T10.NO_PARTNERA,Row_Number() OVER(PARTITION BY Duplicatidentifier ORDER 
    By Datetimecolumn desc)
    AS Rank 
     FROM D3000S.DIA2ZZ0D T2D, D3000S.DIA1ZZ00 T10, D3000S.DIA2ZZ00 T20
    WHERE T10.NO_PARTNERA = T2D.NO_PARTNERA AND T2D.AKTIVITA > 0
   ) AS B WHERE Rank=1
Its_Ady
  • 308
  • 2
  • 10
0

Firstly, echoing a previous comment, you should use ANSI join syntax. Reasons include increased clarity and support for full outer joins. The topic is discussed here

An obvious problem with your query, is that you do not join the third table to anything. Fixing this gives:

SELECT DISTINCT T2D.NO_ARTIKLU,
                T2D.NAZEV_ARTIKLU,
                T2D.DATUM,
                T10.NO_PARTNERA
    FROM D3000S.DIA2ZZ0D T2D
    INNER JOIN D3000S.DIA1ZZ00 T10 ON T10.NO_PARTNERA = T2D.NO_PARTNERA
    INNER JOIN D3000S.DIA2ZZ00 T20 ON T10.NO_PARTNERA = T20.NO_PARTNERA
    WHERE T2D.AKTIVITA > 0
snarpel
  • 11
  • 1