Solved

Visual Studio 2013 Querybuider

Posted on 2015-02-13
7
133 Views
Last Modified: 2015-03-06
I am using a databound gridview to display information about ongoing projects. The data is primarily pulled from the tProjects table but associated with several other tables, one of which is tComments. There can be multiple comments per project. Currently the gridview is displaying one line for each project/comment pair so that if there are three comments on a project there are three lines displayed. I need to limit the display to 1 row with only the most recent comment. I've tried using  TOP in several ways but can't seem to get it to work.

This is the SQL that displays 1 row per project/comment pair. How would I modify it to limit the display to the most recent comment?

SELECT       tUsers.strUserName AS [Section Lead], tLocations.strLocation AS Location,
                    tProjects.strDWGNumber AS [Drawing Number], tProjectStatuses.strStatus AS Status,
                    tProjects.dteDueDate AS [Due Date], tProjects.intProjectID, tComments.strComment AS
                    Comment, tComments.dtmDateEntered AS [Comment Date]
FROM         tProjects INNER JOIN tUsers ON tProjects.intUserID = tUsers.intUserID INNER JOIN
                    tProjectStatuses ON tProjects.intProjectStatusID = tProjectStatuses.intProjectStatusID
                    INNER JOIN tLocations ON tUsers.intLocationID = tLocations.intLocationID INNER JOIN
                    tComments ON tProjects.intProjectID = tComments.intProjectID
WHERE      (tProjects.dteDueDate < @Today)
ORDER BY [Due Date], Location, [Comment Date] DESC

Thanks
0
Comment
Question by:OhioWoodWright
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 14

Expert Comment

by:Lokesh B R
ID: 40608349
SELECT       TOP 1 (tUsers.strUserName AS [Section Lead], tLocations.strLocation AS Location,
                    tProjects.strDWGNumber AS [Drawing Number], tProjectStatuses.strStatus AS Status,
                    tProjects.dteDueDate AS [Due Date], tProjects.intProjectID, tComments.strComment AS
                    Comment, tComments.dtmDateEntered AS [Comment Date])
FROM         tProjects INNER JOIN tUsers ON tProjects.intUserID = tUsers.intUserID INNER JOIN
                    tProjectStatuses ON tProjects.intProjectStatusID = tProjectStatuses.intProjectStatusID
                    INNER JOIN tLocations ON tUsers.intLocationID = tLocations.intLocationID INNER JOIN
                    tComments ON tProjects.intProjectID = tComments.intProjectID
WHERE      (tProjects.dteDueDate < @Today)
ORDER BY  [tComments.dtmDateEntered] DESC
0
 

Author Comment

by:OhioWoodWright
ID: 40608384
Thanks for the suggestion but when I try that I get the following error:

The following errors were encountered while parsing the contents of the SQL pane:
      Error in SELECT clause: expression near 'AS'.
      Missing FROM clause.
      Error in SELECT clause: expression near ','.
      Unable to parse query text.
0
 
LVL 14

Expert Comment

by:Lokesh B R
ID: 40608389
Hi,
Sorry, try this

SELECT       TOP 1 (tUsers.strUserName AS [Section Lead], tLocations.strLocation AS Location,
                    tProjects.strDWGNumber AS [Drawing Number], tProjectStatuses.strStatus AS Status,
                    tProjects.dteDueDate AS [Due Date], tProjects.intProjectID, tComments.strComment AS
                    Comment, tComments.dtmDateEntered AS [Comment Date]
FROM         tProjects INNER JOIN tUsers ON tProjects.intUserID = tUsers.intUserID INNER JOIN
                    tProjectStatuses ON tProjects.intProjectStatusID = tProjectStatuses.intProjectStatusID
                    INNER JOIN tLocations ON tUsers.intLocationID = tLocations.intLocationID INNER JOIN
                    tComments ON tProjects.intProjectID = tComments.intProjectID
WHERE      (tProjects.dteDueDate < @Today))
ORDER BY  [tComments.dtmDateEntered] DESC
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:OhioWoodWright
ID: 40608417
Un fortunately I get the same error
0
 

