Question : Need help with Sql Query

I have a table named tblDestination with columns DestID, Description, Addr1, Addr2, City, Deleted.

I need to retrieve only those records with distinct Description and Description != NULL and deleted != false

Any help would be appreciated!!

Answer : Need help with Sql Query

Select DestID, Description, Addr1, Addr2, City, Deleted
FROM
(
     SELECT DestID, Description, Addr1, Addr2, City, Deleted,
--- row_number() - best to check books online
--- in brief, it allows you to analytically number the rows based on a particular order
--- you can also break up the data (partition), in this case each new description restarts the row number
      rn=row_number() over (partition by Description order by DestID desc)
     FROM tblDestination
     WHERE Description IS NOT NULL
       -- WHERE Description > ''   --- omit nulls and empty strings
       --and Deleted != 0  ---- assuming deleted is bit?
       and Deleted != 'false '  --- string 'false'
) SQ
--- this filters the subquery for where the rn value is 1, i.e. the first record in a partition
Where rn=1


To see what it actually does, run the below on its own

     SELECT DestID, Description, Addr1, Addr2, City, Deleted,
      rn=row_number() over (partition by Description order by DestID desc)
     FROM tblDestination
     WHERE Description IS NOT NULL
       -- WHERE Description > ''   --- omit nulls and empty strings
       --and Deleted != 0  ---- assuming deleted is bit?
       and Deleted != 'false '  --- string 'false'
     ORDER BY Description, DestID desc
Random Solutions  
 
programming4us programming4us