SQL query - multiple inner join

norbre

I have very limited knowledge in SQL, but I have to create a report query (in EPDM*) and I'm lost with INNER JOIN :)

*EPDM is a Project Data Management software, and it has a report generator. This needs a formatted query file.

This is a working query file content:

@[ListAllDoc] 
§Name [List all documents] 
§Company [Econ] 
§Description [This query lists all documents] 
§Version [1.1] 
§Arguments
[
  ProjectID pProjectID [1] [Select search folder. E.g "$\Documents", or browse for folder.]
]
§Sql 
[ 
SELECT D.Filename as 'File Name', P.Path As 'Path'
FROM Projects AS P 
INNER JOIN DocumentsInProjects AS DP ON P.ProjectID = DP.ProjectID
INNER JOIN Documents AS D ON DP.DocumentID = D.DocumentID
WHERE P.ProjectID = {pProjectID}
Order By P.Path ASC, D.Filename ASC
] 

The goal is a file list of a specified path with columns filename + path + username (of latest revision)

The data is stored the database as you see below (table and columns):

  • Documents => DocumentID, ProjectID, Filename, LatestRevisionNo
  • DocumentsInProjects => ProjectID, Path, DocumentID
  • Revisions => RevNr, DocumentID, UserID
  • Users => UserID, Username

The connection between the documents and its path is the DocumentsInProjects table with ProjectID column.

There are multiple revisions on the documents, and I'd liket to get the UserID from the revision table when the Documents.DocumentID = Revisions.DocumentID and Documents.LatestRevisionNo = Revisions.RevNr (user of the latest revision).

Sample data:

+------------------------------------------------------+
| Documents                                            |
+------------+-----------+----------+------------------+
| DocumentID | ProjectID | Filename | LatestRevisionNo |
+------------+-----------+----------+------------------+
| 100        | 10        | Test.txt | 3                |
+------------+-----------+----------+------------------+

+------------------------------------------------+
| DocumentsInProjects                            |
+---------------------+-------------+------------+
| ProjectID           | Path        | DocumentID |
+---------------------+-------------+------------+
| 10                  | D:\TestPath | 100        |
+---------------------+-------------+------------+

+-----------------------------+
| Revisions                   |
+-------+------------+--------+
| RevNr | DocumentID | UserID |
+-------+------------+--------+
| 1     | 10         | 55     |
+-------+------------+--------+
| 2     | 10         | 46     |
+-------+------------+--------+
| 3     | 10         | 32     |
+-------+------------+--------+

+-------------------+
| Users             |
+--------+----------+
| UserID | Username |
+--------+----------+
| 55     | Peter    |
+--------+----------+
| 46     | Mike     |
+--------+----------+
| 32     | Lucy     |
+--------+----------+

With the sample data above I'd like to get the result:

+-----------+-------------+------+
| File Name | Path        | User |
+-----------+-------------+------+
| Test.txt  | D:\TestPath | Lucy |
+-----------+-------------+------+

Here is where I am now, but this doesn't work of course :D

{pProjectID} is a variable taken from selection.

SELECT D.Filename As 'File Name', P.Path As 'Path', U.Username as 'User'
FROM Projects AS P
INNER JOIN DocumentsInProjects AS DP ON P.ProjectID = DP.ProjectID
INNER JOIN Documents AS D ON DP.DocumentID = D.DocumentID
INNER JOIN Revisions AS R ON D.DocumentID = R.DocumentID AND D.LatestRevisionNo = R.RevNr
INNER JOIN Users AS U ON U.UserID = D.UserID
WHERE P.ProjectID = {pProjectID}

please help me to correct this :)

UPDATE -> SOLUTION Thank you for all of you to helped me, even is my question was not well specified and contains errors, missing elements (sorry for that) :) @davidc2p helped me the most with the code correction, based upon that I made a little modification and this code works well:

SELECT D.Filename As 'File Name', P.Path As 'Path', U.Username As 'User'
FROM Projects AS P
INNER JOIN DocumentsInProjects AS DP ON P.ProjectID = DP.ProjectID
INNER JOIN Documents AS D ON DP.DocumentID = D.DocumentID
INNER JOIN Revisions AS R ON D.DocumentID = R.DocumentID AND D.LatestRevisionNo = R.RevNr
INNER JOIN Users AS U ON R.UserID = U.UserID
WHERE P.ProjectID = {pProjectID}

Thank you all!

davidc2p

Information about primary keys or unique keys should be provided. But considering what you sent your query has some errors:

There's no userID on table Documents. Your query should access UserID from Revisions table.

Also, DocumentID is not a field from DocumentsInProjects, you should link this table with ProjectID and therefore get all documents associated with a project.

Also, path is from DP not P.

SELECT D.Filename As 'File Name', DP.Path As 'Path', U.Username as 'User'
FROM Projects AS P

    INNER JOIN DocumentsInProjects AS DP 
    ON P.ProjectID = DP.ProjectID

    INNER JOIN Documents AS D 
    ON DP.ProjectID= D.ProjectID

    INNER JOIN Revisions AS R 
    ON D.DocumentID = R.DocumentID 
    AND D.LatestRevisionNo = R.RevNr

    INNER JOIN Users AS U 
    ON U.UserID = R.UserID

WHERE P.ProjectID = {pProjectID}

There are probably some other mistakes. It would be easier if you provided also temporary table creation with a set of sample data.

Also depending on unicity, your result might show duplicate information.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL Inner Join With Multiple Columns

分類Dev

SQL QUERY Inner Join missing data

分類Dev

Multiple join SQL Query Optimization for report

分類Dev

Django Inner Join on Derived Query

分類Dev

How to write a SQL query that subtracts INNER JOIN results from LEFT JOIN results?

分類Dev

inner join with pivot condition sql

分類Dev

SQL Inner Join 2 Tables

分類Dev

Inner Join with Sum in oracle SQL

分類Dev

SQL PIVOT after an INNER JOIN

分類Dev

How to Get Left Instead of Inner Join in Linq to SQL Navigation Property Generated Query

分類Dev

MySQL query - multiple JOIN

分類Dev

Mysql Join multiple Query

分類Dev

insert using sub-query and inner join

分類Dev

Joining Multiple tables Using Inner Join MYSQL

分類Dev

Add join to SQL query

分類Dev

Add join to SQL query

分類Dev

How to Delete using INNER JOIN with SQL Server?

分類Dev

SQL server inner join not returning Description

分類Dev

SQL - Inner Join Syntax after the on statement

分類Dev

SQL Server: inner join running total

分類Dev

SQL update selected rows with INNER JOIN

分類Dev

SQL Inner Join using Distinct and Order by Desc

分類Dev

SQL inner join list split for an SSRS report

分類Dev

SQL - Join with multiple condition

分類Dev

SQLite select query if inner join query doesn't exists

分類Dev

SQL Query Update using a join

分類Dev

SQL query, join two tables

分類Dev

Mysql query inner join on most recent date (today, yesterday, or before)

分類Dev

Query table data with inner join and two foreing keys