Wednesday, August 28, 2013

[SQL Server] SQL QUERY help - restricting select statement by removing 1 columns duplicates but retaining nulls

[SQL Server] SQL QUERY help - restricting select statement by removing 1 columns duplicates but retaining nulls


SQL QUERY help - restricting select statement by removing 1 columns duplicates but retaining nulls

Posted: 27 Aug 2013 10:21 PM PDT

Hi,The following code is almost working, it selects everything I want but slightly too much. It is duplicating some of the ld_id's as there are multiple records in the ls table against those ld_id's. I would like to remove these duplicates to basically retrun distinct ld_id's. I also want to retain the nulls against the ls_id's as it is important that I know which records do not have data in this column....[code="sql"] SELECT 'thistype'as ATYPE, ls.ls_id, ld.ld_id, ld.name FROM ld_table AS ld LEFT OUTER JOIN ls_table AS ls ON ld.ld_id = ls.ld_id --WHERE (ld.name LIKE '%' + @NAME + '%' OR @NAME IS NULL OR @NAME = '') --AND (ld.as_id = @ID OR @ID IS NULL OR @ID = '') ORDER BY ld.name asc [/code]I have tried various joins and subselects but either they returned the duplicates or didn't return the ld records without a ls_id....I hope you follow. Basically I want the distinct values from the ld_table with the ls_id populated from the ls_table.

No comments:

Post a Comment

Search This Blog