Solved

Visual Studio 2013 Querybuider

Posted on 2015-02-13
7
117 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
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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 47

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with exporting to excel 4 27
T-SQL: "HAVING CASE" Clause 1 24
SQL Error - Query 6 25
CPU high usage when update statistics 2 29
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

776 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