Solved

Visual Studio 2013 Querybuider

Posted on 2015-02-13
7
101 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:OhioWoodWright
Comment Utility
Un fortunately I get the same error
0
 

Author Comment

by:OhioWoodWright
Comment Utility
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
Comment Utility
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 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

763 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

9 Experts available now in Live!

Get 1:1 Help Now