1

I have four tables:

  • dbo.Projects (id, ProjectName, Areas, PaymentSystem, Districts.id, purpose.id, types.id, etc)
  • dbo.Districts(id, DistrictsName)
  • dbo.Purpose (id, PurposeName) - has residential & commercial
  • dbo.Types (id, typName)

I want to select DistrictsName where PurposeName = 'residential'

I tried this procedure :

CREATE PROCEDURE [dbo].[SearchResidentialProjects]   
AS
SELECT 
    dbo.Projects.ID,
    dbo.Districts.DistrictName,
    dbo.Purpose.PurposeName
FROM 
    dbo.Projects 
INNER JOIN 
    dbo.Purpose ON dbo.Projects.PurposeID = dbo.Purpose.ID 
INNER JOIN  
    dbo.Districts ON dbo.Projects.DistrictID = dbo.Districts.ID
WHERE 
    dbo.Purpose.PurposeName = N'Residential'

this is the result from this procedure:

ID DistrictsName PurposeName 
 1   District1    residential
 2   District1    residential
 3   District2    residential
 4   District2    residential

i want display the DistrictsName without duplicate or with different values , i a have also one more project per district in projects records . this what i want to display :

ID DistrictsName PurposeName 
 1   District1    residential
 2   District2    residential

how i get this result , any help is appreciated.

1
  • What is the purpose of the ID column in your preferred result? You can't link it back to the original record. Do you just need some sort of count? Commented Dec 19, 2016 at 13:38

2 Answers 2

2

Why do people use stored procedures when views are much more appropriate? I have never understood this. It seems peculiar to SQL Server users.

In any case, you can do what you want with aggregation:

SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as id, 
       d.DistrictName, p.PurposeName
FROM dbo.Projects pr INNER JOIN 
     dbo.Purpose pu
     ON pr.PurposeID = pu.ID INNER JOIN  
     dbo.Districts d
     ON pr.DistrictID = d.ID
WHERE pu.PurposeName = N'Residential'
GROUP BY d.DistrictName, p.PurposeName;

The use of table aliases makes the query much easier to write and to read.

In addition, I don't understand the id column being output. Why would you want to construct a new id? In any case, that is what your data suggests.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much
I haven't met any SQL Server users that prefer stored procedures to views. I have seen questions from users of other databases that migrated to SQL Server that think that they need stored procedures. It's as if people get the idea that "if it's on the server, it's a stored procedure". Or they get bitten by the ancient fallacy that somehow stored procedures are faster because they are stored procedures ...
-1

Use DISTINCT statement for removing the duplicates:

CREATE PROCEDURE [dbo].[SearchResidentialProjects]   
AS
SELECT DISTINCT
    dbo.Projects.ID,
    dbo.Districts.DistrictName,
    dbo.Purpose.PurposeName
FROM 
    dbo.Projects 
INNER JOIN 
    dbo.Purpose ON dbo.Projects.PurposeID = dbo.Purpose.ID 
INNER JOIN  
    dbo.Districts ON dbo.Projects.DistrictID = dbo.Districts.ID
WHERE 
    dbo.Purpose.PurposeName = N'Residential'

1 Comment

distinct won't help here. because dbo.Projects.ID is unique.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.