Solved

SQL Server 2008 Query Issue - How to select just one row

Posted on 2016-08-04
4
56 Views
Last Modified: 2016-08-04
I am trying to bring back just one row in my resultset with a MAX(Permit.CreatedOn).

How can I fix my SP to bring one instead of 3. because every column value is going to be same.

Picture
USE [Fund]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_rpt_GeneralProjectReport]
    @IsLoan bit,
    @ProjectNumber  varchar(20),		
   @Success bit output,
  @Message varchar(1000) output
	 
AS
BEGIN
	BEGIN TRY     
			SELECT 
			      -- Report Top Part
				  Project.ProjectNumber,
			      Project.ApplicantName,
			      Project.SpecificLocation,	
			      ProjectAssignmentInformation.[Description],
			       
			      --Inside the Tabular column
			      ProjectType.[Type],
			      District.DistrictOffice,
			      Division.Name As DistrictName,
			      ProjectStatus.[Status],
			      Project.RevolvingLoanUnit,
			       
			      RiverBasin.Name As RiverBasinName,
			      Permit.PermitNumber,
			      Permit.CreatedOn As NPDESIssueDate,			      
			      Employee.FirstName + ' ' + Employee.LastName AS TSUReviewer,			      
				  ProjectAssignmentInformation.PlanReceiptDate,
				  
				  ProjectAssignmentInformation.EmployerIdentificationNumber,	  
				  DBO.ufn_GeneralProjectReport_GetCountyName(ProjectAssignmentInformation.CountyId) AS MainCounty,
				  DBO.ufn_GeneralProjectReport_GetCountyName(ProjectAssignmentInformation.SecondCountyId) AS SecondCounty,
				  DBO.ufn_GeneralProjectReport_GetCountyName(ProjectAssignmentInformation.ThirdCountyId) AS ThirdCounty,	
			      dbo.ufn_OOAPreAwardStatus_Milestones(@ProjectNumber,'EA/FNSI Public Notice Clearance') AS FactSheetIssued,
			      
				  ProjectPriorityAssignmentSRFSWQIFNPS.ComplianceDate As ComplianceDate,
			      COALESCE(ProjectPriorityAssignmentSRFSWQIFNPS.PPLYear, ProjectPriorityAssignmentDWRF.PPLYear) AS PPLYear,
				  COALESCE(ProjectPriorityAssignmentSRFSWQIFNPS.ContingentOrFuture, ProjectPriorityAssignmentDWRF.ContingentOrFuture) AS ContingentOrFuture,							      ProjectLoanApplication.TargetedIODate AS OOATargetDate,
				  ProjectPriorityListOrderOfApprovalDate.ProjectPriorityListDate  AS OOAActualDate,	
				  		
				  LoanInterestRate.InterestRate,
				  ProjectOrderOfApprovalInformation.RepaymentStartDate,
				  ProjectOrderOfApprovalInformation.RepaymentEndDate,
				  ProjectDisbursementInformation.BankAccountNumber,
				  ProjectDisbursementInformation.BankABANumber,
				  
				  ProjectLoanApplication.AuthorizedRepResolutionDate,
				  ProjectLoanApplication.TargetedConstructionStartDate,
				  ProjectLoanApplication.TargetedConstructionCompletionDate,
				  ProjectLoanApplication.AuthorizingStatuteId,
				  ProjectLoanApplication.BondCeilingAmount,
				  
				  ProjectLoanApplication.TargetedIODate AS IOTargetDate,
				  ProjectCompletionInformation.InitiationOfOperationsActualDate AS IOActualDate, 
				  ----FirmContact.FirstName +' '+ FirmContact.LastName AS 'Engineer Contact',
				  ----FirmContact.FirstName +' '+ FirmContact.LastName AS 'Bond Counsel Contact',
				  ----FirmContact.FirstName +' '+ FirmContact.LastName AS 'Financial Advisor Contact',
				   
				  ----FirmContact.FirstName +' '+ FirmContact.LastName AS 'Accountant Contact',
				  ----FirmContact.FirstName +' '+ FirmContact.LastName AS 'Authorized Rep Contact',
				  ----FirmContact.FirstName +' '+ FirmContact.LastName AS 'Attorney Contact',
				  ----FirmContact.FirstName +' '+ FirmContact.LastName AS 'Miscellaneous Contact',
				  ----FirmContact.FirstName +' '+ FirmContact.LastName AS 'District Engineer Contact',
				  
				  ProjectLoanApplication.Date47004CivilRightsComplianceFormSignedByApplicant,
				   CASE
					  WHEN DATALENGTH(ProjectLoanApplication.CapitalizedInterest) > 0 
							Then 'Yes' 
							Else 'No' 
				   END CapitalizedInterest,	
				  ProjectLoanApplication.DatePartISentToMFA,
				  ProjectLoanApplication.DatePartIMFAApprovalMemo,
				  ProjectLoanApplication.DatePartIReceived,	
				  
				  ProjectLoanApplication.DatePartIIReceived,
				  ProjectLoanApplication.DatePartIIIReceived,
				  CASE
					  WHEN DATALENGTH(ProjectLoanApplication.AllDebarmentFormsSubmitted) > 0 
							Then 'Yes' 
							Else 'No' 
				   END AllDebarmentFormsSubmitted,
				  ProjectCategoryCostInformation.PPLCategorySumAmount, 
				  ProjectCategoryCostInformation.OOACategorySumAmount,  
				  
				  ProjectCategoryCostInformation.FinalCategorySumAmount,
				  COALESCE(ProjectPriorityAssignmentSRFSWQIFNPS.CurrentPopulationServed, ProjectPriorityAssignmentDWRF.CurrentPopulationServed) AS Population,
				  COALESCE(ProjectPriorityAssignmentSRFSWQIFNPS.TotalPoints, ProjectPriorityAssignmentDWRF.TotalPoints) AS TotalPoints,				 
				  CASE
					  WHEN DATALENGTH(ProjectPriorityAssignmentSRFSWQIFNPS.Grandfathered) > 0 
							Then 'Yes' 
							Else 'No' 
				  END Grandfathered,
				  ProjectOrderOfApprovalInformation.LoanAmount,
				  
				  ProjectCompletionInformation.MDEQAdministrativelyCompleteLetterDate,
				  ProjectCompletionInformation.TreasuryLoanAdjustmentLetterDate,
				  ProjectCompletionInformation.FinalLineItemAmount,
				  NULL	--This one will be removed at the end before shelving			
				   
			  FROM Project	
				   INNER JOIN ProjectType
				           ON (ProjectType.ProjectTypeId = Project.ProjectTypeId AND (ProjectType.IsLoan = @IsLoan))	
				   INNER JOIN ProjectStatus
						   ON Project.ProjectStatusId = ProjectStatus.ProjectStatusId 
				   INNER JOIN ProjectAssignmentInformation	   
				 	       ON Project.ProjectId = ProjectAssignmentInformation.ProjectId  
				   INNER JOIN District
				           ON District.DistrictId = Project.DistrictId 
				   INNER JOIN Division
				           ON Division.DivisionId = Project.DivisionId
				   INNER JOIN RiverBasin
				           ON RiverBasin.RiverBasinId = ProjectAssignmentInformation.RiverBasinId	
				    LEFT JOIN ProjectPermit
				           ON ProjectPermit.ProjectId = Project.ProjectId
				    LEFT JOIN Permit
				           ON Permit.PermitId = ProjectPermit.PermitId
				   INNER JOIN Employee
				           ON Employee.EmployeeId = ProjectAssignmentInformation.RateReviewerEmployeeId
				    LEFT JOIN ProjectPriorityAssignmentDWRF    
                           ON Project.ProjectId = ProjectPriorityAssignmentDWRF.ProjectId
                    LEFT JOIN ProjectPriorityAssignmentSRFSWQIFNPS   
                           ON Project.ProjectId = ProjectPriorityAssignmentSRFSWQIFNPS.ProjectId    	 	 
				    LEFT JOIN ProjectLoanApplication	   
			               ON Project.ProjectId = ProjectLoanApplication.ProjectId 
			       INNER JOIN ProjectPriorityListOrderOfApprovalDate	
					       ON ProjectPriorityListOrderOfApprovalDate.ProjectPriorityListOrderOfApprovalDateId
						   =  CASE
									WHEN Project.ProjectTypeId= 5 THEN ProjectPriorityAssignmentDWRF.ProjectPriorityListOrderOfApprovalDateId
									WHEN Project.ProjectTypeId IN ( 3,4) THEN ProjectPriorityAssignmentSRFSWQIFNPS.ProjectPriorityListOrderOfApprovalDateId																							
							   END 
			        LEFT JOIN ProjectOrderOfApprovalInformation
			               ON Project.ProjectId = ProjectOrderOfApprovalInformation.ProjectId 
			        LEFT JOIN LoanInterestRate
			               ON LoanInterestRate.LoanInterestRateId = ProjectOrderOfApprovalInformation.LoanInterestRateId 
			        LEFT JOIN ProjectDisbursementInformation
			               ON ProjectDisbursementInformation.ProjectId = Project.ProjectId
			        LEFT JOIN ProjectCompletionInformation
			               ON ProjectCompletionInformation.ProjectId = Project.ProjectId
	                LEFT JOIN ProjectCategoryCostInformation       
                           ON ProjectCategoryCostInformation.ProjectId = Project.ProjectId 
			          
			    --   INNER JOIN ProjectFirmContact
					  --     ON Project.ProjectId = ProjectFirmContact.ProjectId
				   --INNER JOIN FirmContact 
					  --     ON ProjectFirmContact.FirmContactId = FirmContact.FirmContactId 
				   --INNER JOIN Firm 
					  --     ON FirmContact.FirmId = Firm.FirmId
				   --INNER JOIN ContactType
				   --        ON Firm.ContactTypeId = ContactType.ContactTypeId  
			    -- 
			  
		     WHERE Project.ProjectNumber=@ProjectNumber --AND MAX(Permit.CreatedOn) 
		  
		      
	        SELECT @Success = 1,
		           @Message = 'Successfully retrieved General Project Report data by loans ProjectType'         

	END TRY
	
	BEGIN CATCH
	
		    SELECT @Success = 0,
			       @Message = 'An error occurred retrieving General Project Report data by loans Project Number, the error was:' + ERROR_MESSAGE()
	END CATCH
END

Open in new window

0
Comment
Question by:Subbu G
[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
  • 2
  • 2
4 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41742380
you can add the TOP clause :

			SELECT TOP 1
			      -- Report Top Part
				  Project.ProjectNumber,
			      Project.ApplicantName,
			      Project.SpecificLocation,	
			      ProjectAssignmentInformation.[Description],

Open in new window

0
 

Author Comment

by:Subbu G
ID: 41742406
Eric , thanks for your reply. I dont want to show just the first record, I want the one which has the most recent dates on this field (Permit.CreatedOn ) As NPDESIssueDate
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41742421
just be sure to include the ORDER BY clause:

			SELECT TOP 1
			      -- Report Top Part
				  Project.ProjectNumber,
			      Project.ApplicantName,
			      Project.SpecificLocation,	
			      ProjectAssignmentInformation.[Description],
....
			ORDER BY Permit.CreatedOn DESC

Open in new window

1
 

Author Closing Comment

by:Subbu G
ID: 41742453
thanks Eric. This one helps
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

762 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