Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
gradstudent2010
Starting Member
4 Posts |
Posted - 2007-10-05 : 17:22:20
|
I'm using CF8 and I have an SQL issue. I'm trying to display data from three tables using one query. Here is the table information with alias and data examples:*[pkey] means it's the primary key.Doc d - [pkey]d.docID, d.docTitle (Holds the doctitle for each docID)EX:111 Emp Application222 Budget Request333 Vacation RequestReadData r - [pkey]r.id, r.transactID, r.docID, r.itemID (holds multiple docID for each transactID, and holds multiple itemid for each docID)EX:1 12345 111 12 12345 111 23 12345 111 34 12345 222 15 73624 111 1Doctransact dt - [pkey]di.id, dt.docID, dt.transactID (Holds multiple docID for each transactID)EX:1 1112 2223 333I want to display:1.) all d.docTitles WHERE d.docID = dt.docID AND dt.transactID = 1.2.) the Max r.itemid (if any) for all rows in the above query results, WHERE r.docID = dt.docID AND r.transactID = dt.transactIDHere is the SQL I have written:SELECT d.doctitle, r.itemidFROM (file_doctransact dt INNER JOIN file_doc d ON dt.docID = d.docID) LEFT OUTER JOIN file_readdata r ON dt.docID = r.docIDWHERE dt.transactID = 1Which displays: ALL r.itemid (if any) for all rows in the above query results, where r.docID = dt.docID. I don’t want to display multiple rows of [the same docTitle and different docID]. I want it to display the one with the highest docID. I get an error when I try to use the Max() function, such as:SELECT d.doctitle, Max(r.itemid). Please help. |
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-10-05 : 17:45:25
|
group by d.doctitle |
 |
|
gradstudent2010
Starting Member
4 Posts |
Posted - 2007-10-06 : 02:51:57
|
That doesn't work. It gives me an error:Error Executing Database Query. You tried to execute a query that does not include the specified expression 'docID' as part of an aggregate function. I even tried using d.docID. |
 |
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-10-08 : 09:24:37
|
Is d.docid in your Select list? |
 |
|
johnsql
Posting Yak Master
161 Posts |
Posted - 2007-10-08 : 12:11:50
|
quote: Originally posted by gradstudent2010 I'm using CF8 and I have an SQL issue. I'm trying to display data from three tables using one query. Here is the table information with alias and data examples:*[pkey] means it's the primary key.Doc d - [pkey]d.docID, d.docTitle (Holds the doctitle for each docID)EX:111 Emp Application222 Budget Request333 Vacation RequestReadData r - [pkey]r.id, r.transactID, r.docID, r.itemID (holds multiple docID for each transactID, and holds multiple itemid for each docID)EX:1 12345 111 12 12345 111 23 12345 111 34 12345 222 15 73624 111 1Doctransact dt - [pkey]di.id, dt.docID, dt.transactID (Holds multiple docID for each transactID)EX:1 1112 2223 333I want to display:1.) all d.docTitles WHERE d.docID = dt.docID AND dt.transactID = 1.2.) the Max r.itemid (if any) for all rows in the above query results, WHERE r.docID = dt.docID AND r.transactID = dt.transactIDHere is the SQL I have written:SELECT d.doctitle, r.itemidFROM (file_doctransact dt INNER JOIN file_doc d ON dt.docID = d.docID) LEFT OUTER JOIN file_readdata r ON dt.docID = r.docIDWHERE dt.transactID = 1Which displays: ALL r.itemid (if any) for all rows in the above query results, where r.docID = dt.docID. I don’t want to display multiple rows of [the same docTitle and different docID]. I want it to display the one with the highest docID. I get an error when I try to use the Max() function, such as:SELECT d.doctitle, Max(r.itemid). Please help.
Your provided data and your query conflict to each other. How can dt.transactID = 1? |
 |
|
gradstudent2010
Starting Member
4 Posts |
Posted - 2007-10-08 : 21:12:15
|
I think I figured it out.SELECT d.docTitleFROM (file_doctransact AS dt INNER JOIN file_doc AS d ON dt.docID = d.docID) LEFT JOIN file_readdata AS r ON dt.docID = r.docIDWHERE (((dt.transactID)=1) And r.itemid is Null or (((SELECT Max(itemid) AS Expr1 FROM file_readdata WHERE transactID=dt.transactID And docid = dt.docID))=[r].[itemid]));dt.transactID is a variable, which for testing/display purposes is set to one. Thanks for all your help, and if you have any advice on the above, let me know. |
 |
|
|
|
|
|
|