Solved

Visual Studio 2013 Querybuider

Posted on 2015-02-13
7
112 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 46

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 the fundamental information of how to create a table.

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now