Author Comment

by:OhioWoodWright
ID: 40608437
OK, I finally got this to run without error but it only shows 1 project not all projects with the latest comment for each project.

SELECT TOP(1)      tUsers.strUserName AS [Section Lead], tLocations.strLocation AS Location,
                     tProjects.strDWGNumber AS [Drawing Number], tProjectStatuses.strStatus AS Status,
                     tProjects.dteDueDate AS [Due Date], tProjects.intProjectID, tComments.strComment AS
                     Comment, tComments.dtmDateEntered AS [Comment Date]
 FROM         tProjects INNER JOIN tUsers ON tProjects.intUserID = tUsers.intUserID INNER JOIN
                     tProjectStatuses ON tProjects.intProjectStatusID = tProjectStatuses.intProjectStatusID
                     INNER JOIN tLocations ON tUsers.intLocationID = tLocations.intLocationID INNER JOIN
                     tComments ON tProjects.intProjectID = tComments.intProjectID
 WHERE      (tProjects.dteDueDate < @Today)
 ORDER BY [Due Date], Location, [Comment Date] DESC

Any help would be greatly appreciated.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40613408
Try it this way:
SELECT  u.strUserName [Section Lead],
        l.strLocation Location,
        p.strDWGNumber [Drawing Number],
        s.strStatus [Status],
        p.dteDueDate AS [Due Date],
        p.intProjectID,
        c.strComment Comment,
        c.dtmDateEntered [Comment Date]
FROM    tProjects p
        INNER JOIN tUsers u ON p.intUserID = u.intUserID
        INNER JOIN tProjectStatuses s ON p.intProjectStatusID = s.intProjectStatusID
        INNER JOIN tLocations l ON u.intLocationID = l.intLocationID
        INNER JOIN tComments c ON tProjects.intProjectID = tComments.intProjectID
        INNER JOIN (SELECT  intProjectID,
                            MAX(c.dtmDateEntered) LastDateEntered
                    FROM    tComments
                    GROUP BY intProjectID
                   ) c2 ON p.intProjectID = c2.intProjectID
                           AND c.dtmDateEntered = c2.LastDateEntered
WHERE   p.dteDueDate < @Today

Open in new window

0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40616448
You can use Common Table Expression (CTE) to get the last comment for each Project and then use it on your SELECT:
WITH CTE_ProjComment (intProjectID, CommentDate)
AS (
	SELECT tComments.intProjectID, MAX(tComments.dtmDateEntered AS [Comment Date])
	FROM tProjects 
		INNER JOIN tComments ON tProjects.intProjectID = tComments.intProjectID
	GROUP BY tComments.intProjectID)

SELECT tUsers.strUserName AS [Section Lead], tLocations.strLocation AS Location,
       tProjects.strDWGNumber AS [Drawing Number], tProjectStatuses.strStatus AS Status,
       tProjects.dteDueDate AS [Due Date], tProjects.intProjectID, 
       tComments.strComment AS Comment, MCTE_ProjComment.CommentDate AS[Comment Date]
FROM tProjects 
	INNER JOIN tUsers ON tProjects.intUserID = tUsers.intUserID 
	INNER JOIN tProjectStatuses ON tProjects.intProjectStatusID = tProjectStatuses.intProjectStatusID 
	INNER JOIN tLocations ON tUsers.intLocationID = tLocations.intLocationID 
	INNER JOIN tComments c ON tProjects.intProjectID = tComments.intProjectID
	INNER JOIN CTE_ProjComment ON tComments.intProjectID = CTE_ProjComment.intProjectID
WHERE (tProjects.dteDueDate < @Today)
ORDER BY [Due Date], Location, [Comment Date] DESC

Open in new window

0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question