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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL Aggregation of selected rows

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 Application
222 Budget Request
333 Vacation Request


ReadData 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 1
2 12345 111 2
3 12345 111 3
4 12345 222 1
5 73624 111 1


Doctransact dt - [pkey]di.id, dt.docID, dt.transactID
(Holds multiple docID for each transactID)

EX:
1 111
2 222
3 333


I 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.transactID

Here is the SQL I have written:

SELECT d.doctitle, r.itemid
FROM (file_doctransact dt INNER JOIN file_doc d ON dt.docID = d.docID) LEFT OUTER JOIN file_readdata r ON dt.docID = r.docID
WHERE dt.transactID = 1

Which 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
Go to Top of Page

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.
Go to Top of Page

Van
Constraint Violating Yak Guru

462 Posts

Posted - 2007-10-08 : 09:24:37
Is d.docid in your Select list?
Go to Top of Page

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 Application
222 Budget Request
333 Vacation Request


ReadData 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 1
2 12345 111 2
3 12345 111 3
4 12345 222 1
5 73624 111 1


Doctransact dt - [pkey]di.id, dt.docID, dt.transactID
(Holds multiple docID for each transactID)

EX:
1 111
2 222
3 333


I 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.transactID

Here is the SQL I have written:

SELECT d.doctitle, r.itemid
FROM (file_doctransact dt INNER JOIN file_doc d ON dt.docID = d.docID) LEFT OUTER JOIN file_readdata r ON dt.docID = r.docID
WHERE dt.transactID = 1

Which 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?
Go to Top of Page

gradstudent2010
Starting Member

4 Posts

Posted - 2007-10-08 : 21:12:15
I think I figured it out.

SELECT d.docTitle
FROM (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.docID
WHERE (((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.
Go to Top of Page
   

- Advertisement -