Friday, March 23, 2012

Query for Last Date in a table

I am new to creating SQL queries and need some help.

I am trying to query from three tables.
1 - A table with a list of Drawings (DRAWINGS)
2 - A table with the Revision history of the Drawings (REVISIONS)
3 - A table with the list of Projects (WRI_SITE_PRJS)

The result I am looking for:
Name of Drawing (I want to return the drawings that don't have a revision as well)
Last Drawing Revision
Date of Revision
Project that the drawing belongs on

The below query I created in access and the result was exactly what I was expecting.

SELECT DRAWINGLIST.NAME, DRAWINGLIST.WRI_DESCRIPTION AS [Drawing Title], LastRev.LastOfREVISIONNAME AS Rev, LastDate.LastOfREVISIONDATE AS [Rev Date], WRI_SITE_PRJS.TITLE AS Project
FROM LastRev RIGHT JOIN (LastDate RIGHT JOIN (DRAWINGLIST INNER JOIN WRI_SITE_PRJS ON DRAWINGLIST.WPRJID = WRI_SITE_PRJS.WPRJID) ON LastDate.NAME = DRAWINGLIST.NAME) ON LastRev.NAME = DRAWINGLIST.NAME
ORDER BY DRAWINGLIST.NAME;

I tried getting the same result in SQL and find that I can't GROUP BY the LAST date in a table. The result I'm getting is all the revisions for the drawing.

My question is: How do I query for the LAST date or revision in a table.

I'm using Microsoft SQL Server 2003

Some help is greatly appreciated.
OS

Quote:

Originally Posted by oszapped

I am new to creating SQL queries and need some help.

I am trying to query from three tables.
1 - A table with a list of Drawings (DRAWINGS)
2 - A table with the Revision history of the Drawings (REVISIONS)
3 - A table with the list of Projects (WRI_SITE_PRJS)

The result I am looking for:
Name of Drawing (I want to return the drawings that don't have a revision as well)
Last Drawing Revision
Date of Revision
Project that the drawing belongs on

The below query I created in access and the result was exactly what I was expecting.

SELECT DRAWINGLIST.NAME, DRAWINGLIST.WRI_DESCRIPTION AS [Drawing Title], LastRev.LastOfREVISIONNAME AS Rev, LastDate.LastOfREVISIONDATE AS [Rev Date], WRI_SITE_PRJS.TITLE AS Project
FROM LastRev RIGHT JOIN (LastDate RIGHT JOIN (DRAWINGLIST INNER JOIN WRI_SITE_PRJS ON DRAWINGLIST.WPRJID = WRI_SITE_PRJS.WPRJID) ON LastDate.NAME = DRAWINGLIST.NAME) ON LastRev.NAME = DRAWINGLIST.NAME
ORDER BY DRAWINGLIST.NAME;

I tried getting the same result in SQL and find that I can't GROUP BY the LAST date in a table. The result I'm getting is all the revisions for the drawing.

My question is: How do I query for the LAST date or revision in a table.

I'm using Microsoft SQL Server 2003

Some help is greatly appreciated.
OS


I have never used SQL Server 2003
but i would pressume it would be order by LastDate.LastOfREVISIONDATE DESC|||

Quote:

Originally Posted by oszapped

I am new to creating SQL queries and need some help.

I am trying to query from three tables.
1 - A table with a list of Drawings (DRAWINGS)
2 - A table with the Revision history of the Drawings (REVISIONS)
3 - A table with the list of Projects (WRI_SITE_PRJS)

The result I am looking for:
Name of Drawing (I want to return the drawings that don't have a revision as well)
Last Drawing Revision
Date of Revision
Project that the drawing belongs on

The below query I created in access and the result was exactly what I was expecting.

SELECT DRAWINGLIST.NAME, DRAWINGLIST.WRI_DESCRIPTION AS [Drawing Title], LastRev.LastOfREVISIONNAME AS Rev, LastDate.LastOfREVISIONDATE AS [Rev Date], WRI_SITE_PRJS.TITLE AS Project
FROM LastRev RIGHT JOIN (LastDate RIGHT JOIN (DRAWINGLIST INNER JOIN WRI_SITE_PRJS ON DRAWINGLIST.WPRJID = WRI_SITE_PRJS.WPRJID) ON LastDate.NAME = DRAWINGLIST.NAME) ON LastRev.NAME = DRAWINGLIST.NAME
ORDER BY DRAWINGLIST.NAME;

I tried getting the same result in SQL and find that I can't GROUP BY the LAST date in a table. The result I'm getting is all the revisions for the drawing.

My question is: How do I query for the LAST date or revision in a table.

I'm using Microsoft SQL Server 2003

Some help is greatly appreciated.
OS


Use Max() on the date column and group by everything else.

No comments:

Post a Comment