Solved

Visual Studio 2013 Querybuider

Posted on 2015-02-13
7
139 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 15

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 15

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 51

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

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…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

627 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