Answer the question
In order to leave comments, you need to log in
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'
;
Answer the question
In order to leave comments, you need to log in
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;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question