D
D
Dmitry Nosikov2020-07-13 17:09:34
MySQL
Dmitry Nosikov, 2020-07-13 17:09:34

How to query data from three SQL tables without duplicates?

Good afternoon, colleagues! The situation is this: There are 3 tables in the database:
1) NA_ListADM, it contains the name (Name) and the OGRN of the organization.
2) NA_MessagesADM, this table contains the document ID (RegNumIndex), as well as the OGRN of the organization that created
it reg. number (RegNumIndex) and so on.
The essence of the issue is as follows, I'm trying to pull out with one request the Date of creation of the document, its name, PSRN and the name of the organization, but I get a huge bunch of duplicates.

If I use a query like this:

SELECT DISTINCT NotarialActs.DocDate, NotarialActs.RegNumIndex, NotarialActs.DocName,
NotarialActs.ActCertifier AS 'UName',NotarialActs.XML AS 'Text'
#, NA_ListADM.OGRN, NA_ListADM.Name
FROM NotarialActs
LEFT JOIN NA_MessagesADM ON NA_MessagesADM.RegNumIndex = NotarialActs.RegNumIndex
LEFT JOIN NA_ListADM ON NA_ListADM.OGRN = NA_MessagesADM.OGRN
WHERE NotarialActs.DocDate LIKE '2020-07%'
AND NotarialActs.DocDate = NA_MessagesADM.DocDate
AND NA_MessagesADM.Status='1'
;


Everything is great. However, it is worth uncommenting the 3rd line, in which I get the OGRN and the name of the organization that created the document - as 4 extra takes immediately appear. Tell me, please, what could be my mistake? I've been struggling with this request for 4 hours now. I would be grateful for any hint. I also attach a screenshot of the table structure.
5f0c6adddfb90711677051.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VicTHOR, 2020-07-13
@deimon260993

SELECT a.DocDate, a.RegNumIndex, a.DocName, a.ActCertifier 'UName', a.XML 'Text' , l.OGRN, l.Name
FROM NA_MessagesADM m
JOIN NotarialActs a
  ON m.RegNumIndex = a.RegNumIndex
JOIN NA_ListADM l
  ON m.OGRN = l.OGRN
WHERE a.DocDate LIKE '2020-07%'
GROUP BY m.RegNumIndex, m.OGRN;

R
Rsa97, 2020-07-13
@Rsa97

This can happen if there are several records in the NA_ListADM table with the same OGRN and different company names.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question