Thursday, March 22, 2012

Duplicate entries in the resulting table

Hi! I am joining 3 tables in SQL , I am getting the results I want exept it's duplicated. So the resultinmg table fom my stored procedure has 3 rows that have the same bulletin. How do I filter the storedprocedure to output only the rows that don't have duplicate entries for the column 'Bulletin' Thanks.

Here is my stored procedure:

PROCEDURE[dbo].[spGetCompBulletins]

@.Useriduniqueidentifier OUTPUT,

@.DisplayNamevarchar(200)

AS

SELECT*

FROMdbo.UserProfileINNER JOIN

dbo.bulletinsONdbo.UserProfile.UserId = dbo.bulletins.UseridINNER JOIN

dbo.AssociationsONdbo.Associations.BusinessID = dbo.bulletins.UseridWHEREUserProfile.DisplayName=@.DisplayName

andUserprofile.Userid = @.UseridORDER BYBulletins.Bulletin_Date

Return

can you post some sample results and expected output?

You can use GROUP BY to avoid duplicates.

|||

The results are kind large. But the stored procedure works fine..Is there a way to name the stored procedure as a Table then remove the rows that has the same bulletin? I trie AS myNewTable at the end of the storedprocedure but I don't think it's the right syntax. I was thinking of doing something like this:

delete from (storedprocedure here) where bulletin is the same , but I don't know the syntax for this. Thanks for the help!

|||

I dont need to see 100 rows. just 4-5 rows with the data should give me an idea of what you are asking for..

|||

I have the following:

keyid name bulletin

143 mark bulletin 1

143 mark bulletin 1

143 mark bulletin 1

The output should be :

keyid name bulletin

143 mark bulletin 1

Thanks!!

|||

I forgot to mention that the same keyid can appear multiple times because I am joining a table that has information about the user(userprofile) with a table that has information about bulletin and a table (associations) that has 2 fields userid and businessid (both userid and businessid can be the userid in userprofile) this is to know what user is associated with who. And I want the user to view only the bulletins of other users who are associated with them in the association table.

Cheers!

|||

For the given data :

SELECT KeyId, Name, Bulletin

FROM YourTable

GROUP BY KeyId, Name, Bulletin

|||

Thanks! It works great!

Cheers!

No comments:

Post a Comment